如何使用 CSS 的"clearfix" hack 清除浮动?
cssweb developmentfront end technology更新于 2025/11/28 8:44:17
要使用 CSS 的"clearfix" hack 清除浮动,代码如下 −
示例
<!DOCTYPE html>
<html>
<head>
<style>
body {
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
div {
border: 3px solid rgb(21, 0, 114);
padding: 5px;
}
img {
float: right;
}
.clearfix {
overflow: auto;
}
</style>
</head>
<body>
<h1>Clearfix hack example</h1>
<h2>Without Clearfix</h2>
<div>
<img
src="https://i.picsum.photos/id/17/300/300.jpg"
width="100"
height="100"/>
Sample Text
</div>
<h2 style="clear:right">With Clearfix</h2>
<div class="clearfix">
<img src="https://i.picsum.photos/id/17/300/300.jpg" width="100" height="100"/>
Sample Text
</div>
</body>
</html>
输出
上述代码将产生以下输出 −

相关文章
有用资源
css 参考教程 - 该教程包含有关 css 的更多信息:https://www.cainiaomax.com/css/

