CSS - Specificity

CSS Specificity

Specificity is an algorithm used by browsers which helps to determine the order or rank of CSS style to be applied to a specific HTML element.

The order of specificity in descending order is as follows:

  • Inline Styles
  • IDs
  • Classes, Pseudo-classes and Attribute Selectors
  • Elements and Pseudo Elements
  • Universal Selector

In the example below, the highest specificity value will override others. Since id is the highest here, green background will be applied.

In the example below, the highest specificity is inline style so therefore, orange background will be applied.

note
NOTE:

If the same specificity is written in an external or embedded style; the last style will override the previous ones.


In the example below, yellow background will be applied.

note
NOTE:

The !important rule overrides all specificity


ADVERTISEMENTS

LEARNING IS A CONTINOUS PROCESS - PRACTICE MAKES PERFECT