为什么 # 选择器的特异性比任何选择器都低?

发布于 2024-09-11 08:04:22 字数 838 浏览 4 评论 0原文

大粗体大写锁定 TL;DR:

我知道如何确定选择器特异性,我认为它使用了有缺陷的假设,并且我可以用有效的集合理论关系来支持我的愤怒,请不要回应解释 W3 特异性计算规则,请阅读问题 <- 阅读该问题。

当我为某些 HTML 编写类似于下面的样式时,这困扰了我一段时间:

...
<div id="outer">
    <span id="inner"></span>
    <span></span>
    ...
</div>
...

为什么特异性规则使选择器“#outer span”比“#inner”更具体? ID 是唯一的,所以当我说“#inner”时,我只能指一个元素,那么为什么它不那么具体呢?我理解确定特异性的规则,我只是想知道这是有意还是无意,还有是否有人知道我如何向编写 css 标准的人提出这个问题。

我应该注意,我确实明白我可以使用 #outer #inner 来确保最大的特异性,但这似乎首先就违背了 ID 的目的。当我编写模板并且不确定一个 ID 是否位于另一个 ID 内时,这也是一个有问题的解决方案。我不是在寻找解决方法,只是寻找理论答案。

我的问题是理论,完全基于设定逻辑。我的想法是,如果您为 n 个可能项目中的 1 个项目定义规则,这不是您可以做到的最具体的吗?为什么 CSS 选择器的创建者会制定一个规则来定义 n 个可能项目中的 m 个项目,其中 m 是 n 的子集作为更具体的规则?

我的想法是,#id 相当于按名称识别 1 个项目,而 #id elm 将根据名称与项目的关系来识别组。调用一个命名项不如具有命名关系的未命名组具体,这是完全违反直觉的。

Big bold caps-lock TL;DR:

I KNOW HOW SELECTOR SPECIFICITY IS DETERMINED, I THINK IT USES FLAWED ASSUMPTIONS AND I CAN BACK MY IRRITATIONS UP WITH VALID SET THEORY RELATIONS, PLEASE DO NOT RESPOND EXPLAINING W3 CALCULATION RULES FOR SPECIFICITY, PLEASE READ THE QUESTION <- read that.

This has bothered me for some time, when I write a style for some HTML that would be similar to below:

...
<div id="outer">
    <span id="inner"></span>
    <span></span>
    ...
</div>
...

Why would specificity rules make the selector "#outer span" more specific than "#inner"?
ID's are unique, so when I say "#inner" I can ONLY be referring to one element, so why is it less specific? I understand the rules on determining specificity, I just wonder if this was intentional or accidental, also if anyone knows how I can ask this question to the people who write the css standards.

I should note, I do understand that I COULD use #outer #inner to ensure maximum specificity, but that seems like it defeats the purpose of ID in the first place. This also is a problematic solution for when I write templates and I'm not sure that one ID will be inside of another. I'm not looking for a workaround, just a theory answer.

My question is theory, entirely based on set logic. The though I have is that if you define a rule for 1 item of n possible items, isn't that as specific as you can go? Why would the creators of CSS selectors make a rule that could define m items of n possible items, where m is a subset of n as a more specific rule?

My thought is that #id would be the equivalent of identifying 1 item by name, and #id elm would be identifying a group by its relation to an item by name. It's completely counter intuitive to call a named item less specific than an unnamed group with a named relation.

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

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

发布评论

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

评论(4

恋你朝朝暮暮 2024-09-18 08:04:25

如何

计算特异性的 W3C 规则

  • 如果声明来自“style”属性而不是
    使用选择器规则,否则为 0 (=
    a) (在 HTML 中,元素的值
    “style”属性是样式表
    规则。这些规则没有选择器,
    所以 a=1、b=0、c=0 和 d=0。)
  • 计算选择器中 ID 属性的数量 (= b)
  • 计算其他属性和伪类的数量
    选择器 (= c)
  • 计算选择器中元素名称和伪元素的数量(=
    d)

特异性仅基于
选择器的形式。特别是,一个
“[id=p33]”形式的选择器是
算作属性选择器(a=0,
b=0, c=1, d=0),即使 id
属性被定义为“ID”
源文档的 DTD。

连接四个数字 abcd
(在基数较大的数字系统中)
给出了特异性。
此外,当规则具有相同的特殊性时,最后一个获胜。

示例

  • 外跨度:a=0, b=1, c=0, d=1 --> 101

  • span#inner: a=0, b=1, c=0, d=1 --> 101
  • div#outer span#inner: a=0, b=2, c=0, d=2 --> 202

尝试重新排列规则 1 和 3:http://jsfiddle.net/Wz96w/

为什么< /strong>

我的想法是 #inner 没有指定唯一的元素。虽然每页只有 1 个元素,但该 ID 可能是另一页上完全不同的元素。

一页:

<div id="outer">
  <div id="inner"> ... </div>
</div>

另一页:

<ul id="outer">
  <li>main1
    <ul id="inner">
      <li>sub1</li>
      <li>sub2</li>
    </ul>
  </li>
  <li>main2</li>
</ul>

虽然,我不会这样编码。我认为它解释了为什么添加元素(ul#outer#outer)值得额外的特殊性。

对于后代这一点,我正在可视化标记的 DOM。规则#outer span的路径长度比#inner的路径长度长。因此,在这种情况下,它指定了一个更具体的子树,因此应该授予它更多的特异性(并且 #outer #inner li 应该 [is] 比 #inner li 更有价值代码>)。

How

W3C rules for calculating specificity:

  • count 1 if the declaration is from is a 'style' attribute rather than a
    rule with a selector, 0 otherwise (=
    a) (In HTML, values of an element's
    "style" attribute are style sheet
    rules. These rules have no selectors,
    so a=1, b=0, c=0, and d=0.)
  • count the number of ID attributes in the selector (= b)
  • count the number of other attributes and pseudo-classes in the
    selector (= c)
  • count the number of element names and pseudo-elements in the selector (=
    d)

The specificity is based only on the
form of the selector. In particular, a
selector of the form "[id=p33]" is
counted as an attribute selector (a=0,
b=0, c=1, d=0), even if the id
attribute is defined as an "ID" in the
source document's DTD.

Concatenating the four numbers a-b-c-d
(in a number system with a large base)
gives the specificity.
Also, when rules have the same specificity, the last one wins.

Example

  • outer span: a=0, b=1, c=0, d=1 --> 101

  • span#inner: a=0, b=1, c=0, d=1 --> 101
  • div#outer span#inner: a=0, b=2, c=0, d=2 --> 202

Try rearranging rules 1 and 3: http://jsfiddle.net/Wz96w/

Why

My thought is that #inner does not specify a unique element. While it is only 1 element per page, that ID could be a completely different element on another page.

One page:

<div id="outer">
  <div id="inner"> ... </div>
</div>

Another page:

<ul id="outer">
  <li>main1
    <ul id="inner">
      <li>sub1</li>
      <li>sub2</li>
    </ul>
  </li>
  <li>main2</li>
</ul>

Although, I would not code it this way. I think it explains why adding the element (ul#outer vs. #outer) is worthy of extra specificity.

For the point on descendants, I'm visualizing the DOM for your markup. The rule #outer span has a path length longer than that of #inner. Therefore, in the case, it specifies a more specific subtree, so it should be awarded more specificity (and #outer #inner li should be [is] worth more than #inner li).

§对你不离不弃 2024-09-18 08:04:25

对我来说,我完全基于观点,这是预期的“自然”行为。

考虑一下:

您知道 CSS 特异性是如何计算的,从该公式我们知道 #outer span#outer 更具体,这对于 CSS 整体来说是必要的才能正确工作,而且这是有道理的。 #outer span 也比 #inner 更具体,这在样式表的域内也是符合逻辑的(#inner 只是一个 ID, #outer span 是一个 ID 加一个元素,因此如果我们只是查看样式表,为了对它们进行排名,更合格的必须更具体)。

这里发生的情况是,您应用了 HTML 标记的上下文,并说“嗯,这没有意义”。为了使事情按照您期望的方式工作,浏览器必须考虑以下事项:

  • 位于
  • #outer span#inner 应用的样式表规则 #outer span
  • #outer span 更具体代码>#inner
  • 但是等等! 位于
    内部,因此忽略基于样式表的计算并声明 #inner 更具体

最后一步使得确定过程完全基于 HTML 的结构,这使得无法仅根据 CSS 来定义特殊性。我个人认为这会使整个过程更加复杂且难以定义,但你可能不同意。

To me, and I base this entirely on opinion, it's the expected "natural" behaviour.

Consider this:

You know how CSS specificity is calculated, and from that formula we know that #outer span is more specific than #outer, which is necessary for CSS on the whole to work correctly, and it makes sense. #outer span is also more specific than #inner, which is also logical within the domain of the stylesheet (#inner is only an ID, and #outer span is an ID plus an element, so in order to rank them if we are just looking at the stylesheet, the more qualified one must be more specific).

What's happening here is that you're applying the context of the HTML markup, and saying "Well, that doesn't make sense." To make things work the way that you're expecting, the browser would have to consier the following:

  • This <span id="inner"> is inside <div id="outer">
  • The stylesheet rules for #outer span and #inner apply
  • The rule #outer span is more specific than #inner
  • But wait! <span id="inner"> is inside <div id="outer">, so ignore the calculations based on the stylesheet and claim that #inner is more specific

That last step makes the determination process entirely based on the structure of the HTML, which makes it impossible to define the specificity in terms of the CSS alone. I personally believe that this would make the entire process more convoluted and hard to define, but you may disagree.

酒解孤独 2024-09-18 08:04:24

因为#outer span既有ID选择器又有元素选择器。该元素选择器使它比 #inner 更重。

前者的意思是“选择在 ID outer任何元素中找到的任何元素”。
后者意味着“选择 ID 为 inner任何元素”。它不知道 #inner 在 HTML 文档中的位置,因此特异性较低。

也许您可以尝试#outer #innerspan#inner尝试#outer span#inner

Because #outer span has both an ID selector and an element selector. That element selector is what makes it weigh more than #inner.

The former means 'select any element found within any element of ID outer'.
The latter means 'select any element with ID of inner'. It doesn't know where #inner is in your HTML document, hence less specificity.

Perhaps you could either try #outer #inner or span#inner try #outer span#inner instead.

羞稚 2024-09-18 08:04:23

我认为“为什么”的想法更多的是“世代”或“权威”的观点。如果#Parent(任何一代人)说我所有符合条件“x”的孩子(在您的例子中,span)将获得“y”的继承权"(无论什么 css 属性),单个 #Child 想要什么并不重要,如果父级已声明,它需要 #Parent 的权限才能获取它否则。

编辑时添加:内联样式将成为叛逆的孩子,而!important将成为镇压的父母。 编辑:我保留这个是为了幽默,但我不认为它反映了这个想法以及我后来在下面的陈述。

在评论中添加对问题的编辑:给出:

#outer span ...
#inner (which is a span element)

然后为了帮助确保#inner选择,我建议:

body span#inner (*edit:* just span#inner works *edit:* if defined later)

或者给body一个id,

#bodyId #inner

当然,这些仍然可以被覆盖。涉及的“代际”越多,由于代际共识,改变行为就越困难(如果曾祖父、祖父和父母都同意,孩子很可能不会做自己的事情) 。

我必须在以后的编辑中主要重写这一部分
鉴于此 HTML:

<div id="grandparent">
  <div id="parent">
    <div id="child"></div>
  </div>
</div>

我之前曾说过“#parent div#grandparent div 具有更大的权限。两者都具有代际权限,事实上,是“平等”的代际权限,但第一个是“近一代”获胜。 其中的错误是,世代“更接近”并不重要,重要的是最后被授予权力。在同等权力的情况下,最后指定的人获胜。

我相信我仍然可以坚持这样的说法:考虑到这一点,像 #child[id] 这样的选择器(它比之前的两个选择器都重要)将其属性视为获得更大权限的权限,以规则其本身控制的内容。 # 已经赋予了它权限,但不足以覆盖较早一代的 #(如果较早一代还带有另一个授予更多权限的选择器)。

因此,#grandparent div 超过 #child,但不是 div#child 如果它是最后一个获得授权的[添加此],而不是 #child[id],因为 [id]#child 增加了更大的权力来统治自己。如果选择性相同,则最后一个被授予权限的人获胜。

同样,设置样式属性本身的 style 属性实际上更像是最高的授予权力来统治自己,假设更“!important”的东西不会剥夺它。

作为回答“为什么”是这样的总结陈述(并且不符合“集合”理论),我相信这与准确性甚至特异性无关(尽管这是使用的术语)事实上,人们会期望 #ChildsName 成为此事的最终唯一发言权,因为没有什么更具体的需要说明。然而,尽管文档可能没有这样说明,但“选择性”实际上是建立在授予权限的基础上的。权限。谁拥有最多的“权利”来统治该元素,并给予“平局”,谁是最后一个被授予这些权利的人。

I think the idea of "why" is more a "generational" or "authority" view point. If #Parent (of any generation back) says all my children who meet qualification "x" (in your case, span) are going to be given an inheritance of "y" (whatever css property), it doesn't matter what the single individual #Child wants, it needs the authority of the #Parent to get it if the parent has stated otherwise.

Added on edit: The inline style would then be the rebellious child, and the !important the crack down parent. Edit: I kept this for humor, but I don't think it reflects the idea as well as my later statement below.

Added on edit to question in comment: Given:

#outer span ...
#inner (which is a span element)

Then to help insure #inner selection I recommend:

body span#inner (*edit:* just span#inner works *edit:* if defined later)

or give body an id and

#bodyId #inner

Of course, these can still be overridden. The more "generations" involved, the more it becomes difficult to change the behavior because of the generational consensus (if great grandpa and grandpa and parent are all in agreement, it's likely the child is not going to get away with doing his own thing).

I had to majorly rewrite this section on later edit
Given this HTML:

<div id="grandparent">
  <div id="parent">
    <div id="child"></div>
  </div>
</div>

I had previously stated that "#parent div has greater authority than #grandparent div. Both have generational authority, in fact, an 'equal' generational authority, but the first is 'nearer' generation" wins. The error in that is that "nearer" generationally is not what matters, but rather last to be granted authority. Given equal authority powers, the own designated last is the one that wins.

I believe I can still stand by this statement: With that thought in mind, a selector like #child[id] (which outweighs both previous selectors) treats its attributes as permissions for greater authority to rule that which itself controls. Having the # already gave it authority, but not enough to override a # of a earlier generation if that earlier generation also carries another selector granting more authority.

So #grandparent div outweighs #child but not div#child if it is last to receive authority [added this], and not #child[id] because the [id] adds greater authority for the #child to rule itself. If equal selectivity then last one to be granted authority wins.

Again, the style attribute setting a style property itself really acts more like a supreme granting of authority to rule oneself, assuming something more "!important" doesn't take it away.

As a summary statement to answer "why" it is this way (and not in line with "set" theory), I believe it is not about accuracy or really even specificity (though that is the term used) as indeed then one would expect #ChildsName to be the final unique say in the matter because nothing more specific need be said. Rather, however, while the documentation may not state it as such, "selectivity" is really structured on a granting of authority. Who has the most "rights" to rule the element, and given a "tie", who was the last one to be granted those rights.

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