CSS 布局——清除浮动 (二)

目录

[1. 清除浮动](#1. 清除浮动)

[2. 清除浮动本质](#2. 清除浮动本质)

[3. 清除浮动](#3. 清除浮动)

[4. 清除浮动方法](#4. 清除浮动方法)

[4.1 额外标签法](#4.1 额外标签法)

[4.1.1 总结](#4.1.1 总结)

[4.2 父级添加 overflow](#4.2 父级添加 overflow)

[4.3 after 伪元素法](#4.3 after 伪元素法)

[4.4 双伪元素清除浮动](#4.4 双伪元素清除浮动)

[5 总结](#5 总结)


1. 清除浮动

这是上面的源代码:

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /* 不给高度,只给宽度,其中的内容会撑开外面的大盒子 */
        .box {
            background-color: bisque;
            width: 600px;
            border: 5px red solid;
        }

        /* 添加浮动后,不给高度的大盒子高度为 0,因为浮动的元素不占有位置 */
        .one,.two {
            width: 200px;
            height: 200px;
            background-color: aquamarine;
            float: left;
        }

        /* 布局再添加一个 footer,布局就会更加乱 */
        .footer {
            height: 200px;
            background-color: burlywood;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="one">111</div>
        <div class="two">222</div>
    </div>
    <div class="footer"></div>
</body>
</html>
2. 清除浮动本质
3. 清除浮动
4. 清除浮动方法
4.1 额外标签法

html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /* 不给高度,只给宽度,其中的内容会撑开外面的大盒子 */
        .box {
            background-color: bisque;
            width: 600px;
            border: 5px red solid;
        }

        /* 添加浮动后,不给高度的大盒子高度为 0,因为浮动的元素不占有位置 */
        .one,.two {
            width: 200px;
            height: 200px;
            background-color: aquamarine;
            float: left;
        }

        /* 布局再添加一个 footer,布局就会更加乱 */
        .footer {
            height: 200px;
            background-color: burlywood;
        }

        .cle {
            clear: both;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="one">111</div>
        <div class="two">222</div>
        <div class="cle"></div>
    </div>
    <div class="footer"></div>
</body>
</html>
4.1.1 总结
4.2 父级添加 overflow
html 复制代码
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <style>
        /* 不给高度,只给宽度,其中的内容会撑开外面的大盒子 */
        .box {
            overflow: hidden;
            background-color: bisque;
            width: 600px;
            border: 5px red solid;
        }

        /* 添加浮动后,不给高度的大盒子高度为 0,因为浮动的元素不占有位置 */
        .one,.two {
            width: 200px;
            height: 200px;
            background-color: aquamarine;
            float: left;
        }

        .footer {
            height: 200px;
            background-color: burlywood;
        }
    </style>
</head>
<body>
    <div class="box">
        <div class="one">111</div>
        <div class="two">222</div>
    </div>
    <div class="footer"></div>
</body>
</html>

4.3 after 伪元素法

使用时直接复制以下代码即可:

html 复制代码
.clearfix:after {
            content:"";
            display: block;
            height: 0;
            clear: both;
            visibility: hidden;
        }

      /*  .clearfix {
            *zoom: 1;
       } */
4.4 双伪元素清除浮动

使用时直接复制以下代码即可:

html 复制代码
.clearfix:before,.clearfix:after {
            content: "";
            display: table;
        }

        .clearfix:after {
            clear: both;
        }

       /*  .clearfix {
            *zoom: 1;
        }  */
5 总结
相关推荐
灵感__idea18 分钟前
Hello 算法:众里寻她千“百度”
前端·javascript·算法
yinuo42 分钟前
轻松接入大语言模型API -04
前端
袋鼠云数栈UED团队1 小时前
基于 Lexical 实现变量输入编辑器
前端·javascript·架构
cipher2 小时前
ERC-4626 通胀攻击:DeFi 金库的"捐款陷阱"
前端·后端·安全
UrbanJazzerati2 小时前
非常友好的Vue 3 生命周期详解
前端·面试
AAA阿giao2 小时前
从零构建一个现代登录页:深入解析 Tailwind CSS + Vite + Lucide React 的完整技术栈
前端·css·react.js
兆子龙3 小时前
像 React Hook 一样「自动触发」:用 Git Hook 拦住忘删的测试代码与其它翻车现场
前端·架构
兆子龙3 小时前
用 Auto.js 实现挂机脚本:从找图点击到循环自动化
前端·架构
SuperEugene4 小时前
表单最佳实践:从 v-model 到自定义表单组件(含校验)
前端·javascript·vue.js
昨晚我输给了一辆AE864 小时前
为什么现在不推荐使用 React.FC 了?
前端·react.js·typescript