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 总结
相关推荐
web守墓人42 分钟前
【前端】ikun-markdown: 纯js实现markdown到富文本html的转换库
前端·javascript·html
Savior`L1 小时前
CSS知识复习5
前端·css
许白掰1 小时前
Linux入门篇学习——Linux 工具之 make 工具和 makefile 文件
linux·运维·服务器·前端·学习·编辑器
中微子5 小时前
🔥 React Context 面试必考!从源码到实战的完整攻略 | 99%的人都不知道的性能陷阱
前端·react.js
Liudef066 小时前
儿童趣味记忆配对游戏
css·游戏·css3
中微子6 小时前
React 状态管理 源码深度解析
前端·react.js
加减法原则7 小时前
Vue3 组合式函数:让你的代码复用如丝般顺滑
前端·vue.js
yanlele8 小时前
我用爬虫抓取了 25 年 6 月掘金热门面试文章
前端·javascript·面试
lichenyang4538 小时前
React移动端开发项目优化
前端·react.js·前端框架
你的人类朋友8 小时前
🍃Kubernetes(k8s)核心概念一览
前端·后端·自动化运维