1、首先给浮动的元素加一个父容器

2、给这个父容器添加一个样式 clearfix

clearfix的设置如下:

.clearfix::after{
        content: "";
        display: block;
        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>清除浮动</title>
    <style>
        .clearfix::after{
            content: "";
            display: block;
            clear: both;
        }
        .float{
            float: left;
            width: 200px;
            height: 200px;
        }
    </style>
</head>
<body>
    <div class="clearfix">
        <div class="float" style="background-color: pink;"></div>
        <div class="float" style="background-color: silver;"></div>
    </div>
    <div style="width:500px;height: 300px;background-color: orange;">

    </div>
</body>
</html>

效果:

Logo

权威|前沿|技术|干货|国内首个API全生命周期开发者社区

更多推荐