html - 为什么 1px 的填充与 float div 的 0px 的填充不同 - 所有浏览器?

标签 html css

检查这个 HTML 示例 - 唯一的区别是

padding-top:0px; 对比 padding-top:1px;

但是第二个例子,div 偏移了一个完全不同的量?

<div style="clear: both; margin:0px; padding-top:0px; border: 0px">
    <div style="float:right;width:300px">
        <div style="margin:10px;border:1px solid silver">A Title</div>
        Sidebar<br />
        Sidebar<br />
        Sidebar<br />
        Sidebar<br />
        Sidebar<br />
        Sidebar<br />
    </div>

    <div style="">
        <div style="margin: 10px; border: 1px solid silver">A Title</div>
        Content<br />
        Content<br />
        Content<br />
        Content<br />
        Content<br />
        Content<br />
    </div>
</div>


<div style="clear: both; margin:0px; padding-top:1px; border: 0px">
    <div style="float:right;width:300px">
        <div style="margin:10px;border:1px solid silver">A Title</div>
        Sidebar<br />
        Sidebar<br />
        Sidebar<br />
        Sidebar<br />
        Sidebar<br />
        Sidebar<br />
    </div>

    <div style="">
        <div style="margin: 10px; border: 1px solid silver">A Title</div>
        Content<br />
        Content<br />
        Content<br />
        Content<br />
        Content<br />
        Content<br />
    </div>
</div>

这已经在 chrome/firefox/ie/safari 上进行了尝试,它一定是我遗漏的明显东西?

希望一些明亮的 Spark 能有所帮助!

最佳答案

此行为在 8.3.1 Collapsing margins 中定义:

The top margin of an in-flow block element collapses with its first in-flow block-level child's top margin if the element has no top border, no top padding, and the child has no clearance.

如果不填充,内容列的上边距会折叠。

关于html - 为什么 1px 的填充与 float div 的 0px 的填充不同 - 所有浏览器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23800954/

相关文章:

javascript - 未捕获的类型错误 : Cannot read property 'className' of undefined?

html - 为什么我的 CSS 类不起作用

javascript - 如何更改 css 下拉列表以根据文本大小更改大小?

javascript - 在纯 Javascript 中将 <div> 修复到视口(viewport)中间 w/'position: absolute'

html - 电子邮件输入字段宽度仅随内联样式变化

html - CSS 没有连接到 HTML

jquery - 如何在 body 内选择任意顺序的奇子?

html - 添加了显示 :inline-block, 但我的 HTML 元素仍然溢出到不同的行

html - Angular HTML 标记 - 表格导致错误

html - 无法单击div中的输入复选框