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 总结
相关推荐
JavaGuide2 小时前
Gemini 真的变强了,写代码做视频的能力接近 Claude Opus5.5 了!
前端·ai编程
niceyayale3 小时前
WebRTC 延迟怎么调试:从 ICE 到 jitter buffer 的六个观测点
前端
aleimu3 小时前
Windows 11 右键打开 Cmder 2.0:从找不到菜单到默认使用 Cmder
前端·人工智能·后端
tryCbest3 小时前
Vue系列——第 20 篇:Vue Router 4——路由管理完全指南(上)
前端·javascript·vue.js
feixing_fx3 小时前
原始类型与引用类型:赋值时到底复制了什么
开发语言·前端·javascript·前端框架
一条破秋裤3 小时前
【1115】【多线程】【笔记】
前端·chrome·笔记
黑十3 小时前
FDE 怎么快速搭一个可演示页面?最小前端方案
java·大数据·前端·数据库·人工智能
Funlang3 小时前
在 Windows 95 上写前端
前端·javascript·windows
cidy_983 小时前
python第7课. 输入与输出
前端
小龙66664 小时前
给 Agent 一张地图,而不是一座迷宫:前端 monorepo 目录结构重构实录
前端·agent