CSS:字体上边距

发布于 2024-11-14 17:07:15 字数 1197 浏览 2 评论 0原文

我知道这个问题的标题很愚蠢,但我想不出更好的答案。我已经在这个主题上搜索了几个小时,但要么我搜索到了错误的东西,要么我错过了一些 CSS 基础知识。

我有一个容器 div,其左上角放置了 40x40px 背景图像。我想将 h2 标签中的文本顶部与背景图像的顶部对齐,我尝试这样做。

HTML:

<div id="container">
  <h2 id="title">Lorem</h2>
</div>

CSS:

body {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 13px;
}

#container {
    background: url('test-icon.png') top left no-repeat;
    margin: 200px auto; /* To center the container on the page*/
    min-height: 50px;
    padding-left: 50px; /* To prevent the text from "colliding with the background-image"*/
    width: 300px;
}

#title {
    font-family: "Arial Black", Arial, Helvetica, sans-serif;
    font-size: 1.3em;
    text-transform: uppercase;
}

我使用 Eric Meyers CSS 重置来摆脱浏览器特定的样式,但在上面的代码中忽略了这一点。

我创建了一个 实时示例

问题是背景图像和文本顶部来自h2 标签未对齐 - h2 标签顶部有一个小“边距”。这个边距似乎随着字体大小的变化而变化。当我使用 Google Chromes 检查器时,它告诉我 h2 和 div 标签的边距和填充都是 0px。由于浏览器渲染字体的方式不同,我不想乱用负边距,以免破坏布局。有没有办法“安全”地消除这个余量?完全清楚的是,我想要实现的是这个

I know the title of this question is stupid, but I couldn't come up with anything better. I have searched for hours on this topic, but either I search for something wrong, or else I have missed out on some CSS basics.

I have a container div with a 40x40px background-image placed in the top left corner. I want to align the top of the text from an h2 tag with the top of that background-image, which I have tried to do like this.

HTML:

<div id="container">
  <h2 id="title">Lorem</h2>
</div>

CSS:

body {
    font-family: Arial, Helvetica, sans-serif;
    font-size: 13px;
}

#container {
    background: url('test-icon.png') top left no-repeat;
    margin: 200px auto; /* To center the container on the page*/
    min-height: 50px;
    padding-left: 50px; /* To prevent the text from "colliding with the background-image"*/
    width: 300px;
}

#title {
    font-family: "Arial Black", Arial, Helvetica, sans-serif;
    font-size: 1.3em;
    text-transform: uppercase;
}

I have used Eric Meyers CSS reset to get rid of browser specific styles, but have left this out in the above code.

I have created a live example

The problem is that the background-image and the top of the text from the h2-tag do not align - there is a small "margin" in the top of the h2 tag. This margin seems to change with the font size. When I use Google Chromes inspector, it tells me that the margin and padding of the h2 and div tag are all 0px. As browsers render fonts differently, I don't mant to mess around with negative margins in case it breaks the layout. Is there a way to "safely" get rid of this margin? To be totally clear, what I want to acheive is this.

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

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

发布评论

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

评论(2

惯饮孤独 2024-11-21 17:07:15

我认为你的问题是 line-height

上。稍微摆弄一下就知道你希望它是这样的:

line-height: 10px;

你可能需要使用 px 作为你的 font-size 以使事情在各种浏览器中一致地工作。

I think your problem is the line-height on the <h2>. A bit of fiddling around tells me that you want it to be:

line-height: 10px;

You might need to use px for your font-size to get things to work consistently in various browsers.

一个人的夜不怕黑 2024-11-21 17:07:15

您是否尝试过行高:1; ?

Have you tried line-height: 1; ?

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