CSS 规则“clear: both”有什么作用?
我们在 CSS 中使用“float”属性来使元素浮动在左侧或右侧。因此,每当我们为任何 HTML 元素设置“float”属性时,它就会从文档的普通流中移除,有时这可能会导致很多问题。
“clear” CSS 属性允许开发者设置紧跟在浮动元素之后的元素的位置。但是,它总是将下一个元素推到浮动元素的下方。
CSS 的“clear”属性可以有 4 个不同的值:“left”、“right”、“both”和“inherit”。“left”值用于将下一个元素推到左侧浮动元素的下方。“right”值用于将下一个元素推到右侧浮动元素的下方。“both”值用于将下一个元素推到左侧和右侧浮动元素的下方。
在本教程中,我们将讨论“clear” CSS 属性的“both”值,并提供一些示例。
语法
用户可以按照以下语法使用“clear: both” CSS 规则。
Css_selector {
Clear: both;
}
在上述语法中,CSS_selector 用于选择位于浮动元素之后的元素。
示例 1
在下面的示例中,“parent” div 元素包含一个“child”元素和一个“text”元素。在这里,我们为父元素设置了固定尺寸。之后,我们也为子元素设置了固定尺寸和“float: left”属性。
我们对“text”元素使用了“clear: both” CSS 属性。在输出中,用户可以看到文本内容位于下方,而不是浮动在子 div 元素的右侧。
<html>
<style>
.parent {
width: 500px;
height: 300px;
background-color: green;
border-radius: 12px;
border: 3px solid orange;
}
.child1 {
width: 200px;
height: 200px;
background-color: red;
border-radius: 12px;
border: 2px dotted pink;
margin: 10px;
float: left;
}
.text {
font-size: 1.2rem;
clear: both;
color: white;
height: 20px;
}
</style>
<body>
<h3> Using the <i> clear: both </i> CSS property to push the next element at below of left-floated element </h3>
<div class = "parent">
<div class = "child1"> </div>
<p class = "text"> Hey! How are you? Are you fine? </p>
</div>
</body>
</html>
示例 2
在下面的示例中,父元素包含多个“child”元素。它还在多个“child”元素之间包含一个“clear”div 元素。我们为每个子 div 元素设置了“float: right” CSS 属性。
此外,我们为“clear”div 元素设置了“clear: both” CSS 属性。在输出中,用户可以看到所有 div 元素都浮动到右侧。此外,由于“clear”div 位于子 div 元素的中间,“clear: both” CSS 属性将所有紧挨着它的子元素显示在“clear”div 的下方。
<html>
<style>
.parent {
width: 600px;
height: 300px;
background-color: blue;
border-radius: 12px;
border: 3px solid orange;
}
.child {
width: 100px;
height: 100px;
border: 2px dotted pink;
margin: 10px;
background-color: white;
border-radius: 12px;
float: right;
}
.clear { clear: both;}
</style>
<body>
<h3> Using the <i> clear: both </i> CSS property to push the next element below the left-floated element </h3>
<div class = "parent">
<div class = "child"> </div>
<div class = "child"> </div>
<div class = "child"> </div>
<div class = "clear"> </div>
<div class = "child"> </div>
<div class = "child"> </div>
</div>
</body>
</html>
示例 3
在下面的示例中,我们创建了 main div 元素。“left” div 元素在容器中向左浮动,“right” div 元素在容器中向右浮动。
在输出中,用户可以使用单选按钮为“clear”属性设置“both”或“none”值。用户可以选择不同的单选按钮并观察输出。当我们选择“both”时,它将中间的 div 显示在两个浮动 div 元素的下方。
<html>
<head>
<style>
.main {
width: 600px;
height: 200px;
background-color: pink;
border-radius: 12px;
border: 3px solid orange;
margin-bottom: 20px;
}
.right,
.left {
width: 200px;
height: 120px;
background-color: aqua;
}
.right {float: right;}
.left {float: left;}
.center {
width: auto;
height: auto;
font-size: 2rem;
background-color: yellow;
color: blue;
clear: both;
}
</style>
</head>
<body>
<h3> Using the <i> clear: both </i> CSS property to push the next element at below of left-floated element </h3>
<div class = "main">
<div class = "left"> </div>
<div class = "right"> </div>
<div class = "center"> This is a div in the center of the left and right div element. </div>
</div>
<input type = "radio" name = "clear" id = "both" value = "both" checked /> both
<input type = "radio" name = "clear" id = "none" value = "none" /> none
<script>
let allRadio = document.getElementsByName('clear');
console.log(allRadio)
for (let i = 0; i < allRadio.length; i++) {
allRadio[i].addEventListener('click', () => {
console.log(allRadio[i].value)
let centerDiv = document.getElementsByClassName('center');
centerDiv[0].style.clear = allRadio[i].value;
})
}
</script>
</body>
</html>
我们已经看到了“clear: both” CSS 属性的各种用例。我们使用“clear”属性将下一个元素显示在浮动元素的下方。每当开发者不确定浮动元素的下一个元素的尺寸时,都应该使用“clear”CSS 属性;否则,HTML 元素可能会溢出,看起来很奇怪。
但是,用户可以使用“overflow” CSS 属性来解决使用“float”属性时发生的溢出问题。
数据结构
网络
关系数据库管理系统 (RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP