欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

html中清除浮动(float)带来的影响

程序员文章站 2022-03-29 17:09:10
...

在要清除浮动的标签中添加清除浮动的方法:

示例代码:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <style>
        .cleanfix :after{  //清除浮动带来的影响
            content: "";
            display: table;
            clear: both;
        }
        #left,#left2,#center,#right{
            float: left;
        }
        #top1,#top2{
            float: left;
        }
        #left{
            width: 20%;
            height: 300px;
            background-color: red;
        }
        #left2{
            width: 20%;
            height: 300px;
            background-color: #91DB4B;
        }
        #left21{
            width: 100%;
            height: 50%;
            background-color: #F2AD0A;
        }
        #left22{
            width: 100%;
            height: 50%;
            background-color: #42668F;
        }
        #center{
            width: 20%;
            height: 300px;
            background-color: #F2BB6F;
        }
        #right{
            width: 40%;
            height: 300px;
            background-color: aqua;
        }
        #top{
            width: 100%;
            height: 50%;
            background-color: #FF6600;
        }
        #bottom{
            width: 100%;
            height: 50%;
            background-color: chartreuse;
        }
        #top1{
            width: 50%;
            height: 100%;
            background-color: blue;
        }
        #top2{
            width: 50%;
            height: 100%;
            background-color: blanchedalmond;
        }
    </style>
    <title>页面作业</title>
</head>
<body>
<div class="cleanfix" ><!--清除浮动-->
    <div id="left"></div>
    <div id="left2">
        <div id="left21"></div>
        <div id="left22"></div>
    </div>
    <div id="center"></div>
    <div id="right">
        <div id="top" class="cleanfix">
            <div id="top1"></div>
            <div id="top2"></div>
        </div>
        <div id="bottom"></div>
    </div>
</div>

</body>
</html>