简体   繁体   English

将背景图像与标题元素中的文本顶部对齐

[英]Aligning a background image to the top of text in a header element

I'm having a problem aligning an image to the top the text in an <h2> element because the space between the top of the text and the top of the elements box is different across browsers (so far it's been different between IE7, Safari 4, and FF3 - haven't tested anything else). 我在将图像与<h2>元素中的文本顶部对齐时遇到问题,因为文本顶部和元素框顶部之间的空间在不同的浏览器中是不同的(到目前为止,IE7和Safari之间有所不同4和FF3-没有测试其他功能)。 Particularly noticeable because I'm using a large font (48px). 特别值得注意,因为我使用的是大字体(48px)。 Is there any way to reset this so they're all the same? 有什么办法可以重置它,所以它们都一样吗? It isn't margin/padding. 不是边距/填充。

This seems like it'd be an easy fix if I just take the text out of the header element and put it in a div, but it is in fact a header element and I'd like to still use it if at all possible. 如果仅将文本从header元素中取出并放入div中,这似乎很容易解决,但实际上它是header元素,并且我希望尽可能使用它。

Have a look into reset stylesheets , which are designed to reduce inconsistencies across different browsers thus saving a lot of work in the long term. 查看reset样式表 ,该样式表旨在减少不同浏览器之间的不一致,从而长期节省大量工作。 Also see: 另请参阅:

A reset stylesheet would be the obvious first step (see, for example, Eric Meyer's 'Reset Reloaded': 重置样式表将是显而易见的第一步(例如,参见Eric Meyer的“重置后重新加载”: http://meyerweb.com/eric/thoughts/2007/05/01/reset-reloaded/ ). http://meyerweb.com/eric/thoughts/2007/05/01/reset-reloaded/ )。

This essentially overrides browser rendering defaults back to 0, so all margins and padding is at 0px, all fonts are at 1em , line-heights at 1em etc. 这实际上会覆盖浏览器的渲染默认值,即默认为0,因此所有边距和填充均为0px,所有字体均为1em ,行高为1em等。

If you want to omit the use of a reset-stylesheet, you can just use the following: 如果要省略对重置样式表的使用,则可以使用以下代码:

h1,h2 /* etc... */
{padding: 0;
margin: 0;
}

No, that's not possible. 不,那不可能。

You can specify the margin, padding and line-height to get them as close as possible, but beyond that there is nothing you can do. 您可以指定页边距,内边距和行高,以使其尽可能接近,但除此之外,您无能为力。 Text will always be rendered differently in different browsers and on different systems. 在不同的浏览器和不同的系统上,文本将始终以不同的方式呈现。

How text is rendered depends on a lot of factors besides just how the browser positions it, like the fonts installed on the system, the font size the user selected and font smoothing settings. 呈现文本的方式不仅取决于浏览器的位置,还取决于许多因素,例如系统上安装的字体,用户选择的字体大小以及字体平滑设置。 You can never expect text to be rendered at the same pixel position on all systems. 您永远都不能期望在所有系统上以相同的像素位置呈现文本。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM