CSS清除浮动

12 篇文章 0 订阅

CSS清除浮动的方法

相信很多刚入前端的道友都会对清除浮动很陌生,我也一样不知道怎么回事,看了很多资料,有了一定了解。这是一项前端页面设计必备的知识点。

方法一
设置父级容器的高度,自己撑起来。

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>父级设置高度去除浮动</title>
    <style type="text/css">
        .content {
            background: #000080;
            border: 1px solid red;
            /*解决代码*/
            height: 200px;
        }

        .footer {
            background: #800080;
            border: 1px solid red;
            height: 100px;
            margin-top: 10px
        }

        .left {
            float: left;
            width: 20%;
            height: 200px;
            background: #DDD
        }

        .right {
            float: right;
            width: 30%;
            height: 80px;
            background: #DDD
        }
    </style>
</head>

<body>
    <div class="content">
        <div class="left">Left</div>
        <div class="right">Right</div>
    </div>
    <div class="footer">
        footer
    </div>
</body>

</html>

原理:父级div手动定义height,就解决了父级div无法自动获取到高度的问题。

优点:简单、代码少、容易掌握

缺点:只适合高度固定的布局,要给出精确的高度,如果高度和父级div不一样时,会产生问题

建议:不推荐使用,只建议高度固定的布局时使用

方法二
通过设置clear:both 实现

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>设置clear:both </title>
    <style type="text/css">
        .content {
            background: #000080;
            border: 1px solid red
        }

        .footer {
            background: #800080;
            border: 1px solid red;
            height: 100px;
            margin-top: 10px
        }

        .left {
            float: left;
            width: 20%;
            height: 200px;
            background: #DDD
        }

        .right {
            float: right;
            width: 30%;
            height: 80px;
            background: #DDD
        }
        /*清除浮动代码*/

        .clearfloat {
            clear: both
        }
    </style>
</head>

<body>
    <div class="content">
        <div class="left">Left</div>
        <div class="right">Right</div>
        <div class="clearfloat"></div>
    </div>
    <div class="footer">
        footer
    </div>
</body>

</html>

原理:添加一个空div,利用css提高的clear:both清除浮动,让父级div能自动获取到高度

优点:简单、代码少、浏览器支持好、不容易出现怪问题

缺点:不少初学者不理解原理;如果页面浮动布局多,就要增加很多空div,让人感觉很不好

建议:不推荐使用,但此方法是以前主要使用的一种清除浮动方法

方法三
父级容器定义 伪类:after 和 zoom

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>伪类:after 和 zoom </title>
    <style type="text/css">
        .content {
            background: #000080;
            border: 1px solid red;
        }

        .footer {
            background: #800080;
            border: 1px solid red;
            height: 100px;
            margin-top: 10px
        }

        .left {
            float: left;
            width: 20%;
            height: 200px;
            background: #DDD
        }

        .right {
            float: right;
            width: 30%;
            height: 80px;
            background: #DDD
        }
        /*清除浮动代码*/

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

        .clearfloat {
            zoom: 1
        }
    </style>
</head>

<body>
    <div class="content clearfloat">
        <div class="left">Left</div>
        <div class="right">Right</div>
    </div>
    <div class="footer">
        footer
    </div>
</body>

</html>

原理:IE8以上和非IE浏览器才支持:after,原理和方法2有点类似,zoom(IE转有属性)可解决ie6,ie7浮动问题

优点:浏览器支持好、不容易出现怪问题(目前:大型网站都有使用,如:腾迅,网易,新浪等等)

缺点:代码多、不少初学者不理解原理,要两句代码结合使用才能让主流浏览器都支持。

建议:推荐使用,建议定义公共类,以减少CSS代码。

方法四
父级容器定义 overflow:hidden

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>overflow: hidden</title>
    <style type="text/css">
        .content {
            background: #000080;
            border: 1px solid red;
            /*解决代码*/
            width: 98%;
            overflow: hidden
        }

        .footer {
            background: #800080;
            border: 1px solid red;
            height: 100px;
            margin-top: 10px;
            width: 98%
        }

        .left {
            float: left;
            width: 20%;
            height: 200px;
            background: #DDD
        }

        .right {
            float: right;
            width: 30%;
            height: 80px;
            background: #DDD
        }
    </style>
</head>

<body>
    <div class="content">
        <div class="left">Left</div>
        <div class="right">Right</div>
    </div>
    <div class="footer">
        footer
    </div>
</body>

</html>

原理:必须定义width或zoom:1,同时不能定义height,使用overflow:hidden时,浏览器会自动检查浮动区域的高度

优点:简单、代码少、浏览器支持好

缺点:不能和position配合使用,因为超出的尺寸的会被隐藏。

建议:只推荐没有使用position或对overflow:hidden理解比较深的朋友使用。

方法五
父级容器定义 overflow:auto

<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>overflow: auto</title>
    <style type="text/css">
        .content {
            background: #000080;
            border: 1px solid red;
            /*解决代码*/
            width: 98%;
            overflow: auto
        }

        .footer {
            background: #800080;
            border: 1px solid red;
            height: 100px;
            margin-top: 10px;
            width: 98%
        }

        .left {
            float: left;
            width: 20%;
            height: 200px;
            background: #DDD
        }

        .right {
            float: right;
            width: 30%;
            height: 80px;
            background: #DDD
        }
    </style>
</head>

<body>
    <div class="content">
        <div class="left">Left</div>
        <div class="right">Right</div>
    </div>
    <div class="footer">
        footer
    </div>
</body>

</html>

原理:必须定义width或zoom:1,同时不能定义height,使用overflow:auto时,浏览器会自动检查浮动区域的高度

优点:简单、代码少、浏览器支持好

缺点:内部宽高超过父级div时,会出现滚动条。

建议:不推荐使用,如果你需要出现滚动条或者确保你的代码不会出现滚动条就使用吧。

参考文章http://www.jb51.net/css/173023.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值