:首字母选择器不适用于链接

发布于 2024-12-13 14:05:19 字数 270 浏览 1 评论 0原文

:first-letter 伪元素选择器非常适合

元素,但不适用于链接。例如,这没有任何效果:

a:first-letter
{
    font-size:200%;
    text-transform:uppercase;
    color:#8A2BE2;
}

为什么?如何使 元素的首字母样式不同

The :first-letter pseudo-element selector works perfectly for a <p> element but not for links. This, for instance, has no effect:

a:first-letter
{
    font-size:200%;
    text-transform:uppercase;
    color:#8A2BE2;
}

Why? How can I make the first-letter style different for an <a> element

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

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

发布评论

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

评论(2

百思不得你姐 2024-12-20 14:05:19

根据 W3 规范:first-letter 伪元素仅适用于块元素,而 a 则不然。

奇怪的是,在 Chrome 14 和 Firefox 3.6.23 中,*:first-letter 导致第一个字母发生变化。小提琴:http://jsfiddle.net/8W7FF/3/

According to the W3 spec, the :first-letter pseudo-element only work for a block element, which a is not.

Oddly, *:first-letter caused the first letter to transform, at Chrome 14 and Firefox 3.6.23. Fiddle: http://jsfiddle.net/8W7FF/3/

爱的十字路口 2024-12-20 14:05:19

检查规范。截至目前,::first-letter 不支持 inline 元素:

在 CSS 中,::first-letter 伪元素适用于块状容器,例如 blocklist-itemtable-celltable-captioninline-block 元素。
注意:此规范的未来版本可能允许此伪元素应用于更多显示类型。
https://www.w3.org/TR/selectors-3 /#application-in-css

因此,如果您尝试设置不是“块状容器”(例如内联元素)的 ::first-letter 样式,这是行不通的。这不仅适用于链接;也适用于链接。您还会发现,默认情况下,您也无法设置 span::first-letter 样式,如下所示:

div::first-letter, span::first-letter, a::first-letter {
    font-size:200%;
    text-transform:uppercase;
    color:#8A2BE2;
}
<div>I'm a div</div>
<span>I'm a span</span>
<a href="https://google.com">I'm an anchor</a>

对此的一个可能的解决方法是将元素转换为块容器,例如将其设置为 display: blockdisplay: inline-block。下面是一个示例,将前者用于 span,后者用于 a

div::first-letter, span::first-letter, a::first-letter {
    font-size:200%;
    text-transform:uppercase;
    color:#8A2BE2;
}
span {
    display: block;
}
a {
    display: inline-block;
}
<div>I'm a div</div>
<span>I'm a span</span>
<a href="https://google.com">I'm an anchor</a>

Check the specification. As of now, inline elements are not supported by ::first-letter:

In CSS, the ::first-letter pseudo-element applies to block-like containers such as block, list-item, table-cell, table-caption, and inline-block elements.
Note: A future version of this specification may allow this pseudo-element to apply to more display types.
https://www.w3.org/TR/selectors-3/#application-in-css

Thus, if you try to style the ::first-letter of something that isn't a "block-like container", like an inline element, it won't work. This doesn't just apply to links; you'll also find that, by default, you can't style the ::first-letter of a span either, as shown below:

div::first-letter, span::first-letter, a::first-letter {
    font-size:200%;
    text-transform:uppercase;
    color:#8A2BE2;
}
<div>I'm a div</div>
<span>I'm a span</span>
<a href="https://google.com">I'm an anchor</a>

A possible fix to this is to turn the element into a block container by, for instance, setting it to display: block or display: inline-block. Below is an example, using the former for the span and the latter for the a:

div::first-letter, span::first-letter, a::first-letter {
    font-size:200%;
    text-transform:uppercase;
    color:#8A2BE2;
}
span {
    display: block;
}
a {
    display: inline-block;
}
<div>I'm a div</div>
<span>I'm a span</span>
<a href="https://google.com">I'm an anchor</a>

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