CSS Specificity is the set of the rules applied to CSS selectors in order to determine which style is applied to an element.
- It helps you understand why you styles aren't being applied.
- It helps you organize your css code.
- It helps you write cleaner and Less css code.
At a high-level, there are three buckets of specificity your CSS will fall under:
- Type selectors & pesudo-elements
// Type selectors
h1{}
// Pesudo-elements
::before{}
- Class selectors & attibuite selectors & pesudo-classes
// Class selectors
.cat { }
// Attibuite selectors
[type="radio"] { }
// Pesudo-classes
:hover { }
- Id selectors
// Id selector
#cat { }
Note :
You may run into situations when leveraging CSS frameworks, such as Bootstrap, where you can't use CSS specificity to override the native styles. In these instances, using !important is not considered bad practice.