I've seen many cases refer to CSS specificity when trying to override a particular CSS style.
What is CSS specificity really and what is it used for?
I've seen many cases refer to CSS specificity when trying to override a particular CSS style.
What is CSS specificity really and what is it used for?
Specificity refers to the relevance of a particular CSS style to a page element that is being affected by multiple CSS styles at the same time. That is, it refers to the degree of importance of one style over another.
The greater the specificity that we are giving to a style, the greater the probability that this style will be applied in the end. To do this, the CSS rules follow an order of precedence.
The order of priority goes as follows, from least to most specific:
p
) and pseudo-elements (eg::before
).ejemplo
), attribute selectors (eg:[type="text"]
and pseudo-classes (eg::focus
)#ejemplo
)However, in addition to all these specifics, if we use inline styles they will override any styling on the external CSS pages. It could be said that inline styles have the most specificity, therefore, we should never use inline styles in our page .
Example of styles with an ID on a class
As we can see, we have two styles that refer to the paragraph (the ID
#parrafo
and the class.parrafo
) and, according to the cascading model that CSS uses to apply the styles, the class should be applied.parrafo
since it is placed last in the CSS file. . However, we see that this is not the case.This is where specificity comes into play, and having an ID that has higher specificity than a class, it is the style corresponding to the ID that is ultimately applied.
Example of inline styles on an ID
As we can see, the paragraph, even though it has an ID (element with greater specificity) with a green color style, is not applied because it also has inline styles and, therefore, the latter are applied as they have a greater specificity.
EXCEPTION: The use of !important
CAUTION!!: The use of the declaration
!important
is considered a bad practice. I explain it in the next point.The declaration
!important
associated with an element's style ignores the specificity of the rest of the styles applied to that element and applies the styles marked with that declaration.Referring to the first example:
Based on this example, and based on CSS specificity precedence ordering, the text should be green and 15px font size (because of the ID). However, we see that the resulting text is red and with a font size of 15px.
Why is this happening? Because even though the red color is contained within a class that has a lower specificity than an ID, the declaration
!important
ignores the specificity's priority order and sets this style as the one that takes precedence over the others.Why is the use of !important considered bad practice?
Because it totally breaks the CSS rules and makes it very difficult to maintain our CSS.
For example, let's imagine that we want all the paragraphs on our page within a div to be green, except for two paragraphs that will be red and purple respectively.
A possible solution could be the following:
As we can see, all the paragraphs inside the div with the class
.coloreados
are affected, except two, to which we have assigned an ID,#morado
and#rojo
and which are applied to said paragraphs because they have greater specificity.But what would happen if we used the statement
!important
in the class.coloreados
?As we can see in the example above, the styles for the IDs
#morado
and#rojo
are not being applied even though they have higher specificity than the class. This is due to the use of!important
.The result of the previous example is a problem since we could only overwrite these styles with another one
!important
associated to each one of the IDs, which limits us a lot when applying the CSS rules, which implies that it is much more difficult to maintain. .So should I never use !important?
No, there are times (very few) that there is no other option than to use the declaration
!important
or that it comes in handy for very specific cases. In my opinion, these cases would be:!important
applying to a particular style!important
and therefore cannot otherwise override (unless we modify the library's styles directly)h1
on your page have a blue color and a certain font. In this way, you "make sure" (unless there is someone else!important
who overwrites them) that allh1
of your page will have the same style.conclusion
Whenever possible, we should make use of CSS specificity to apply our styles and try to override the ones we already have.
We should NEVER abuse the use of
!important
, as it will require more effort when maintaining and applying the CSS rules on the styles of our page.