惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
Last Week in AI
Last Week in AI
T
Tailwind CSS Blog
WordPress大学
WordPress大学
B
Blog RSS Feed
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
aimingoo的专栏
aimingoo的专栏
MongoDB | Blog
MongoDB | Blog
博客园 - Franky
C
Check Point Blog
P
Proofpoint News Feed
H
Help Net Security
月光博客
月光博客
博客园_首页
Stack Overflow Blog
Stack Overflow Blog
博客园 - 三生石上(FineUI控件)
Martin Fowler
Martin Fowler
Recent Announcements
Recent Announcements
人人都是产品经理
人人都是产品经理
U
Unit 42
美团技术团队
I
InfoQ
A
About on SuperTechFans

kshao-blog-前端知识记录

win or mac clash 无 TUN 让 Antigravity、Chrome 强制 proxy(解决 Antigravity 无法加载选择 model、自动更新无法登录、跳转) 【大杂烩】在 pnpm 中直接修改 node_modules(.pnpm) 中的依赖项,项目中持久化 - pnpm 中的依赖处理、幽灵依赖、寻址规则等 在 html 中直接使用 Esm、Jsx 脚本快速调试和使用 React@19 和 Vue@3 源码,解决 React19 UMD 构建等问题 一键在本地批量检测并升级更新 package.json 中的模块依赖,ncu(npm-check-updates)在 npm、pnpm 或 workspace 项目中的使用教程 解决 Mac Docker Desktop 中启动出现的问题合集 通过阿里云、腾讯云无服务器搭建自定义的企业域名邮箱,实现在 QQ邮箱 收发等功能(附腾讯 SMTP 和 IMAP) 解决使用代理(clash 等)进行 SSH 连接(如 Github ssh key clone/push)出现 kex_exchange_identification 错误 静态文件资源 cdnjs, jsdelivr 抖音字节国内快速 CDN 镜像推荐【2025】- 仍在使用 bootcdn 和 staticfile CDN 请注意验证资源的完整性(SRI) pnpm monorepo 中管理依赖的最佳实践,与 Catalogs(目录)协议的使用(monorepo 中统一版本管理) Web 安全中的 Secure Contexts(安全上下文)- 解决在本地中使用 clipboard 或 Crypto 等 API 限制或关闭上下文限制 使用 serve 配合 openssl 或 mkcert 创建本地自签名可信任的证书 - 创建本地 TLS\SSL https 协议服务 利用 Github Actions 和 Acme 自动申请、更新和部署至阿里云、腾讯云 CDN Lets Encrypt SSl\TLS ECC RSA 双证书 【CSS】解决在 flex 容器中使用 align-content 或 justify-content 属性 center 居中时的溢出滚动和截断问题 - 理解 safe 关键字 在线工具 - 一键获取下载抖音无水印视频、抖音去水印解析工具、下载抖音无水印高清图集【2025 最新】 【React Router】v6 data router 在非组件(或工具方法)中如何优雅的跳转路由 【React】为什么路由跳转时页面滚动高度不会被重置(保留上个页面高度)?理解 history scrollRestoration 的场景与使用,以及如何使用 React Router 重置和跳转前保留滚动高度 【React】在本地 Html 中快速 debug(调试)React 源码 【React】结合源码和 EventLoop 分析 - 为什么 useLayoutEffect 会阻止 DOM 重绘(而 useEffect 闪烁)?为什么其内部 useState 会“同步”执行? React Compiler - 解放在函数中编程时的性能焦虑(React Conf 2024)附 Next 在线演示 在 github actions 中获取时间,并转换为中国标准时间(中国时区) 【npm】npm ci - npm clean install,在 CI、CD 中保持构建的一致性和可重复性 eslint 9.x 升级或使用指南,eslint.config.js 配置,包含 react、typescript、prettier 等常用配置升级迁移 使用 Spicetify 自定义 Spotify - 歌词翻译、全屏展示、主题替换 在 node 中快速代理请求(Proxy),解决跨域或请求转发问题 - http-proxy-middleware 修改请求体和返回 在 Hexo 中使用 AI(Gemini、deepseek、Azure)生成文章摘要,支持自定义模板。hexo-ai-summaries 插件文档(默认适配主题 Butterfly) 解决在 webstorm 或 idea 等 jetbrains 工具中遇到 Git 无法 force push,或 force push 灰色禁用无法点击(protected branches) hexo-seo-submit,Hexo 博客 SEO 优化插件 - 每日定时自动或手动提交链接至百度、Bing、Google,支持 Github Actions 和 Coding Jenkins 等CI(Hexo 插件编写) 一文吃透 pnpm 如何使用 workspace 构建 monorepo,与 npm、yarn 的用法对比(pnpm 9.x 内部安装依赖问题 link-workspace-packages) 【Node】Corepack - 解决 pnpm 或 yarn 的多版本管理、解决本地版本与 packageManager 中的版本一致性问题 解决 npm、cnpm 或 pnpm install 遇到 certificate has expired (证书已过期)
【CSS】解决外边距重叠(重合)引起的 margin 垂直方向(top...
kshao · 2024-05-15 · via kshao-blog-前端知识记录

随着各种 UI 库的使用,像这种问题在业务中可能都很少接触,但是仍需了解其现象和解决方案。

一键直达

  • 相邻兄弟元素的外边距重叠
  • 父级与子级的外边距重叠
  • 空块级元素的外边距重叠

什么是外边距重叠 —— Margin Collapsing

当块级元素(Block)的上下外边距与其相邻或嵌套的块级元素外边距重叠时,外边距会合并为一个外边距,而不是相加。这种行为成为外边距重叠。(浮动和绝对定位的元素不会发生外边距重叠)

外边距重叠只会发生在块级元素(Block)上,例如 divpsection 等。

外边距重叠计算规则

示例元素

<div class="css-fix-margin-item-parent">
  <div class="css-fix-margin-item-child">child div</div>
</div>
  1. 两个正数的外边距取最大的边距作为外边距
  2. 一个正数一个为负数,外边距为两者之和
.css-fix-margin-item-parent {
  margin-top: 20px;
  background-color: red;

  .css-fix-margin-item-child {
    margin-top: -30px;
    background-color: green;
  }
}
  1. 两个都是负数的外边距,外边距为两者绝对值的最大值
.css-fix-margin-item-parent {
  margin-top: -20px;
  background-color: red;

  .css-fix-margin-item-child {
    margin-top: -30px;
    background-color: green;
  }
}

出现外边距重叠的几个场景

示例可在控制面板调试,释放注释代码即可

相邻兄弟元素的外边距重叠

解决方法:

  • 父级元素设置弹性或网格布局:display: flex | grid or inline-flex 等…
  • 子级底部元素设置:float: left or position: absolute | fixed
  • 统一使用 margin-top or margin-bottom,而非共存
  • 子级相邻之间增加一个占位元素
<style>
  .css-fix-margin-container {
    /*display: flex;*/
    /*flex-direction: column;*/
    /*display: grid;*/

    .css-fix-margin-item-siblings:first-of-type {
      margin-bottom: 20px;
      background-color: red;
    }

    .css-fix-margin-item-siblings:last-of-type {
      margin-top: 30px;
      background-color: green;

      /*position: absolute;*/
      /*float: left;*/
      /*width: 100%;*/
      /*&::after {*/
      /*  content: "";*/
      /*  clear: both;*/
      /*}*/
    }
  }
</style>

<div class="css-fix-margin-container">
  <div class="css-fix-margin-item-siblings">sibling</div>
  <!-- <div>placeholder</div> -->
  <div class="css-fix-margin-item-siblings">sibling</div>
</div>

父级与子级的外边距重叠

当父元素无 borderpaddinginline-contentclearance 时,子元素的 margin-top or margin-bottom 会与父元素的 margin 产生重叠问题

解决方法:

  • 父元素添加:外边距方向不为 0 的 paddingborder
  • 父元素创建 BFC 环境,如:display: flex | gridoverflow: hidden 等…
  • 子元素绝对定位:position: absolute | fixed or float: left
  • 父子元素非相邻嵌套(外边距子元素与父元素之间填充内容)
<style>
  .css-fix-margin-container {
    .css-fix-margin-item-parent {
      margin-top: 20px;

      /*border-top: 1px transparent solid;*/
      /*display: inline-block;*/
      /*display: table;*/
      /*padding-top: 1px;*/
      /*position: absolute;*/
      /*overflow: hidden;*/

      .css-fix-margin-item-child {
        margin-top: 30px;
        /*position: absolute | fixed;*/
      }
    }
  }
</style>

<div class="css-fix-margin-container">
  <div class="css-fix-margin-item-parent">
    <!-- <div>placeholder</div> -->
    <div class="css-fix-margin-item-child">child div</div>
  </div>
</div>

空块级元素的外边距重叠

当块级元素没有 paddingborderheightinline content 时,上下外边距会重叠

解决方法:

  • 上面的条件反推即可,自身添加:paddingborderheightinline content
  • 自身创建 BFC 环境,如:display: flex | gridoverflow: hidden 等…
  • 父级元素添加:display: flex | grid
<style>
  .css-fix-margin-container {
    /* display: flex */
    /* display: grid */
    
    .css-fix-margin-item-empty {
      margin-top: 20px;
      margin-bottom: 30px;

      /*border-top: 1px transparent solid;*/
      /*display: inline-block;*/
      /*display: table;*/
      /* any direction & not 0 */
      /*padding-top: 1px;*/
      /*position: absolute;*/
      /*overflow: hidden;*/
    }
  }
</style>

<div class="css-fix-margin-container">
  <div class="css-fix-margin-item-empty"></div>
  <div style="background-color: red">empty</div>
</div>

Mastering margin collapsing
Block formatting context
https://juejin.cn/post/6953500845426081800#heading-2

版权声明: 本博客所有文章除特别声明外,均采用 CC BY-NC-SA 4.0 许可协议。转载请注明来源 kshao-blog-前端知识记录

赞助

  • 微信

    微信

  • 支付宝

    支付宝


avatar

kshao

免费服务器,kshao-blog,seo,面试题,前端,vue,react,JavaScript,js,html,css,less

Follow Me