Is an attribute selector for the ID attribute less specific than an ID selector?(ID 属性的属性选择器是否不如 ID 选择器具体?)
问题描述
我需要做些什么才能使 [id^=value]
选择器具有与常规 ID 相同的特异性,为什么它不等于或大于?(考虑到我也给了它html
)
What do I need to do to give the [id^=value]
selector the same specificity as a regular ID, and why isn't it equal or greater already? (considering that I gave it html
as well)
html div[id^="blue"] {
background-color: blue
}
#blue4 {
background-color: red
}
jsfiddle: http://jsfiddle.net/bjwe6yr0/1/一个>
推荐答案
属性选择器总是不如 ID 选择器具体;它的特异性值不会根据属性名称而改变.选择器仅将特定的属性名称映射到类选择器和 ID 选择器;属性选择器是一个通用概念,不包含任何此类映射.
An attribute selector will always be less specific than an ID selector; its specificity value does not change based on the attribute name. Selectors only maps specific attribute names to class selectors and ID selectors; an attribute selector is a generic concept and does not contain any such mappings.
复杂选择器具有 ID 特异性的唯一方法是它包含一个或多个 ID 选择器.除了实现限制之外,理论上不可能用任意数量的属性选择器或任何其他类型的简单选择器覆盖单个 ID 选择器.
The only way for a complex selector to have ID specificity is if it contains one or more ID selectors. Implementation limits aside, it is theoretically not possible to override even a single ID selector with any number of attribute selectors or any other type of simple selector.
以下是您的两个选择器的比较:
Here is how your two selectors compare:
/* 1 attribute, 2 types -> specificity = 0-1-2 */
html div[id^="blue"] {
background-color: blue
}
/* 1 ID -> specificity = 1-0-0 */
#blue4 {
background-color: red
}
即使添加 html
也无济于事,因为它只是一个类型选择器.将其更改为 :root
,您将获得一个伪类,它同样特定于属性选择器,因此 仍然 不如 ID 特定.
Even the addition of html
doesn't help because it's just a type selector. Change it to :root
and you get a pseudo-class which is equally specific to an attribute selector, and thus still less specific than an ID.
这篇关于ID 属性的属性选择器是否不如 ID 选择器具体?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:ID 属性的属性选择器是否不如 ID 选择器具体?


基础教程推荐
- 直接将值设置为滑块 2022-01-01
- 自定义 XMLHttpRequest.prototype.open 2022-01-01
- 我可以在浏览器中与Babel一起使用ES模块,而不捆绑我的代码吗? 2022-01-01
- Chart.js 在线性图表上拖动点 2022-01-01
- 如何使用JIT在顺风css中使用布局变体? 2022-01-01
- Vue 3 – <过渡>渲染不能动画的非元素根节点 2022-01-01
- 如何使用TypeScrip将固定承诺数组中的项设置为可选 2022-01-01
- html表格如何通过更改悬停边框来突出显示列? 2022-01-01
- 用于 Twitter 小部件宽度的 HTML/CSS 2022-01-01
- Electron 将 Node.js 和 Chromium 上下文结合起来意味着 2022-01-01