父子边距重合

BFC【块级格式化上下文】+IFC【行内格式化上下文】
BFC(边距重叠解决方案)

  • 其他博主的文章,写的挺好的
1-1 概念

1、概念: 边界重合是指 两个或多个盒子(可能相邻也可能嵌套)的相邻边界 重合在一起而形成一个单一边界

1-2 例子: 父子元素的边界重叠

1、

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <style>
        .parent {
            background-color: red;
            /* height: 110px; */
            /*overflow: hidden;*/
        }
        
        .parent .child {
            background: #c8cdf5;
            height: 100px;
            margin-top: 10px;
        }
    </style>
    <section class="parent">
        <article class="child"></article>
    </section>
</body>

</html>

预期效果:
在这里插入图片描述
实际效果:
在这里插入图片描述
2、 父元素的高度不是110px,而是100px,在这里发生了高度塌陷
3、 产生原因: 是如果块元素的margin-top与它的第一个子元素的margin-top之间没有border、padding、inlinecontent、clearance来分隔,或者块元素的 margin-bottom 与它的最后一个子元素的 margin-bottom 之间没有border、padding、inlinecontent、height、min-height、max-height分隔,那么外边距会塌陷。子元素多余的外边距会被父元素的外边距截断。

1-3 解决方法

1、 父子元素的边界重叠解决方案: 在父元素上加上overflow:hidden;,使其成为BFC
2、

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <style>
        .parent {
            background-color: red;
            /* height: 110px; */
            overflow: hidden;
        }
        
        .parent .child {
            background: #c8cdf5;
            height: 100px;
            margin-top: 10px;
        }
    </style>
    <section class="parent">
        <article class="child"></article>
    </section>
</body>

</html>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值