浮动元素不会增加容器元素的高度,因此如果你不清除它们,容器高度不会增加......
我会直观地告诉你:
更多解释:
<div>
<div style="float: left;"></div>
<div style="width: 15px;"></div> <!-- This will shift
besides the top div. Why? Because of the top div
is floated left, making the
rest of the space blank -->
<div style="clear: both;"></div>
<!-- Now in order to prevent the next div from floating beside the top ones,
we use `clear: both;`. This is like a wall, so now none of the div's
will be floated after this point. The container height will now also include the
height of these floated divs -->
<div></div>
</div>
您也可以在容器元素上添加overflow: hidden;,但我建议您改用clear: both;。
此外,如果您想自行清除可以使用的元素
.self_clear:after {
content: "";
clear: both;
display: table;
}
CSS 浮动是如何工作的?
什么是浮点数,它有什么作用?
-
float 属性被大多数初学者误解。那么,float 到底是做什么的?最初,float 属性被引入以围绕图像流动文本,这些图像是浮动的left 或right。 Here's another explanation@Madara Uchicha。
那么,使用float 属性来并排放置盒子是错误的吗?答案是否;如果您使用float 属性来并排设置框,则没有问题。
-
浮动inline 或block 级别元素将使该元素的行为类似于inline-block 元素。
Demo
1234563 /li>
-
你不能 float 一个元素 center。这是我在初学者中经常遇到的最大问题,使用 float: center;,它不是 float 属性的有效值。 float 通常用于float/将内容移动到非常left 或非常right。 float 属性只有 四个 有效值,即 left、right、none(默认)和 inherit。
-
父元素折叠,当它包含浮动子元素时,为了防止这种情况,我们使用clear: both;属性,清除两边的浮动元素,这样可以防止父元素折叠。更多信息可以参考我的另一个答案here。
(重要) 想想我们有一堆各种元素的地方。当我们使用float: left; 或float: right; 时,元素在堆栈上方移动一个。因此,普通文档流中的元素将隐藏在浮动元素后面,因为它位于普通浮动元素之上的堆栈级别。 (请不要将此与z-index 联系起来,因为那完全不同。)
以一个例子来解释 CSS 浮动是如何工作的,假设我们需要一个简单的 2 列布局,带有页眉、页脚和 2 列,那么蓝图是这样的......
在上面的示例中,我们将只浮动红色框,您可以将float 都浮动到left,或者您可以将float 浮动到left,另一个也浮动到right , 取决于布局,如果它是 3 列,您可以 float 2 列到 left 另一列到 right 所以取决于,尽管在这个例子中,我们有一个简化的 2 列布局,所以 float一个给left,另一个给right。
用于创建布局的标记和样式进一步解释...
<div class="main_wrap">
<header>Header</header>
<div class="wrapper clear">
<div class="floated_left">
This<br />
is<br />
just<br />
a<br />
left<br />
floated<br />
column<br />
</div>
<div class="floated_right">
This<br />
is<br />
just<br />
a<br />
right<br />
floated<br />
column<br />
</div>
</div>
<footer>Footer</footer>
</div>
* {
-moz-box-sizing: border-box; /* Just for demo purpose */
-webkkit-box-sizing: border-box; /* Just for demo purpose */
box-sizing: border-box; /* Just for demo purpose */
margin: 0;
padding: 0;
}
.main_wrap {
margin: 20px;
border: 3px solid black;
width: 520px;
}
header, footer {
height: 50px;
border: 3px solid silver;
text-align: center;
line-height: 50px;
}
.wrapper {
border: 3px solid green;
}
.floated_left {
float: left;
width: 200px;
border: 3px solid red;
}
.floated_right {
float: right;
width: 300px;
border: 3px solid red;
}
.clear:after {
clear: both;
content: "";
display: table;
}
让我们一步一步来布局,看看浮动是如何工作的..
首先,我们使用主包装元素,您可以假设它是您的视口,然后我们使用header 并分配height 的50px,所以没什么特别的。它只是一个普通的非浮动块级元素,它将占用100% 水平空间,除非它是浮动的或者我们将inline-block 分配给它。
float 的第一个有效值是left,因此在我们的示例中,我们使用float: left; 来表示.floated_left,因此我们打算将一个块浮动到容器元素的left。
Column floated to the left
是的,如果您看到,父元素 .wrapper 已折叠,您看到的带有绿色边框的元素没有展开,但它应该是对的?待会再讨论,目前,我们有一个浮动到 left 的列。
来到第二列,让它float这个right
Another column floated to the right
在这里,我们有一个300px 宽列,我们将其float 连接到right,当它浮动到left 时,它将位于第一列旁边,因为它浮动到left,它为right 创建了空槽,并且由于right 上有足够的空间,我们的right 浮动元素完美地位于left 旁边。
尽管如此,父元素还是折叠了,好吧,现在让我们修复它。有很多方法可以防止父元素折叠。
- 添加一个空的块级元素并在父元素结束之前使用
clear: both;,它包含浮动元素,现在这个是clear 浮动元素的廉价解决方案,它将为您完成这项工作,但我会建议不要使用这个。
在.wrapper div 结束前添加<div style="clear: both;"></div>,就像
<div class="wrapper clear">
<!-- Floated columns -->
<div style="clear: both;"></div>
</div>
Demo
好吧,这解决得很好,不再有折叠的父级,但它向 DOM 添加了不必要的标记,因此有人建议,在包含浮动子元素的父元素上使用 overflow: hidden;,这可以按预期工作。
在.wrapper 上使用overflow: hidden;
.wrapper {
border: 3px solid green;
overflow: hidden;
}
Demo
每次我们需要 clear float 时,这为我们节省了一个元素,但是当我用它测试各种案例时,它在一个特定的案例中失败了,它在子元素上使用了 box-shadow。
Demo(无法看到所有 4 个侧面的阴影,overflow: hidden; 导致此问题)
那么现在呢?保存一个元素,没有overflow: hidden; 所以去明确的修复黑客,在你的CSS中使用下面的sn-p,就像你对父元素使用overflow: hidden;一样,在父元素上调用class自我清除。
.clear:after {
clear: both;
content: "";
display: table;
}
<div class="wrapper clear">
<!-- Floated Elements -->
</div>
Demo
在这里,阴影按预期工作,而且它会自行清除防止折叠的父元素。
最后,我们在clear 浮动元素之后使用页脚。
Demo
什么时候使用float: none;,因为它是默认值,所以要声明float: none;吗?
好吧,这取决于,如果您要进行响应式设计,您将多次使用此值,当您希望浮动元素以特定分辨率呈现在另一个下方时。因为float: none; 属性在那里起着重要作用。
float 有用的真实示例很少。
- 我们已经看到的第一个示例是创建一个或多个列布局。
- 使用
img 浮动在 p 中,这将使我们的内容能够四处流动。
Demo(不浮动img)
Demo 2(img 浮动到left)
也可以浮动第二个元素,或者使用 `margin`
最后但并非最不重要的一点是,我想解释一下这种特殊情况,您 float 仅将单个元素传递给 left 但您没有 float 另一个,那么会发生什么?
假设如果我们从.floated_right class 中删除float: right;,则div 将从极端left 呈现,因为它没有浮动。
Demo
所以在这种情况下,你可以float the to the left as well
或
你可以use margin-left which will be equal to the size of the left floated column i.e 200px wide。