本文翻译自:What does the CSS rule “clear: both” do?
What does the following CSS rule do: 以下CSS规则有什么作用:
.clear { clear: both; }
And why do we need to use it? 为什么我们需要使用它?
#1楼
参考:https://stackoom.com/question/S0we/CSS规则-清除-两者-有什么作用
#2楼
When you want one element placed at the bottom other element you use this code in CSS. 当您希望一个元素位于其他元素的底部时,可以在CSS中使用此代码。 It is used for floats. 它用于浮子。
If you float content you can float left or right... so in a common layout you might have a left nav, a content div and a footer. 如果您浮动内容,则可以向左或向右浮动...因此,在常见布局中,您可能会有一个左导航,一个内容div和一个页脚。
To ensure the footer stays below both of these floats (if you have floated left and right) then you put the footer as clear: both
. 为了确保页脚停留在这两个浮点的下方(如果您左右浮动),则将页脚设置为clear: both
。
This way it will stay below both floats. 这样,它将保持在两个浮点以下。
(If you are only clearing left then you only really need to clear: left;
.) (如果您只需要清除左,那么您只需要clear: left;
)
Go through this tutorial: 完成本教程:
#3楼
I won't be explaining how the floats work here (in detail), as this question generally focuses on Why use clear: both;
我不会在这里详细解释浮点数的工作原理,因为这个问题通常集中在为什么要使用clear: both;
OR what does clear: both;
或clear: both;
exactly do... 确实是...
I'll keep this answer simple, and to the point, and will explain to you graphically why clear: both;
我将使答案简单明了,并以图形方式向您说明为什么clear: both;
is required or what it does... 是必需的或其作用...
Generally designers float the elements, left or to the right, which creates an empty space on the other side which allows other elements to take up the remaining space. 通常,设计人员将元素向左或向右浮动,从而在另一侧创建一个空白空间,该空间允许其他元素占用剩余空间。
Why do they float elements? 为什么它们浮动元素?
Elements are floated when the designer needs 2 block level elements side by side. 当设计人员并排需要2个块级元素时,元素将浮动。 For example say we want to design a basic website which has a layout like below... 例如说我们要设计一个基本的网站,其布局如下图所示。
Live Example of the demo image. 演示图像的实时示例 。
Code For Demo 演示代码
/* CSS: */ * { /* Not related to floats / clear both, used it for demo purpose only */ box-sizing: border-box; -moz-box-sizing: border-box; -webkit-box-sizing: border-box; } header, footer { border: 5px solid #000; height: 100px; } aside { float: left; width: 30%; border: 5px solid #000; height: 300px; } section { float: left; width: 70%; border: 5px solid #000; height: 300px; } .clear { clear: both; }
<!-- HTML --> <header> Header </header> <aside> Aside (Floated Left) </aside> <section> Content (Floated Left, Can Be Floated To Right As Well) </section> <!-- Clearing Floating Elements--> <div class="clear"></div> <footer> Footer </footer>
Note: You might have to add header
, footer
, aside
, section
(and other HTML5 elements) as display: block;
注意: 您可能必须将header
, footer
, aside
, section
(和其他HTML5元素)添加为display: block;
in your stylesheet for explicitly mentioning that the elements are block level elements. 在样式表中明确提到元素是块级元素。
Explanation: 说明:
I have a basic layout, 1 header, 1 side bar, 1 content area and 1 footer. 我有一个基本布局,1个标题,1个侧边栏,1个内容区域和1个页脚。
No floats for header
, next comes the aside
tag which I'll be using for my website sidebar, so I'll be floating the element to left. header
没有浮动,接下来是我将用于网站侧边栏的aside
标签,因此我将元素浮动到左侧。
Note: By default, block level element takes up document 100% width, but when floated left or right, it will resize according to the content it holds. 注意:默认情况下,块级元素占用文档100%的宽度,但是向左或向右浮动时,它将根据其持有的内容调整大小。
So as you note, the left floated div
leaves the space to its right unused, which will allow the div
after it to shift in the remaining space. 因此,正如您所注意到的,左侧浮动div
保留了未使用其右侧的空间,这将使div
之后的div
移入剩余空间。
-
div
's will render one after the other if they are NOT floateddiv
没有浮动,它们将一个接一个地渲染 -
div
will shift beside each other if floated left or right 如果向左或向右浮动,div
将彼此并排移动
Ok, so this is how block level elements behave when floated left or right, so now why is clear: both;
好的,这就是块级元素向左或向右浮动时的行为,所以现在很clear: both;
required and why? 必需,为什么?
So if you note in the layout demo - in case you forgot, here it is.. 因此,如果您在布局演示中注明-如果您忘记了,就在这里 。
I am using a class called .clear
and it holds a property called clear
with a value of both
. 我正在使用一个名为.clear
的类,它包含一个名为clear
的属性,且both
值。 So lets see why it needs both
. 因此,让我们看看为什么both
需要。
I've floated aside
and section
elements to the left, so assume a scenario, where we have a pool, where header
is solid land, aside
and section
are floating in the pool and footer is solid land again, something like this.. 我已经在左侧浮动了aside
和section
元素,因此假设有一个场景,我们有一个池,其中header
是固定土地, aside
和section
在池中浮动,而页脚又是固定土地,类似这样。
So the blue water has no idea what the area of the floated elements are, they can be bigger than the pool or smaller, so here comes a common issue which troubles 90% of CSS beginners: why the background of a container element is not stretched when it holds floated elements. 因此,蓝色的水不知道浮动元素的面积是多少,它们可以大于池的面积,也可以小于池的面积,因此出现了一个使90%的CSS初学者感到困扰的常见问题:为什么容器元素的背景没有拉伸当它包含浮动元素时。 It's because the container element is a POOL here and the POOL has no idea how many objects are floating, or what the length or breadth of the floated elements are, so it simply won't stretch. 这是因为容器元素在这里是一个POOL ,而POOL不知道有多少个对象在浮动,或者浮动元素的长度或宽度是多少,因此它根本不会拉伸。
- Normal Flow Of The Document 文件的正常流动
- Sections Floated To Left 剖面向左浮动
- Cleared Floated Elements To Stretch Background Color Of The Container 清除浮动元素以拉伸容器的背景色
(Refer [Clearfix] section of this answer for neat way to do this. I am using an empty div
example intentionally for explanation purpose) (请参阅此答案的[Clearfix]部分,以获取整洁的方法。我有意使用一个空的div
示例进行说明)
I've provided 3 examples above, 1st is the normal document flow where red
background will just render as expected since the container doesn't hold any floated objects. 我在上面提供了3个示例,第一个是普通文档流,其中red
背景将按预期显示,因为容器不包含任何浮动对象。
In the second example, when the object is floated to left, the container element (POOL) won't know the dimensions of the floated elements and hence it won't stretch to the floated elements height. 在第二个示例中,当对象向左浮动时,容器元素(POOL)将不知道浮动元素的尺寸,因此不会拉伸到浮动元素的高度。
After using clear: both;
使用完后clear: both;
, the container element will be stretched to its floated element dimensions. ,则容器元素将被拉伸到其浮动元素的尺寸。
Another reason the clear: both;
另一个原因clear: both;
is used is to prevent the element to shift up in the remaining space. 用于防止元素在剩余空间中向上移动。
Say you want 2 elements side by side and another element below them... So you will float 2 elements to left and you want the other below them. 假设您要并排放置2个元素,并在其下面放置另一个元素...因此,您需要将2个元素向左浮动,然后将另一个元素放置在它们下面。
-
div
Floated left resulting insection
moving into remaining spacediv
向左浮动,导致section
移至剩余空间 - Floated
div
cleared so that thesection
tag will render below the floateddiv
s 已清除浮动div
以便section
标签将在浮动div
下方呈现
1st Example 第一个例子
2nd Example 第二个例子
Last but not the least, the footer
tag will be rendered after floated elements as I've used the clear
class before declaring my footer
tags, which ensures that all the floated elements (left/right) are cleared up to that point. 最后但并非最不重要的一点是,在声明footer
标签之前,由于我使用clear
类,所以footer
标签将在浮动元素之后呈现,这可以确保清除所有浮动元素(向左/向右)。
Clearfix Clearfix
Coming to clearfix which is related to floats. 来到与浮点数有关的clearfix。 As already specified by @Elky , the way we are clearing these floats is not a clean way to do it as we are using an empty div
element which is not a div
element is meant for. 正如@Elky已经指定的那样 ,清除这些浮点数的方法并不是一种干净的方法,因为我们正在使用一个空的div
元素,而这不是div
元素的目的。 Hence here comes the clearfix. 因此,这里出现了clearfix。
Think of it as a virtual element which will create an empty element for you before your parent element ends. 将其视为虚拟元素,它将在父元素结束之前为您创建一个空元素。 This will self clear your wrapper element holding floated elements. 这将自动清除包含浮动元素的包装器元素。 This element won't exist in your DOM literally but will do the job. 该元素实际上不会在您的DOM中存在,但可以完成工作。
To self clear any wrapper element having floated elements, we can use 为了自清除任何具有浮动元素的包装器元素,我们可以使用
.wrapper_having_floated_elements:after { /* Imaginary class name */
content: "";
clear: both;
display: table;
}
Note the :after
pseudo element used by me for that class
. 注意我在class
使用的:after
伪元素。 That will create a virtual element for the wrapper element just before it closes itself. 这将在包装器元素自身关闭之前为其创建一个虚拟元素。 If we look in the dom you can see how it shows up in the Document tree. 如果我们查看dom,您会看到它在Document树中的显示方式。
So if you see, it is rendered after the floated child div
where we clear the floats which is nothing but equivalent to have an empty div
element with clear: both;
因此,如果看到的话,它将在浮动子div
之后呈现,我们在其中清除了浮点数,这只不过等于具有带clear: both;
的空div
元素clear: both;
property which we are using for this too. 我们也为此使用的属性。 Now why display: table;
现在为什么display: table;
and content
is out of this answers scope but you can learn more about pseudo element here . 并且content
超出了答案的范围,但是您可以在这里了解更多的伪元素 。
Note that this will also work in IE8 as IE8 supports :after
pseudo . 注意,这也将在IE8中工作,因为IE8支持:after
伪 。
Original Answer: 原始答案:
Most of the developers float their content left or right on their pages, probably divs holding logo, sidebar, content etc., these divs are floated left or right, leaving the rest of the space unused and hence if you place other containers, it will float too in the remaining space, so in order to prevent that clear: both;
大多数开发人员在其页面上左右浮动内容,可能是带有徽标,边栏,内容等的div,这些div则是左右浮动,剩下的空间未使用,因此,如果您放置其他容器,它将漂浮在剩余的空间中,所以为了防止这种情况的发生clear: both;
is used, it clears all the elements floated left or right. 使用时,它会清除所有向左或向右浮动的元素。
Demonstration: 示范:
------------------ ----------------------------------
div1(Floated Left) Other div takes up the space here
------------------ ----------------------------------
Now what if you want to make the other div render below div1
, so you'll use clear: both;
现在,如果要使另一个div呈现在div1
以下,该怎么办,将使用clear: both;
so it will ensure you clear all floats, left or right 这样可以确保您清除所有左侧或右侧的浮标
------------------
div1(Floated Left)
------------------
<div style="clear: both;"><!--This <div> acts as a separator--></div>
----------------------------------
Other div renders here now
----------------------------------
#4楼
Just try to remove clear:both
property from the div
with class
sample
and see how it follows floating divs
. 只需尝试使用class
sample
从div
清除clear:both
属性,然后查看它如何跟随divs
浮动。
#5楼
Mr. Alien's answer is perfect, but anyway I don't recommend to use <div class="clear"></div>
because it just a hack which makes your markup dirty. Alien先生的回答是完美的,但是无论如何我都不建议使用<div class="clear"></div>
因为它只是一种使标记变脏的hack。 This is useless empty div
in terms of bad structure and semantic, this also makes your code not flexible. 就不良的结构和语义而言,这是没有用的空div
,这也使您的代码不灵活。 In some browsers this div causes additional height and you have to add height: 0;
在某些浏览器中,此div会导致额外的高度,您必须添加height: 0;
which even worse. 更糟的是。 But real troubles begin when you want to add background or border around your floated elements - it just will collapse because web was designed badly . 但是,当您想在浮动元素周围添加背景或边框时,真正的麻烦就开始了,因为网络设计不当,它只会崩溃。 I do recommend to wrap floated elements into container which has clearfix CSS rule. 我建议将浮动元素包装到具有clearfix CSS规则的容器中。 This is hack as well, but beautiful, more flexible to use and readable for human and SEO robots. 这也是一种技巧,但它美观,易于使用且对人类和SEO机器人可读。
#6楼
The clear
property indicates that the left, right or both sides of an element can not be adjacent to earlier floated elements within the same block formatting context. clear
属性指示在同一块格式上下文中,元素的左侧,右侧或两侧不能与更早的浮动元素相邻。 Cleared elements are pushed below the corresponding floated elements. 清除的元素被推到相应的浮动元素下方。 Examples: 例子:
clear: none;
Element remains adjacent to floated elements 元素保持与浮动元素相邻
body { font-family: monospace; background: #EEE; } .float-left { float: left; width: 60px; height: 60px; background: #CEF; } .float-right { float: right; width: 60px; height: 60px; background: #CEF; } .clear-none { clear: none; background: #FFF; }
<div class="float-left">float: left;</div> <div class="float-right">float: right;</div> <div class="clear-none">clear: none;</div>
clear: left;
Element pushed below left floated elements 元素被推到左侧浮动元素下方
body { font-family: monospace; background: #EEE; } .float-left { float: left; width: 60px; height: 60px; background: #CEF; } .float-right { float: right; width: 60px; height: 120px; background: #CEF; } .clear-left { clear: left; background: #FFF; }
<div class="float-left">float: left;</div> <div class="float-right">float: right;</div> <div class="clear-left">clear: left;</div>
clear: right;
Element pushed below right floated elements 元素被推到右浮动元素下方
body { font-family: monospace; background: #EEE; } .float-left { float: left; width: 60px; height: 120px; background: #CEF; } .float-right { float: right; width: 60px; height: 60px; background: #CEF; } .clear-right { clear: right; background: #FFF; }
<div class="float-left">float: left;</div> <div class="float-right">float: right;</div> <div class="clear-right">clear: right;</div>
clear: both;
Element pushed below all floated elements 元素被推到所有浮动元素的下方
body { font-family: monospace; background: #EEE; } .float-left { float: left; width: 60px; height: 60px; background: #CEF; } .float-right { float: right; width: 60px; height: 60px; background: #CEF; } .clear-both { clear: both; background: #FFF; }
<div class="float-left">float: left;</div> <div class="float-right">float: right;</div> <div class="clear-both">clear: both;</div>
clear
does not affect floats outside the current block formatting context clear
不会影响当前块格式上下文之外的浮点数
body { font-family: monospace; background: #EEE; } .float-left { float: left; width: 60px; height: 120px; background: #CEF; } .inline-block { display: inline-block; background: #BDF; } .inline-block .float-left { height: 60px; } .clear-both { clear: both; background: #FFF; }
<div class="float-left">float: left;</div> <div class="inline-block"> <div>display: inline-block;</div> <div class="float-left">float: left;</div> <div class="clear-both">clear: both;</div> </div>