gpt4 book ai didi

css - 相同的特异性,考虑到布局后,:first-letter always wins?

转载 作者:技术小花猫 更新时间:2023-10-29 10:37:59 25 4
gpt4 key购买 nike

看看这个 jsfiddle:http://jsfiddle.net/ZNddz/

.intro:first-letter {
font-size: 130px;
}
span.letter {
background-color: red;
font-size: 30px;
}
p {
font-size: 80px;
}

第一条规则由一个类选择器和一个伪元素选择器组成= 11

第二条规则包含一个类选择器.letter和一个标签选择器span = 11

这两个规则具有相同的特殊性,因此可以合理地认为获胜者应该是最后一个样式。显然不是这样的。所以我决定向第二条规则添加 background-color 属性,如您所见,它的高度为 30px。

我从中推断出这两个规则都没有选择相同的元素。但我想对这种效果进行官方解释,这有点太奇怪了。

最佳答案

I deduce from this that both rule are not selecting the same element.

这是因为 .intro 匹配 p 元素,而 span.letter.intro 的后代>。 As already mentioned, specificity is not relevant when selectors are matching different elements.但是由于每个选择器确实匹配了一些元素,所以这两个规则都会应用,导致你的红色背景在 span.letter 上生效。

But it's kinda too bizarre that I want to have an official explanation to this effect.

spec包含一些与您所拥有的非常相似的示例:以包含文本的内联级元素开头的 block 级元素,以及应用于 block 级元素的样式 :first-letter block 级元素及其内联级子元素上的伪元素。在所有示例中,:first-letter 伪元素在格式化结构方面始终是最内层的后代;这意味着它嵌套在行内级子元素中。

最后一个示例说明了元素的层次结构,包括伪元素,尽管之前的示例在其样式表中包含一个覆盖规则,它演示了在级联方面发生的情况:

The following CSS will make a drop cap initial letter span about two lines:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN">
<HTML>
<HEAD>
<TITLE>Drop cap initial letter</TITLE>
<STYLE type="text/css">
P { font-size: 12pt; line-height: 1.2 }
P::first-letter { font-size: 200%; font-style: italic;
font-weight: bold; float: left }
SPAN { text-transform: uppercase }
</STYLE>
</HEAD>
<BODY>
<P><SPAN>The first</SPAN> few words of an article
in The Economist.</P>
</BODY>
</HTML>

This example might be formatted as follows:

The fictional tag sequence is:

<P>
<SPAN>
<P::first-letter>
T
</P::first-letter>he first
</SPAN>
few words of an article in the Economist.
</P>

Note that the ::first-letter pseudo-element tags abut the content (i.e., the initial character), while the ::first-line pseudo-element start tag is inserted right after the start tag of the block element.

在您的情况下,两个 font-size 声明照常应用,但由于 .intro:first-letter 嵌套在 within span.letter,它使用自己的font-size 值。如果您使用相对值 it would be calculated based on span.letter , 如果你根本没有包含 font-size 样式 it would simply inherit it from span.letter .

请注意,:first-letter 伪元素适用于内联级元素(但它确实适用于内联 block ):

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.

行内框(使用display: inline 生成的)不是block container box . ( block 容器框的行内级框的示例是行内 block 。)

如果浏览器将伪元素应用于内联,那么它就违反了规范。虽然没有迹象表明当您对 block 容器和内联框后代都有 :first-letter 规则时应该发生什么,因为它确实说它不适用于内联,理想情况下是浏览器应该始终忽略针对行内框后代的规则。显然,Chrome 不这么认为。见Danield's answer .

关于css - 相同的特异性,考虑到布局后,:first-letter always wins?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19118036/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com