当前位置: 首页 > article >正文

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

目录

1. 清除浮动

2. 清除浮动本质

3. 清除浮动

4. 清除浮动方法

4.1 额外标签法

4.1.1 总结

4.2 父级添加 overflow

 4.3 after 伪元素法

 4.4 双伪元素清除浮动

5 总结


1. 清除浮动

 

 

这是上面的源代码:

<!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 额外标签法

<!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

<!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 伪元素法

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

.clearfix:after {
            content:"";
            display: block;
            height: 0;
            clear: both;
            visibility: hidden;
        }

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

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

.clearfix:before,.clearfix:after {
            content: "";
            display: table;
        }

        .clearfix:after {
            clear: both;
        }

       /*  .clearfix {
            *zoom: 1;
        }  */
5 总结


http://www.kler.cn/news/336253.html

相关文章:

  • hutool bug
  • 核心理论框架对比:如何通过TOGAF实现企业数字化转型的全面战略
  • javaweb-分层解耦
  • 接口怎么返回文件或数据流,@RequestMapping注解里面的属性项有哪些
  • 计算机组成原理(笔记6阵列乘法器、补码阵列乘法器)
  • Nuxt.js 应用中的 app:beforeMount 钩子详解
  • 18年408数据结构
  • 论文笔记:微表情欺骗检测
  • FineReport 11 在线学习
  • INS风格时尚自拍人像摄影后期Lr调色,手机滤镜PS+Lightroom预设下载!
  • Android阶段学习思维导图
  • pytorch中的TensorDataset和DataLoader
  • 红外画面空中目标检测系统源码分享
  • LeetCode讲解篇之139. 单词拆分
  • JS模块化工具requirejs详解
  • webpack/vite的区别
  • Oracle架构之物理存储之日志文件
  • 计算机毕业设计 基于Python的智能文献管理系统的设计与实现 Python+Django+Vue 前后端分离 附源码 讲解 文档
  • 【图像处理】多幅不同焦距的同一个物体的平面图象,合成一幅具有立体效果的单幅图像原理(一)
  • MFC工控项目实例二十二主界面计数背景颜色改变