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.
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
}
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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…