CSS 首字下沉在 IE9 中使用 em 填充错误定位

发布于 2025-01-02 16:22:09 字数 1297 浏览 0 评论 0原文

这是一个比其他任何问题都更有趣的问题,因为我已经设法解决了它,但并不是一个我觉得完全令人满意的解决方案。我宁愿知道问题发生的原因,以便更好地理解它。

我有几个段落的第一个字母是首字母大写,使用 CSS3 伪选择器。这在 FF、Opera 和 Safari 中显示良好,但在 IE9 中则不然。问题是我使用 em 单位作为填充来定位字母。如果我将它们更改为 px,则该字母在所有浏览器中都可以正常显示;但我不高兴在我的文本上混合 px 和 em 。我认为这与 IE9 渲染 em 单位的方式有关。

p {
    margin:0 0 1.5em 0;
    text-align:justify;
    font:1em/1.5 Georgia, Palatino, "Palatino Linotype", "Palatino LT STD", "Book Antiqua", serif;
}
.post-content p:first-child:first-letter {
    float:left;
    color:#444;
    font-size:3.3em;
    padding:0.1em 0.1em 0 0;
    line-height:0.7em;
    text-shadow:2px 2px 0 #dadada;
}
<section class="post-content">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sit amet mi ut erat dapibus varius. Cras aliquet augue eget ipsum posuere a mattis quam gravida. Proin pretium rhoncus mi, nec dapibus odio iaculis id. Aenean mattis, nulla eu hendrerit fermentum, urna tellus tristique mauris, eu dignissim quam arcu ut nisi.</p>
</section>

我在这里做了一个 JSFiddle: http://jsfiddle.net/C5zsv/

This is more of an intriguing problem than anything else since I have managed to solve it, but not with a solution that I find entirely satisfying. I'd rather know why the problem occurs to better understand it.

I have several paragraphs with drop-caps on the first letters using CSS3 pseudo-selectors. This displays fine in FF, Opera and Safari but not IE9. The problem is the em units I'm using as padding to position the letter. If I change these to px the letter displays fine in all browsers; BUT I'm not happy mixing px and em on my text. I assume this has something to do with how IE9 renders em units.

p {
    margin:0 0 1.5em 0;
    text-align:justify;
    font:1em/1.5 Georgia, Palatino, "Palatino Linotype", "Palatino LT STD", "Book Antiqua", serif;
}
.post-content p:first-child:first-letter {
    float:left;
    color:#444;
    font-size:3.3em;
    padding:0.1em 0.1em 0 0;
    line-height:0.7em;
    text-shadow:2px 2px 0 #dadada;
}
<section class="post-content">
    <p>Lorem ipsum dolor sit amet, consectetur adipiscing elit. Donec sit amet mi ut erat dapibus varius. Cras aliquet augue eget ipsum posuere a mattis quam gravida. Proin pretium rhoncus mi, nec dapibus odio iaculis id. Aenean mattis, nulla eu hendrerit fermentum, urna tellus tristique mauris, eu dignissim quam arcu ut nisi.</p>
</section>

I've made a JSFiddle here: http://jsfiddle.net/C5zsv/

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

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

发布评论

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

评论(1

短叹 2025-01-09 16:22:09

是的,这个问题对我来说就像一个美妙的黑匣子谜题,所以我试图收集一些证据来支持任何答案。事实证明,各种浏览器对 em:first-letter 的组合的呈现方式截然不同。

首先,这是我用来测试这个的代码(问题小提琴的一个分支):

<p>Yyy is a<br />tall letter<br />indeed.</p>
<p>Ñino has a<br />tall letter<br />as well.</p>
<p>ggg has a<br />big bottom<br />ahw yeah.</p>

和CSS:

p {
    margin: 0 0 1.0em 0;
    font: 3em/1.5 Georgia;
    background-color: #CCE;
}
p:first-letter {
    float: left;
    font-size: 3.3em;
    padding: 0.1em 0.1em 0 0;
    line-height: 0.7em;
    background-color: #DFD;
}

查看 the new fiddle

在各种浏览器中如何呈现:

First-letter in different browser

基本上这个问题的答案(或者至少是底线,没有双关语)似乎是:混合emfirst- letter 让您的网站受浏览器和字体系列的支配。孩子们,他们是不仁慈的。

这种“怜悯”还有一些有趣的功能:

  • Chrome (125px) 和 IE (134px) 保持第一个字母的一致高度,但 FF 决定给 Ñ 一些额外的高度。
  • FF 几乎总是将字母保留在实际的框中(Y 的左上角部分除外,但所有 3 个浏览器都这样做)。
  • Chrome 和 IE 都允许 Ñ 和小写 g 超出框外。
  • 三个浏览器中没有一个第二行文本的底部与第一个字母的实际底部部分对齐。
  • 将内边距设置为 px 确实可以改善 IE 中大写 Y 的情况,但是对我来说,这在 FF 和 Chrome 中一直把事情搞砸。
  • 正如预期的那样,当您更改字体系列时,结果会有很大差异。
  • 最后但并非最不重要的一点是:这只是第一个字母的行为不一致。在这三个浏览器中,三个常规文本行非常相似(甚至可能相同?)。

来自 w3.org 的信息

在 CSS3-linebox 模块中,有一些关于 基线对齐< /a> 这似乎与这些问题相关。可能需要阅读一下才能了解其状态(或者也许这里有人可以添加它?)。

Right, this question felt like a wonderful black box puzzle to me, so I tried to gather some evidence to support any answers. Turns out various browsers render the combination of em and :first-letter quite differently.

First up, here's the code I used to test this (a fork of the question's fiddle):

<p>Yyy is a<br />tall letter<br />indeed.</p>
<p>Ñino has a<br />tall letter<br />as well.</p>
<p>ggg has a<br />big bottom<br />ahw yeah.</p>

And the CSS:

p {
    margin: 0 0 1.0em 0;
    font: 3em/1.5 Georgia;
    background-color: #CCE;
}
p:first-letter {
    float: left;
    font-size: 3.3em;
    padding: 0.1em 0.1em 0 0;
    line-height: 0.7em;
    background-color: #DFD;
}

Check out the new fiddle

How this renders in various browsers:

First-letter in various browsers

Basically the answer to this SO question (or at least bottom line, no pun intended) seems to be: mixing em and first-letter leaves your site at the mercy of the browser and the font-family. And boy, are the not merciful.

This "mercy" has a few interesting features as well:

  • Chrome (125px) and IE (134px) keep a consistent height for the first-letter, but FF decides to give Ñ some extra height.
  • FF almost always keeps the letter inside the actual box (with the exception of the top-left part of the Y, but all 3 browsers do this).
  • Chrome and IE both allow the Ñ and lowercase g to go outside the box.
  • Not one of the three browsers has the bottom of the second line of text aligned with the actual bottom part of the first-letter.
  • Setting the padding in px does improve the capital Y situation in IE, however for me this kept screwing things up in FF and Chrome.
  • As expected results vary wildly when you change the font-family.
  • Last but not least: it's just the first-letter that's not behaving consistently. The three regular lines of text are quite similar (perhaps even identical?) in these three browsers.

Info from w3.org

In the CSS3-linebox module there is some info on baseline alignment that seems relevant to these issues. May have to read on a bit to see what the status is on that (or perhaps someone here can add it?).

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