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 总结

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值