码迷,mamicode.com
首页 > Web开发 > 详细

前端布局之CSS清除浮动的六种方法

时间:2017-04-21 20:11:53      阅读:393      评论:0      收藏:0      [点我收藏+]

标签:size   pre   解决办法   float   absolute   utf-8   hidden   文档流   定位   

在网页开发过程中经常会用到各种浮动,此时就需要及时清除浮动,不然会对后面的布局造成影响。

技术分享
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .outer{
            border: 1px solid black;
            background: yellow;
            width:300px;
        }
        .inner{
            width: 50px;
            height: 50px;
            background-color: red;
            margin-right: 20px;
            float: left;
        }
        .footer{
            background-color: blue;
            width: 200px;
            height: 100px;
        }
    </style>
</head>
<body>
    <div class="outer">
        <div class="inner"></div>
        <div class="inner"></div>
        <div class="inner"></div>
    </div>
    <div class="footer"></div>
</body>
</html>
View Code

 

技术分享

我们都知道使用浮动会产生很多未知的问题,通过上面的例子我们可以发现 class="outer" 的高度并没有被里面的 div 给撑开,这是因为里面的 div 产生浮动而脱离了该文档,因为 outer本身没有高度,所以我们看不到它的黄色背景,看出本应包住3个 inner DIV的 outer DIV 却没有包住他们,此刻只剩一条由上下边框贴合组成的线,同时 footer DIV元素也跑到了三个浮动元素的底下。

解决办法:

1.给父元素设置高度

 

技术分享
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .outer{
            border: 1px solid black;
            background: yellow;
            width:300px;
            height: 100px;
        }
        .inner{
            width: 50px;
            height: 50px;
            background-color: red;
            margin-right: 20px;
            float: left;
        }
        .footer{
            background-color: blue;
            width: 200px;
            height: 100px;
        }
    </style>
</head>
<body>
    <div class="outer">
        <div class="inner"></div>
        <div class="inner"></div>
        <div class="inner"></div>
    </div>
    <div class="footer"></div>
</body>
</html>
View Code

 

 

技术分享

此时可以看到outer  DIV包住了inner DIV.

2.让父元素浮动

技术分享
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .outer{
            border: 1px solid black;
            background: yellow;
            float: left;
        }
        .inner{
            width: 50px;
            height: 50px;
            background-color: red;
            margin-right: 20px;
            float: left;
        }
        .footer{
            background-color: blue;
            width: 200px;
            height: 100px;
        }
    </style>
</head>
<body>
    <div class="outer">
        <div class="inner"></div>
        <div class="inner"></div>
        <div class="inner"></div>
    </div>
    <div class="footer"></div>
</body>
</html>
View Code

 

技术分享

此时,由于其父元素outer也浮动了,从而脱离了文档流,所以footer DIV上升到了原来outer DIV 所在的位置。(注意几个DIV的大小)。

3.为父元素添加overflow:hidden

技术分享
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .outer{
            border: 1px solid black;
            background: yellow;
            width:300px;
            overflow:hidden;
        }
        .inner{
            width: 50px;
            height: 50px;
            background-color: red;
            margin-right: 20px;
            float: left;
        }
        .footer{
            background-color: blue;
            width: 200px;
            height: 100px;
        }
    </style>
</head>
<body>
    <div class="outer">
        <div class="inner"></div>
        <div class="inner"></div>
        <div class="inner"></div>
    </div>
    <div class="footer"></div>
</body>
</html>
View Code

 

技术分享

实际上,overflow:hidden 的真正用途是防止包含元素被超大元素撑大,应用overflow:hidden 之后,多余的子元素内容会被容器剪切掉。从而迫使父元素包含住其浮动的子元素。

4.添加非浮动的清除元素

技术分享
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .outer{
            border: 1px solid black;
            background: yellow;
            width:300px;
        }
        .inner{
            width: 50px;
            height: 50px;
            background-color: red;
            margin-right: 20px;
            float: left;
        }
        .footer{
            background-color: blue;
            width: 200px;
            height: 100px;
        }
        .clear{
            clear: left;
        }
    </style>
</head>
<body>
    <div class="outer">
        <div class="inner"></div>
        <div class="inner"></div>
        <div class="inner"></div>
        <div class="clear"></div>
    </div>
    <div class="footer"></div>
</body>
</html>
View Code

 

技术分享

这种方法就是简单的在父元素中添加一个额外的标签,并给他应用clear属性、因为没有默认的样式,不会引入多余的空间,div 元素很适合用于这个目的、

5.用CSS来添加清除元素

技术分享
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .outer{
            border: 1px solid black;
            background: yellow;
            width:300px;
        }
        .inner{
            width: 50px;
            height: 50px;
            background-color: red;
            margin-right: 20px;
            float: left;
        }
        .footer{
            background-color: blue;
            width: 200px;
            height: 100px;
        }
        .clear{
            clear: left;
        }
    </style>
</head>
<body>
    <div class="outer">
        <div class="inner"></div>
        <div class="inner"></div>
        <div class="inner"></div>
        <div class="clear"></div>
    </div>
    <div class="footer"></div>
</body>
</html>
View Code

 

技术分享

这里是为outer DIV的元素内容后面添加一个不可见的内容。

这种方法在IE浏览器并不适用,因为 IE8以上才支持:after,对于IE6和IE7解决浮动问题可以在上述方法中添加如下代码:

技术分享
.clearfix{
zoom:1;
}
View Code

 

6.父元素绝对定位

技术分享
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title></title>
    <style>
        .outer{
            border: 1px solid black;
            background: yellow;
            width:300px;
            position: absolute;
        }
        .inner{
            width: 50px;
            height: 50px;
            background-color: red;
            margin-right: 20px;
            float: left;
        }
        .footer{
            background-color: blue;
            width: 200px;
            height: 100px;
        }
    </style>
</head>
<body>
    <div class="outer">
        <div class="inner"></div>
        <div class="inner"></div>
        <div class="inner"></div>
    </div>
    <div class="footer"></div>
</body>
</html>
View Code

 

技术分享

这种方法是在一篇文章中看到的。由于水平有限,暂时没有理解。

 

前端布局之CSS清除浮动的六种方法

标签:size   pre   解决办法   float   absolute   utf-8   hidden   文档流   定位   

原文地址:http://www.cnblogs.com/theone10/p/6745196.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!