Today I’ve read a post from the code miner blog talking about CSS specificity.
CSS Selector Specificity It’s a way of avoiding the use of !import
directive and it’s quite simple:
Suppose that you have a CSS like this:
|
|
and this css code is applied to a HTML like this:
|
|
The question is: “What will be the background color of that div?
The answer is the result of specificity calculation and it’s like this:
!important | style attributes | id | Class, Attribute selector, pseudo-classes | Tag, pseudo-elements |
---|---|---|---|---|
!import | #id | .class,[type=“input”],:hover | h1, ::before |
The table of points will be:
selector | !important | style attributes | id | Class, Attribute selector, pseudo-classes | Type, pseudo-elements |
---|---|---|---|---|---|
div | 0 | 0 | 0 | 0 | 1 |
#box | 0 | 0 | 1 | 0 | 0 |
.box | 0 | 0 | 0 | 1 | 0 |
The biggest value we got is #box, which is 00100. And the background color will be red!
More examples can be found on the original post: https://blog.codeminer42.com/codetips2-how-specificity-works-in-css/