css-float - 为什么 `inline-block` 元素会自动清除他们的 child ?

标签 css-float css

display: inline-block 用于许多 clearfixes(12),尽管这些文章都没有解释为什么它使元素在其自身之后清晰。为什么会这样?

我仔细阅读了规范,但没有找到关于此事的任何明确解释。

example说明了有问题的行为(在 IE9 以及最新版本的 Chrome 和 Firefox 中一致):

enter image description here

<div style="display: inline-block;">
    <div class="float"></div>
    <div class="float"></div>
    <div class="float"></div>
</div>

<hr/>

<div>
    <div class="float"></div>
    <div class="float"></div>
    <div class="float"></div>
</div>

最佳答案

行内 block 元素建立新的block formatting contexts为他们的内容。如果自动调整大小, block 格式化上下文根总是试图包含它的 float ;见section 10.6.7规范的:

In addition, if the element has any floating descendants whose bottom margin edge is below the element's bottom content edge, then the height is increased to include those edges.

这就是使内联 block 能够包含其 float 的原因;实际上不涉及清除,因为在 float 子项之后没有引入清除元素。

关于css-float - 为什么 `inline-block` 元素会自动清除他们的 child ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39665001/

相关文章:

html - CSS Float 内容创建空白并与其他 div 重叠

css - 如何在 Angular 选择时将下拉箭头的方向从右侧更改为向下

html - 如何使用 CSS 将以下框居中

c# - 为一个元素设置样式表

css - 可以选择第一个 child (当第一个元素通常不同时)

html - 背景框的底部不见了

jquery - 为什么我的 css 和 div 不能正常工作?

html - 如何让 div float 在彼此旁边和下方

html - 列内的嵌套 float /嵌套行

javascript - 让文本围绕可以由用户移动的对象流动