BFC(块级格式上下文)

BFC算是比较理论的东西,在实际应用中比较少应用到

一:概念

BFC(Block formatting context),块级格式上下文,它是指一个独立的块级渲染区域,只有Block-level Box(块级元素)参与,该区域拥有一套渲染规则来约束块级盒子的布局,且与区域外部无关。

二:常见现状

当一个盒子没有设置高度的时候,当盒子内部的子元素都漂浮时,无法撑起自身

原因:这个盒子没有形成BFC

三:如何创建BFC

1,float的值不是none。

2,position的值不是static或者relative。

3,display的值是inline-block,flex或者inline-flex。

4,overflow:hidden

四:BFC的其他作用

1,可以取消盒子margin塌陷

2,可以阻止元素被浮动元素覆盖

五:示例

1:取消盒子margin塌陷

xml 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>margin塌陷</title>
    <style>
        .father{
            width: 200px;
            height: 300px;
            background: red;
        }
        .son{
            width: 100px;
            height: 100px;
            background: blue;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="son"></div>
    </div>
</body>
</html>

效果如下:子元素与父元素并没有间隔的20px。

当给父元素添加一个float:left;

xml 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>margin塌陷</title>
    <style>
        .father{
            width: 200px;
            height: 300px;
            background: red;
            float: left;
        }
        .son{
            width: 100px;
            height: 100px;
            background: blue;
            margin-top: 20px;
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="son"></div>
    </div>
</body>
</html>

效果如下:

2,阻止浮动元素覆盖

xml 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>阻止浮动元素覆盖</title>
    <style>
        .son{
            width: 200px;
            height: 200px;
            background-color: blueviolet;
            float: left;
        }
        .son-last{
            width: 200px;
            height: 300px;
            background-color: brown;
        }
    </style>
</head>
<body>
    <div class="son"></div>
    <div class="son"></div>
    <div class="son-last"></div>
</body>
</html>

效果如下:

给son-last添加overflow: hidden;

xml 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>阻止浮动元素覆盖</title>
    <style>
        .son{
            width: 200px;
            height: 200px;
            background-color: blueviolet;
            float: left;
        }
        .son-last{
            width: 200px;
            height: 300px;
            background-color: brown;
            overflow: hidden;
        }
    </style>
</head>
<body>
    <div class="son"></div>
    <div class="son"></div>
    <div class="son-last"></div>
</body>
</html>

效果如下:

相关推荐
天天扭码1 小时前
《很全面的前端面试题》——HTML篇
前端·面试·html
到底起什么网名才能不重名4 小时前
使用各种CSS美化网页
前端·css·vscode·bootstrap·html
sunbyte5 小时前
50天50个小项目 (Vue3 + Tailwindcss V4) ✨ | ContentPlaceholder(背景占位)
前端·javascript·css·vue.js·tailwindcss
码哥DFS8 小时前
Flex布局原理
前端·css·css3
小飞悟11 小时前
别再只会用 px 了!移动端适配必须掌握的 CSS 单位
前端·css·设计
默默地离开12 小时前
Blob二进制处理的王者
前端·javascript·html
晴殇i13 小时前
前端内容保护:如何有效防止用户复制页面内容?
前端·javascript·css
西西木科技丨Shopify开发机构14 小时前
如何在 Shopify 中建立重定向
前端·html
Liudef0614 小时前
基于HTML与Java的简易在线会议系统实现
java·前端·html
2401_8812444014 小时前
javaweb———html
前端·javascript·html