CSS外边距合并现象

外边距合并(Margin Collapsing)是指在文档流中,两个或多个相邻元素的外边距(margin)会合并为一个外边距,其大小会取其中最大的外边距值

当两个相邻的兄弟元素之间没有其他内容(如边框、内边距、行内元素等)分隔时,它们的垂直外边距会发生合并

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=, initial-scale=1.0">
    <title>Document</title>
    <style>
        .one {
            width: 100px;
            height: 100px;
            background-color: brown;
            margin-bottom: 80px;
        }
        .two {
            width: 100px;
            height: 100px;
            background-color: orange;
            margin-top: 50px;
        }
    </style>
</head>
<body>
    <div class="one">one</div>
    <div class="two"> two</div>
    
</body>
</html>

第一个 div元素的 margin - bottom 为 80px,第二个 div 元素的 margin - top 为 50px,最终它们之间的外边距会合并为 80px。

相关推荐
liliangcsdn4 分钟前
mac mlx大模型框架的安装和使用
java·前端·人工智能·python·macos
CssHero7 分钟前
基于vue3完成领域模型架构建设
前端
PanZonghui10 分钟前
用项目说话:我的React博客构建成果与经验复盘
前端·react.js·typescript
言兴13 分钟前
教你如何理解useContext加上useReducer
前端·javascript·面试
sunbyte17 分钟前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | GoodCheapFast(Good - Cheap - Fast三选二开关)
前端·javascript·css·vue.js·tailwindcss
前端的日常18 分钟前
网页视频录制新技巧,代码实现超简单!
前端
前端的日常20 分钟前
什么是 TypeScript 中的泛型?请给出一个使用泛型的示例。
前端
今禾21 分钟前
一行代码引发的血案:new Array(5) 到底发生了什么?
前端·javascript·算法
ccc101824 分钟前
老师问我localhost和127.0.0.1,有什么区别?
前端
Struggler28131 分钟前
Chrome插件开发
前端