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 天前
Vue3 零基础每日笔记(055):组件里正确使用 store——storeToRefs 解构与 $patch 批量修改
笔记·vue·html·前端开发·软件开发
吴声子夜歌1 天前
HTML——看似普通的元素的背后(<a>标签)
html
IMPYLH1 天前
HTML 的 <style> 元素
前端·html
刃神太酷啦1 天前
前端入门第一课:HTML 基础语法 + 常用标签 + 实战全解
服务器·c语言·前端·javascript·css·c++·html
IMPYLH1 天前
HTML 的 <strong> 元素
前端·html
开开心心就好1 天前
批量提取PDF中的图片,直接导出原图
前端·javascript·支持向量机·智能手机·pdf·html·启发式算法
扶苏10022 天前
CSS 图标换色指南:用 brightness / invert / mask 把任意图片改成想要的颜色
css
我命由我123452 天前
CSS - CSS 媒体查询 orientation
前端·javascript·css·html·css3·html5·js
咩咩啃树皮2 天前
ES6 Set 核心特点 + 最简数组去重
前端·javascript·html
zhanghaha13142 天前
HTML系列教程:18_HTML / CSS 颜色零基础详解
css·html·tensorflow