html - 内联 block 与 block : why does adding a "border" change inner height?

标签 html css

我注意到向 display:block 元素添加边框会改变它们尊重内部元素的 margin-top 值的方式。

在下面的例子中:http://jsfiddle.net/vbmaxgh0/2/ ,为什么“无边框” block 元素不会以与“边框” block 元素或“内联 block ”元素相同的大小呈现?

最佳答案

您遇到此问题是因为边距崩溃:

The adjoining margins of two or more boxes (which might or might not be siblings) can combine to form a single margin. Margins that combine this way are said to collapse, and the resulting combined margin is called a collapsed margin.

来源:W3C - 8.3.1 Collapsing margins

您还可以将 overflow: auto; 添加到 #block 来解决这个问题。

JSFiddle - DEMO

#block {
    display: block;
    overflow: auto; /* or overflow: hidden; */
}

进一步阅读:

Mozilla MDN - Margin collapsing

关于html - 内联 block 与 block : why does adding a "border" change inner height?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26325541/

相关文章:

html - 文本(绝对)不会通过垂直滚动条

css - 有没有办法使用 CSS 使子 DIV 的宽度比父 DIV 宽?

javascript - 取消复选框选择时触发单击事件

html - 有没有类似jsbin的HTML CSS测试bin

javascript - 在另一个 Canvas 上分层 Canvas ?

html - Overflow-y 导致 div 的高度为零

css - 您可以在不使用相对路径的情况下从 css 引用图像吗?

html - 限制在 div 中看到的内容

html - 响应式两列页脚

css调整div容器大小以适应小窗口