带有边框/轮廓/边距/填充的奇怪错误
HTML(3x 次):
<div class="scream">
<div class="author">
<img src="avatars/dagrevis.png" alt="" title="daGrevis" />
</div>
<div class="data">
<span>Lorem Ipsum is simply dummy text of the printing and typesetting industry...</span>
</div>
<div class="clearer"></div>
</div>
CSS:
.scream {
background: #F4F4F4;
width: 944px; height: 48px;
}
.scream .author {
float: left;
}
.scream .data {
float: left;
}
.clearer { clear: both; }
这就是它在我的浏览器上的外观:
如您所见,< code>height 设置为 48px。图像大小也是 48px。为什么我没有在单独的行中看到每个 div.scream
?例如,如果我将 height
设置为 50px,则一切正常!在我看来,这是因为某种边框/轮廓/边距/填充毁了我的生活。不幸的是,对于 FireBug 我没有看到类似的东西......
HTML (3x times):
<div class="scream">
<div class="author">
<img src="avatars/dagrevis.png" alt="" title="daGrevis" />
</div>
<div class="data">
<span>Lorem Ipsum is simply dummy text of the printing and typesetting industry...</span>
</div>
<div class="clearer"></div>
</div>
CSS:
.scream {
background: #F4F4F4;
width: 944px; height: 48px;
}
.scream .author {
float: left;
}
.scream .data {
float: left;
}
.clearer { clear: both; }
This is how it looks on my browser:
As you can see, height
is set to 48px. Images size is 48px as well. How it comes that I don't see each div.scream
in separate line? If I set height
to 50px, for example, all works! In my opinion, it's because there are some-kind of border/outline/margin/padding that ruin my life. Unfortunately, with FireBug I don't see anything like that...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
默认情况下,图像的底部边缘与线框的基线(灰线)对齐。基线下方的空间(也称为“下降空间”)是导致问题的原因。有关更多详细信息,请参阅 Eric Meyer 撰写的这篇文章。
要修复此问题,请添加以下规则:
或者添加以下规则:
The bottom edge of an image is aligned with the baseline (the grey line) of the line box by default. The space below the baseline (also called "descender space") is what's causing your problem. See this article by Eric Meyer for more details.
To fix it, add this rule:
Or this one:
如果您稍微使用一下 DOM 检查器,您会发现
.author
的高度为 52px 左右。额外的 4px 似乎来自line-height
。将line-height
设置为 0:修复布局:http://jsfiddle.net/ ambigitude/8KzdD/
或者,您也可以将图像向左浮动:
这将从本地流中删除图像并使
line-height
变得无关紧要:http://jsfiddle.net/ambigously/8KzdD/1/另一种选择是放弃清理
并在外部
上使用
overflow:hidden
:这将留下
高度为 52px,但效果在视觉上并不明显:
If you play around with a DOM inspector for a bit, you'll find that
.author
is coming out with a height of 52px or so. The extra 4px appears to be coming from theline-height
. Settingline-height
to 0:Fixes the layout: http://jsfiddle.net/ambiguous/8KzdD/
Or, you can float the image to the left as well:
That will remove the image from the local flow and make the
line-height
irrelevant: http://jsfiddle.net/ambiguous/8KzdD/1/Yet another option is to ditch the clearing
<div>
and useoverflow:hidden
on the outer<div>
:This will leave the
<div class="author">
with their 52px height but the effect won't be visually noticeable: http://jsfiddle.net/ambiguous/8KzdD/2/