:首字母选择器不适用于链接
: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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
根据 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, whicha
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/检查规范。截至目前,
::first-letter
不支持inline
元素:因此,如果您尝试设置不是“块状容器”(例如内联元素)的
::first-letter
样式,这是行不通的。这不仅适用于链接;也适用于链接。您还会发现,默认情况下,您也无法设置span
的::first-letter
样式,如下所示:对此的一个可能的解决方法是将元素转换为块容器,例如将其设置为
display: block
或display: inline-block
。下面是一个示例,将前者用于span
,后者用于a
:Check the specification. As of now,
inline
elements are not supported by::first-letter
: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 aspan
either, as shown below:A possible fix to this is to turn the element into a block container by, for instance, setting it to
display: block
ordisplay: inline-block
. Below is an example, using the former for thespan
and the latter for thea
: