美文网首页
清除浮动

清除浮动

作者: xing222333 | 来源:发表于2019-03-22 21:34 被阅读0次

方法1:在浮动元素后面增加空白元素用上clear:both

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>清除浮动</title>
    <style>
        .box {
            background-color: #2AB561;
            border: 1px solid red;
        }

        .one {
            width: 100px;
            height: 50px;
            background-color: #0DA5D6;
            float: left;
        }

        .two {
            float: left;
            width: 100px;
            height: 50px;
            background-color: #1b6d85;
        }

        .three {
            width: 200px;
            height: 100px;
            background-color: #0f0f0f;
        }
        .four{
            clear: both;
        }

    </style>
</head>
<body>
<div class="box">
    <div class="one"></div>
    <div class="two"></div>
    <div class="four"></div>
</div>
<div class="three"></div>
</body>
</html>

方法2:父级添加overflow:hidden

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>清除浮动</title>
    <style>
        .box {
            background-color: #2AB561;
            border: 1px solid red;
            /*overflow触发BFC,BFC可以清除浮动*/
            overflow: hidden; 
        }

        .one {
            width: 100px;
            height: 50px;
            background-color: #0DA5D6;
            float: left;
        }

        .two {
            float: left;
            width: 100px;
            height: 50px;
            background-color: #1b6d85;
        }

        .three {
            width: 200px;
            height: 100px;
            background-color: #0f0f0f;
        }


    </style>
</head>
<body>
<div class="box">
    <div class="one"></div>
    <div class="two"></div>
</div>
<div class="three"></div>
</body>
</html>

方法3:添加伪元素

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>清除浮动</title>
    <style>
        .box {
            background-color: #2AB561;
            border: 1px solid red;
        }

        .one {
            width: 100px;
            height: 50px;
            background-color: #0DA5D6;
            float: left;
        }

        .two {
            float: left;
            width: 100px;
            height: 50px;
            background-color: #1b6d85;
        }

        .three {
            width: 200px;
            height: 100px;
            background-color: #0f0f0f;
        }

        .clearFloat:after {
            /*避免有空隙*/
            content: '.';
            /*变成块级元素*/
            display: block;
            /*隐藏content*/
            visibility: hidden;
            clear: both;
            /*清除盒子高度*/
            height: 0;

        }

        .clearFloat {
            /*兼容IE6,7*/
            *zoom: 1;
        }


    </style>
</head>
<body>
<div class="box clearFloat">
    <div class="one"></div>
    <div class="two"></div>
</div>
<div class="three"></div>
</body>
</html>

方法4:双伪元素清除浮动

<!doctype html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>清除浮动</title>
    <style>
        .box {
            background-color: #2AB561;
            border: 1px solid red;
        }

        .one {
            width: 100px;
            height: 50px;
            background-color: #0DA5D6;
            float: left;
        }

        .two {
            float: left;
            width: 100px;
            height: 50px;
            background-color: #1b6d85;
        }

        .three {
            width: 200px;
            height: 100px;
            background-color: #0f0f0f;
        }

        .clearFloat:before ,.clearFloat:after {
            /*避免有空隙*/
            content: '';
            /*变成块级元素*/
            display: table;
        }
        .clearFloat:after{
            clear: both;
        }

        .clearFloat {
            /*兼容IE6,7*/
            *zoom: 1;
        }


    </style>
</head>
<body>
<div class="box clearFloat">
    <div class="one"></div>
    <div class="two"></div>
</div>
<div class="three"></div>
</body>
</html>

相关文章

  • 11.22 前端学习

    清除浮动 clear:left清除左浮动clear:right清除右浮动clear:both清除对它影响最大的浮动...

  • 前端06

    清除浮动 clear:left清除左浮动clear:right清除右浮动clear:both清除对它影响最大的浮动...

  • 06 前端学习

    清除浮动 clear:left清除左浮动clear:right清除右浮动clear:both清除对它影响最大的浮动...

  • 清除浮动

    未清除浮动前 清除浮动后

  • 技术知识点整理

    清除浮动 BFC清除浮动浮动的父级末尾插入块级元素清除浮动 BFC(Block Formatting Contex...

  • H5前端开发学习笔记——0x15清除浮动

    清除浮动 课时130 浮动元素高度问题(掌握) 课时131 清除浮动方式一(理解) 课时132 清除浮动方式二(理...

  • css清除浮动

    前端开发中浮动处处可见,本文探讨浮动的成因以及如何更加有效的清除浮动。 1、浮动与清除浮动 2、清除浮动 基本cs...

  • CSS清除浮动三种方式

    清除浮动 当父盒子没有定义高度,嵌套的盒子浮动之后,下边的元素发生位置错误。 清除浮动不是不用浮动,清除浮动产生的...

  • 布局浮动的问题

    浮动的问题 什么是浮动?浮动(float)的副作用清除浮动两种清除浮动的办法如下:

  • 清除浮动

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

网友评论

      本文标题:清除浮动

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