我今天在工作中遇到了这个不寻常的仅限 Firefox(据我所知 - 我只检查了 Safari 和 Chrome,并且使用的是 Firefox 3.6)CSS 错误,并设法用更小的代码片段重现了这个问题,这里:
<!DOCTYPE html>
<head>
<style>
/*
* A small snippet of some CSS resets code from html5doctor and YUI fonts and resets
* added just to make sure it's not from weird browser padding/margin. Still happens
* if this is removed though
*/
html, body, div, span, p, ul, li {
margin: 0;
padding: 0;
border: 0;
outline: 0;
font-size: 100%;
background: transparent;
}
body {
line-height: 1;
}
li {
list-style: none;
}
body {
color: #333;
font-family: Helvetica, Arial, Verdana, Geneva, sans-serif;
line-height: 1.3;
}
/* Some clearfix code from HTML5 Boilerplate */
.clearfix:before, .clearfix:after {
content: "\0020";
display: block;
height: 0;
visibility: hidden;
}
.clearfix:after {
clear: both;
}
.clearfix {
zoom: 1;
}
</style>
</head>
<body>
<div style="padding: 20px; border: solid thin black;">Hello!</div>
<div>
<ul class="clearfix">
<li style="float: left; padding: 5px; border: solid thin black;">There</li>
<li style="float: left; padding: 5px; border: solid thin black;">should</li>
<li style="float: left; padding: 5px; border: solid thin black;">be no</li>
<li style="float: left; padding: 5px; border: solid thin black;">margin</li>
<li style="float: left; padding: 5px; border: solid thin black;">above</li>
<li style="float: left; padding: 5px; border: solid thin black;">this</li>
<li style="float: left; padding: 5px; border: solid thin black;">list</li>
</ul>
<p style="margin-top: 30px">Yet for some reason the 30px margin-top on this p applies to both this p as well as the above list</p>
</div>
</body>
</html>
这是问题的截图
所以我通常期望在这里发生的是两个<div>
s 之间或 s 上方没有边距<ul>
,实际上,将鼠标悬停在 Firebug 中的元素上将不会显示边距/填充颜色。但由于某种原因,来自 30px 的 margin-top<p>
被同时应用于 <p>
及其包含的<div>
. 我的猜测是 clearfix 有一些问题(事实上,如果你使用 clearing <br/>
,这个问题就会消失),但我很好奇是否有人能深入了解这里的问题到底是什么。谢谢!