<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>清除浮动脱标影响</title>
<style>
.parent {
margin: 0 auto;
width: 400px;
/* 方法四:设置overflow属性 */
overflow: hidden;
}
.childFir {
float: left;
width: 200px;
height: 200px;
background-color: green;
}
.childSec {
float: right;
width: 200px;
height: 200px;
background-color: greenyellow;
}
.testBox {
height: 100px;
background-color: purple;
}
/* 方法三:设置单伪元素
.clearfix::after {
content: "";
display: block;
clear: both;
} */
/* 方法二:设置双伪元素
.clearfix::before,
.clearfix::after {
content: "";
display: table;
}
.clearfix::after {
clear: both;
} */
</style>
</head>
<body>
<div class="parent clearfix">
<div class="childFir"></div>
<div class="childSec"></div>
<!-- 方法一:另加块级标签并设置clear属性
<div style="clear: both;"></div> -->
</div>
<div class="testBox"></div>
</body>
</html>
CSS清除浮动(float)脱标影响
于 2025-08-06 15:52:32 首次发布
1915

被折叠的 条评论
为什么被折叠?



