I have read on css tricks that :not should not add additional specificity. But it looks like it does?
https://css-tricks.com/almanac/selectors/n/not/
The specificity of the :not pseudo class is the specificity of its argument. The :not() pseudo class does not add to the selector specificity, unlike other pseudo-classes.
Or am I missing something?
.red:not(.blue) {
background: blue;
}
.red {
height: 100px;
width: 100px;
background: red;
}
<div class="red">
</div>
ID selectors have higher specificity than classes and elements. In our specificity weight system, they have a value of 100.
As a special case for increasing specificity, you can duplicate weights from the CLASS or ID columns. Duplicating id, class, pseudo-class or attribute selectors within a compound selector will increase specificity when overriding very specific selectors over which you have no control.
ID selectors: ID selectors are the most specific.
ID selectors have the highest specificity amongst the CSS selectors: 1, 0, 0, 0. Because of the unique nature of the ID attribute definition, we are usually styling a really specific element when we call up the ID in our CSS. – The specificity is 1, 0, 0, 1 because we have a type selector and an ID selector.
Yes, it adds the specificity of its argument. Look at the first sentence:
The specificity of the :not pseudo class is the specificity of its argument. The :not() pseudo class does not add to the selector specificity, unlike other pseudo-classes.
So the specificity of .red:not(.blue) is equal to that of .red.blue — 2 class selectors, or (0, 2, 0), making it more specific than .red on its own. What the second sentence means is that the :not() itself does not contribute the additional specificity of a pseudo-class to make it (0, 3, 0), like the :hover in .red.blue:hover does for example.
The :not selector don't have it's own specificity, however the selector inside :not() do have.
From MDN
Selector Types
The following list of selector types is by increasing specificity:
- Type selectors (e.g.,
h1) and pseudo-elements (e.g.,:before).- Class selectors (e.g.,
.example), attributes selectors (e.g.,[type="radio"]) and pseudo-classes (e.g.,:hover).- ID selectors (e.g.,
#example).Universal selector (
*), combinators (+,>,~,' ') and negation pseudo-class (:not()) have no effect on specificity. (The selectors declared inside:not()do, however.)
As you're having the rule .red:not(.blue) and the element <div class="red"> don't have the class blue, the rule is applied.
.red:not(.blue) {
background: blue;
}
.red {
height: 100px;
width: 100px;
background: red;
}
div {
background: green;
width: 50px;
height: 50px;
margin: 10px;
}
<div></div>
<div class="red"></div>
<div class="blue"></div>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With