前端清除浮动

第一张方式:额外标签清楚法

<div style="clear: both;"></div> 

特点:如果清除了浮动,父亲去自动检测孩子高度,以最高为准

优点:简单易懂

缺点:添加额外的标签破坏结构

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>额外标签 清除浮动</title>
    <style>
    .father {

    }
    .big {
        width: 200px;
        height: 200px;
        background-color: blue;
        float: left;
    }
    .small {
        width: 100px;
        height: 100px;
        background-color: yellow;
        float: left;
    }
    .footer {
        width: 400px;
        height: 100px;
        background-color: pink;
    }
    /* 清除浮动 */
    /* .clear {
        clear: both;
        如果清除了浮动, 父亲去自动检测孩子的高度  以最高的为准
    } */

    </style>
</head>
<body>
    <div class="father">
        <div class="big"></div>
        <div class="small"></div>
    </div>
    <!-- 额外标签清除浮动 -->
    <!-- <div class="clear"></div> -->
    <div style="clear: both;"></div> <!-- 优点:简单易懂 缺点:添加了很多额外没用的标签破坏结构 -->
    <!-- 额外标签清除浮动 -->
    <div class="footer"></div>
</body>
</html>

第二种方式:overflow清除法

在父标签盒子里添加overflow:hidden

优点:代码简洁

缺点:内容增多时候容易造成不会自动换行导致内容被隐藏掉

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>overflow 清除浮动</title>
    <style>
    .father {
        /* 在父元素这里加上 overflow :hidden; 就可以清除浮动
        不是所有的浮动都要清除 谁影响就清除谁
        优点:代码简洁;
        缺点:内容增多时候容易造成不会自动换行导致内容被隐藏掉
        无法显示需要溢出的元素; */
        overflow: hidden;

    }
    .big {
        width: 200px;
        height: 200px;
        background-color: blue;
        float: left;
    }
    .small {
        width: 100px;
        height: 100px;
        background-color: yellow;
        float: left;
    }
    .footer {
        width: 400px;
        height: 100px;
        background-color: pink;
    }

    </style>
</head>
<body>
    <div class="father">
        <div class="big"></div>
        <div class="small"></div>
    </div>
    <div class="footer"></div>
</body>
</html>

第三种方式:after伪元素清除法

.clearfix:after{ /* 正常浏览器 清除浮动 */
        content: "";
        display: block;
        height: 0;
        clear: both;
        visibility: hidden;
    }
    .clearfix{ 
        *zoom: 1; /* zoom 1 就是ie6 清除浮动的方法 * 是ie7以下的版本所识别 */
    }
第四种方式:双伪元素标记法

.clearfix:before, .clearfix:after {
        content: "";
        display: table;
    }
    .clearfix:after {
        clear: both;
    }
    .clearfix {
        *zoom: 1;
    }
 

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>overflow 清除浮动</title>
    <style>
    .clearfix:before, .clearfix:after {
        content: "";
        display: table;
    }
    .clearfix:after {
        clear: both;
    }
    .clearfix {
        *zoom: 1;
    }

    .father {

    }
    .big {
        width: 200px;
        height: 200px;
        background-color: blue;
        float: left;
    }
    .small {
        width: 100px;
        height: 100px;
        background-color: yellow;
        float: left;
    }
    .footer {
        width: 400px;
        height: 100px;
        background-color: pink;
    }

    </style>
</head>
<body>
    <div class="father clearfix">
        <div class="big"></div>
        <div class="small"></div>
    </div>
    <div class="footer"></div>
</body>
</html>

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值