关于清除浮动的理解clear:both,clear:left

465 阅读4分钟
1. clear
图像的左侧和右侧均不允许出现浮动元素
clear 属性规定元素的哪一侧不允许其他浮动元素。
clear 属性定义了元素的哪边上不允许出现浮动元素。在 CSS1 和 CSS2 中,这是通过自动为清除元素(即设置了 clear 属性的元素)增加上外边距实现的。在 CSS2.1 中,会在元素上外边距之上增加清除空间,而外边距本身并不改变。不论哪一种改变,最终结果都一样,如果声明为左边或右边清除,会使元素的上外边框边界刚好在该边上浮动元素的下外边距边界之下。
  • 上面这个是w3school对clear这一个属性的说明。
  • 但是我觉得有很大的歧异,下面我用案例来深入理解一下clear这个属性。
2. clear:left 的元素左侧不允许有浮动

[AppleScript]
纯文本查看
复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
<!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>Document</title>
<style>
div {
width: 100px;
height: 100px;
background-color: #66ccff;
float: left;
}
.box1 {
background-color: #66ccff;
}
.box2 {
background-color: #0a0;
}
.box3 {
background-color: #f00;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>

那么现在

我们有三个左浮动的盒子,如果想让第二个盒子在新的一排显示,那么就要用到我们的
clear: left
不允许该元素左侧有任何的浮动
只需要在
.box2
上加上
clear:left
便可以实现效果

[AppleScript]
纯文本查看
复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
<!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>Document</title>
<style>
div {
width: 100px;
height: 100px;
background-color: #66ccff;
float: left;
}
.box1 {
background-color: #66ccff;
}
.box2 {
background-color: #0a0;
clear: left;
}
.box3 {
background-color: #f00;
}
</style>
</head>
<body>
<div class="box1"></div>
<div class="box2"></div>
<div class="box3"></div>
</body>
</html>

那么,我们想要让第二个绿色的盒子的左右两侧都不允许有浮动元素,也就是让三个盒子类似于标准流的显示方式,我们使用clear:both就可以了么?

3. clear: both 无法清除当前元素右侧的浮动对象
clear 无论在何种情况下(clear:left,clear:right,clear:both)都只能让作用了clear属性的标签的前面的标签起到效果。
非常疑惑,那岂不是只能清除掉左侧的浮动对象咯,clear:both和clear:right有什么用咯?
我们还是拿刚才的案例,不过不同的是,我们让所有的div右浮动
现在!!!!!!!!!!
我们让第二个盒子的右侧不允许有浮动的元素出现,我们就要用到clear:right或者clear:both了
[AppleScript]
纯文本查看
复制代码
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
<!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>Document</title>
<style>
.box {
width: 400px;
height: 300px;
border: 1px solid #000;
}
.box div {
width: 100px;
height: 100px;
float: right;
font-size: 50px;
line-height: 100px;
text-align: center;
color: aliceblue;
border: 1px solid #000;
}
.box1 {
background-color: #66ccff;
}
.box2 {
background-color: #0a0;
clear: both;
/*或者 clear:right*/
}
.box3 {
background-color: #f00;
}
</style>
</head>
<body>
<div class="box">
<div class="box1">1</div>
<div class="box2">2</div>
<div class="box3">3</div>
</div>
</body>
</html>
4. 总结
  • 无论是both,还是left,还是right。clear始终无法处理他后面的元素,你要问为啥不行啊 ,我就要清掉,那我想和你说,你左右都清掉何必还用浮动哇。。。。