美文网首页Html+Css
CSS 清除浮动

CSS 清除浮动

作者: roy_pub | 来源:发表于2018-12-09 21:09 被阅读16次

由于浮动元素不在占用原文档流的位置,所以会对后面的排版产生影响,因此需要在该元素中清除浮动。
清除浮动的本质是解决父级元素因为子级浮动引起内部高度为0的问题。

为何要清除浮动

如下,子盒子是标准流,父盒子虽然没有高度,但是会撑开父盒子的高度。

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        .father {
            background-color: cyan;
        }

        .father .son1 {
            width: 100px;
            height: 100px;
            background-color: green;
        }

        .father .son2 {
            width: 100px;
            height: 100px;
            background-color: red;
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="son1">son box</div>
        <div class="son2">son box</div>
    </div>
</body>
</html>

子盒子添加浮动,浮动不占用位置,因为父盒子没有高度,导致父盒子的高度为0

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        .father {
            background-color: cyan;
            /*height: 100px;*/
        }

        .son1 {
            width: 100px;
            height: 100px;
            background-color: green;
            float: left;
        }

        .son2 {
            width: 100px;
            height: 100px;
            background-color: red;
            float: left;
        }

    </style>
</head>
<body>
    <div class="father">
        <div class="son1">son1 box</div>
        <div class="son2">son2 box</div>
    </div>
</body>
</html>

因为父盒子的高度为0,会影响其它盒子

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        .father {
            background-color: cyan;
        }

        .son1 {
            width: 100px;
            height: 100px;
            background-color: green;
            float: left;
        }

        .son2 {
            width: 100px;
            height: 100px;
            background-color: red;
            float: left;
        }

        .footer {
            height: 120px;
            background-color: black;
        }
    </style>
</head>
<body>
    <div class="father">
        <div class="son1">son1 box</div>
        <div class="son2">son2 box</div>
    </div>
    <div class="footer"></div>

</body>
</html>

如何清除浮动

  • 额外标签法
  • 父级添加overflow属性方法
  • 使用after伪元素清除浮动
  • 使用before和after双伪元素清除浮动
额外标签法

选择器 {clear: 属性值;}

属性值 说明
left 不允许左侧有浮动元素(清除左侧的浮动影响)
right 不允许右侧有浮动元素(清除右侧浮动的影响)
both 同时清除左右两侧浮动的影响

额外标签法是W3C推荐的做法是通过在浮动元素末尾添加一个空的标签,例如<div style="clear: both"></div>或其它br亦可。

  • 优点:通俗易懂,书写方便
  • 确定:添加许多无意义的标签,结构化较差。
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        .father {
            background-color: cyan;
        }

        .son1 {
            width: 100px;
            height: 100px;
            background-color: green;
            float: left;
        }

        .son2 {
            width: 100px;
            height: 100px;
            background-color: red;
            float: left;
        }

        .footer {
            height: 120px;
            background-color: black;
        }

        .clear {
            clear: both;
        }

    </style>
</head>
<body>
    <div class="father">
        <div class="son1">son1 box</div>
        <div class="son2">son2 box</div>
        <!--最有浮动标签后添加一个标签,清除浮动-->
        <div class="clear"></div>
    </div>

    <div class="footer"></div>
</body>
</html>
父级添加overflow属性方法
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        .father {
            background-color: cyan;
            overflow: hidden;
        }

        .son1 {
            width: 100px;
            height: 100px;
            background-color: green;
            float: left;
        }

        .son2 {
            width: 100px;
            height: 100px;
            background-color: red;
            float: left;
        }

        .footer {
            height: 120px;
            background-color: black;
        }

    </style>
</head>
<body>
    <div class="father">
        <div class="son1">son1 box</div>
        <div class="son2">son2 box</div>
    </div>

    <div class="footer"></div>
</body>
</html>
使用after伪元素清除浮动

**:after 方式

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

  • .clearfix {*zoom: 1;} // IE6、7专有

  • 优点: 复合闭合浮动思想,结构语义化正确

  • 缺点:由于IE6-7不支持 :after,使用 zoom:1触发hasLayout

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        .father {
            background-color: cyan;
        }

        .son1 {
            width: 100px;
            height: 100px;
            background-color: green;
            float: left;
        }

        .son2 {
            width: 100px;
            height: 100px;
            background-color: red;
            float: left;
        }

        .footer {
            height: 120px;
            background-color: black;
        }

        /*正常浏览器清除浮动*/
        .clearfix:after {
            content: "";
            display: block;
            height: 0;
            clear: both;
            visibility: hidden;
        }

        /*IE6-7清除浮动方式,*表示ie7以下的版本所识别*/
        .clearfix {
            *zoom: 1;
        }

    </style>
</head>
<body>
    <div class="father clearfix">
        <div class="son1">son1 box</div>
        <div class="son2">son2 box</div>
    </div>

    <div class="footer"></div>
</body>
</html>
使用before和after双伪元素清除浮动
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>

    <style>
        .father {
            background-color: cyan;
        }

        .son1 {
            width: 100px;
            height: 100px;
            background-color: green;
            float: left;
        }

        .son2 {
            width: 100px;
            height: 100px;
            background-color: red;
            float: left;
        }

        .footer {
            height: 120px;
            background-color: black;
        }

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

        .clearfix:after {
            clear: both;
        }

        .clearfix {
            *zoom: 1;
        }
    </style>
</head>
<body>
    <div class="father clearfix">
        <div class="son1">son1 box</div>
        <div class="son2">son2 box</div>
    </div>

    <div class="footer"></div>
</body>
</html>

相关文章

  • CSS浮动续

    CSS清除浮动案例 CSS版心居中显示案例 清除浮动的四种用法: 1. 使用空标记清除浮动,隔墙法,增加标签 2....

  • CSS clear both清除浮动

    原文地址:CSS clear both清除浮动 DIV+CSS clear both清除产生浮动我们知道有时使用了...

  • 清除浮动

    一、清除浮动 or 闭合浮动 ? 清除浮动:清除对应的单词是 clear,对应CSS中的属性是 clear:lef...

  • 一篇文章带你了解CSS clear both清除浮动

    一、前言 CSS clear both清除产生浮动 ,使用了css float浮动会产生css浮动,这个时候就需要...

  • CSS 中的浮动

    浮动的定义: 元素脱离文档流 举栗子: 修改 CSS 代码,清除浮动: 浮动的影响: 父元素高度塌陷 清除浮动: ...

  • CSS浮动.清除浮动

    给父级元素设置高度 在底部添加一个空元素,清除浮动 父级div定义 overflow:hidden或者auto 为...

  • css浮动 清除浮动

    float : left | right | none 设计之初的作用是做文字环绕 p标签段落双标签块级 i...

  • Test10

    引用文章: 那些年我们一起清除过的浮动 CSS浮动float详解 Clear Float CSS float浮动的...

  • 经常写却记不住的前端代码

    CSS透明 清除浮动影响 响应式 css 文字处理

  • css3复习

    清除浮动: 方法:clear清除浮动(添加空div法)在浮动元素下方添加空div,并给该元素写css样式: ...

网友评论

    本文标题:CSS 清除浮动

    本文链接:https://www.haomeiwen.com/subject/qlhkhqtx.html