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>

效果如下:

相关推荐
jingling5556 小时前
CSS进阶 | 不用一行JS!用纯CSS打造会动的现代化单页应用(3D翻转卡片)
前端·javascript·css
Gee Explorer8 小时前
第一篇使用HTML写一个随机点名网页
css·html5·1024程序员节
奶酪博士8 小时前
【html】每日打卡页面
html·1024程序员节
元直数字电路验证8 小时前
HTML 标签及推荐嵌套结构
前端·javascript·html
charlie1145141918 小时前
HTML 理论笔记
开发语言·前端·笔记·学习·html·1024程序员节
前端橙一陈9 小时前
彻底理解 Bootstrap 的响应式断点机制:从 CSS 到 SCSS 的实现原理
css·bootstrap·scss
Mr.Jessy10 小时前
JavaScript学习第六天:函数
开发语言·前端·javascript·学习·html·1024程序员节
神膘护体小月半10 小时前
css 的 clip-path 属性,绘制气泡
css
代码改变世界1008610 小时前
像素塔防游戏:像素守卫者
css·游戏·css3·1024程序员节
BUG_Jia14 小时前
如何用 HTML 生成 PC 端软件
前端·javascript·html·桌面应用·1024程序员节