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 小时前
Less功能介绍和完整使用示例
css
降临-max1 小时前
Selenium(HTML+JavaScript&DOM+Selenium IDE)
自动化测试·selenium·测试工具·html
用户298698530141 小时前
PDF 转 HTML 全攻略:三种方法轻松搞定网页转换
c#·html·.net
IMPYLH3 小时前
HTML 的 <nav> 元素
前端·html
IMPYLH3 小时前
HTML 的 <meter> 元素
前端·html
右耳朵猫AI4 小时前
Web前端周刊2026W34 | CSS类前缀选择器采纳、Lovable迁移TanStack Start、Fastify 6.0 Alpha
前端·css
Lenyiin4 小时前
第6篇_Python高级语法与标准库精髓:从会用到达精通
java·python·html
web打印社区12 小时前
网页静默打印热敏小票:从 HTML 到出纸的完整指南
前端·javascript·vue.js·electron·pdf·html
三十而立洋18 小时前
Flex 与 Grid 布局完全指南
css
用户298698530141 天前
React 中 HTML 内容转 Word 文档的实现方案
javascript·react.js·html