为什么当我将图像置于具有行高的 div 中时,顶部会出现 3px 间隙?

发布于 2024-08-03 10:45:45 字数 830 浏览 6 评论 0原文

请查看此页面。右侧的图像应位于其 div 的中心。但如果你仔细观察,就会发现顶部有一个大约 3 像素的小边框。如果您禁用 overflow:hidden (通过 firebug 或 IE8 等效项),它就会伸出底部。

HTML 是这样的:

<div class="small">
    <img src="/images/photos/Bedroom.jpg" alt="Bedroom" />
</div>
<div class="small">
    <img src="/images/photos/View.jpg" alt="View" />
</div>

CSS 是这样的:

div.small
{
    width:100px;
    height:100px;
    line-height:100px;
    text-align:center;
    overflow:hidden;
    margin:5px;
    background-color: #C0C0C0;
    float:left;
}
div.small img
{
    vertical-align: middle;
    max-width:100px;
    max-height:100px;
    display: inline;
}

是什么导致了这个神秘的差距?我检查了边距和填充,它们似乎不是问题。

Have a look at this page. The images on the right should be centered within their divs. But if you look closely, there's a small border of around 3 pixels at the top. And if you disable the overflow: hidden (through firebug or the IE8 equivalent), it sticks out the bottom.

The HTML is this:

<div class="small">
    <img src="/images/photos/Bedroom.jpg" alt="Bedroom" />
</div>
<div class="small">
    <img src="/images/photos/View.jpg" alt="View" />
</div>

And the CSS, this:

div.small
{
    width:100px;
    height:100px;
    line-height:100px;
    text-align:center;
    overflow:hidden;
    margin:5px;
    background-color: #C0C0C0;
    float:left;
}
div.small img
{
    vertical-align: middle;
    max-width:100px;
    max-height:100px;
    display: inline;
}

What is causing this mysterious gap? I've checked margins and padding, and they don't seem to be the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

野侃 2024-08-10 10:45:45

注意:我不完全确定这个解释是否正确,但对我来说似乎是合理的。

首先,让我们看看规范在 前导和半前导

由于“line-height”的值可能与内容区域的高度不同,因此渲染字形的上方和下方可能存在空间。内容高度和“line-height”使用值之间的差异称为行距。半行距称为半行距。

用户代理将字形垂直居中于行内框中,并在顶部和底部添加半行距。例如,如果一段文本的高度为“12px”,“line-height”值为“14px”,则应添加 2px 的额外空间:字母上方 1px,下方 1px。 (这也适用于空盒子,就好像空盒子包含一个无限窄的字母一样。)

到目前为止,一切顺利。因此,div.small 中高度小于 div.smallline-height 的任何行框都将垂直居中div.small。现在让我们看看vertical-align属性,特别是中间值:

将框的垂直中点与父框的基线加上父框 x 高度的一半对齐。

请注意,这不一定是线框的中心!确切的位置将根据您选择的字体和大小而变化。您可以通过放大和缩小文本来验证这一点:间隙的大小会发生变化。

正如您所发现的,设置 font-size: 0 可以完全消除间隙。由于字体现在没有高度,因此行框的前导和半前导为 50 像素,基线垂直居中。为了渲染 vertical-align: middle 图像,浏览器将其中点设置在该基线上,加上字体的 x 高度(现在为零)。这给出了垂直居中的图像。

NB: I'm not completely sure this explanation is correct, but it seems reasonable to me.

First, let's see what the spec has to say on leading and half-leading:

Since the value of 'line-height' may be different from the height of the content area there may be space above and below rendered glyphs. The difference between the content height and the used value of 'line-height' is called the leading. Half the leading is called the half-leading.

User agents center glyphs vertically in an inline box, adding half-leading on the top and bottom. For example, if a piece of text is '12px' high and the 'line-height' value is '14px', 2pxs of extra space should be added: 1px above and 1px below the letters. (This applies to empty boxes as well, as if the empty box contained an infinitely narrow letter.)

So far, so good. So any line boxes within a div.small that have a height less than the div.small's line-height will be vertically centered with the div.small. Now let's look at the vertical-align property, specifically the middle value:

Align the vertical midpoint of the box with the baseline of the parent box plus half the x-height of the parent.

Note that this is not necessarily the center of the line box! The exact position will change with your choice of font face and size. You can verify this by zooming the text larger and smaller: the size of the gap changes.

As you found, setting font-size: 0 removes the gap entirely. As the font now has no height, the line box gets a leading and half-leading of 50px, with the baseline centered vertically. To render the vertical-align: middle image, the browser sets its midpoint at that baseline, plus the x-height of the font, which is now zero. This gives a vertically-centered image.

若相惜即相离 2024-08-10 10:45:45

事实证明,在 div 上设置 font-size:0; 可以解决问题。然而,它仍然无法解释这一差距。有人知道造成差距的原因是什么吗?

Turns out, setting font-size:0; on the div fixes the problem. However, it still doesn't explain the gap. Anyone know what causes the gap?

长梦不多时 2024-08-10 10:45:45

我无法完全解释发生了什么,但在处理同样的问题后,它似乎与我在 css 中声明字体属性的方式有关,例如

font:11px/1.35em Verdana、Arial、Helvetica、sans-serif;

= 显然对桌子不利。 -- 我删除了该声明,而是使用 font-size:11px、line-height:px、font-family 等,它修复了间隙!

I cant explain entirely what is happening but after dealing with same problem, it appears it had something to do with the way I was declaring font property in css, e.g.

font:11px/1.35em Verdana, Arial, Helvetica, sans-serif;

= bad for tables apparently. -- I removed that declaration and instead used font-size:11px, line-height:px, font-family, etc, and it fixed the gap!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文