BFC

一.BFC定义

他解决了块级元素如何对它的内容进行布局,以及与其他元素的关系和相互作用

关键词解释:

块级元素:父级(是一个块元素)

内容:子元素(是一个块元素)

其它元素:与内容同级别的兄弟元素

相互作用:BFC里的元素与外面的元素不会发生影响

二.BFC触发方式

  1. float的值不为none
  2. overflow的值不为visible
  3.  display的值为table-cell,table-caption或inline-block之一
  4. position的值不为static或releative中任何一个
  5. 弹性项:display:flex
  6. 网格项:display:grid

三.BFC的布局规则

  1. margin不会传递给父级(父级触发了BFC)
<!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            body{
                display: flex;
                justify-content: center;
            }
            .father{
                    width: 500px;
                    height: 500px;
                    background-color: hotpink;
                    display: flex;
                    justify-content: center;
            }
            .son{
                width: 300px;
                height: 100px;
                background-color: deepskyblue;
                margin-top: 100px;
            }
            
        </style>
    </head>
    <body>
        <div class="father">
            <div class="son">
    
            </div>
        </div>
    </body>
    </html>

    

       2.非浮动元素不会覆盖浮动元素的位置(非浮动元素触发了BFC)

    <!DOCTYPE html>
    <html lang="en">
    <head>
        <meta charset="UTF-8">
        <title>Title</title>
        <style>
            .first{
                    width: 300px;
                    height: 300px;
                    background-color: hotpink;
                    float: left;
            }
            .second{
                width: 300px;
                height: 300px;
                background-color: deepskyblue;
                overflow: hidden;
            }

        </style>
    </head>
    <body>
        <div class="first"></div>
        <div class="second"></div>
    </body>
    </html>

    3.两个相邻的元素上下margin不会重叠(其中一个元素添加一个单独的父级,然后让他的父级触发了BFC)

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <style>
        .father{
            overflow: hidden;
        }
        .son{
            width: 100px;
            height: 100px;
            background-color: yellow;
            margin-bottom: 50px;
        }
        .uncle{
            width: 100px;
            height: 100px;
            background-color: deepskyblue;
            margin-top: 50px;
        }
    </style>
</head>
<body>
<div class="father">
    <div class="son"></div>
</div>
<div class="uncle">

</div>
</body>
</html>


  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值