Open Playground
CSS TutorialLesson #110

Specificity Hierarchy

Each type of CSS selector has a position in the specificity hierarchy, and the selector types carry different "weights".

Each type of CSS selector has a position in the specificity hierarchy, and the selector types carry different "weights".

SelectorExampleDescriptionWeight
Inline styles<h1 style="color: pink;">Highest priority, will override all other selectors
Id selectors#navbarSecond highest priority1-0-0
Classes, attribute selectors and pseudo-classes.test, [type="text"], :hoverThird highest priority0-1-0
Elements and pseudo-elementsh1, ::before, ::afterLow priority0-0-1
Universal selector and :where()*, where()No priority0-0-0

Understand the Weight Notation

The weight notation uses three numbers (X-Y-Z):

  • X - Number of ID selectors
  • Y - Number of class selectors, attribute selectors, and pseudo-classes
  • Z - Number of element selectors and pseudo-elements

The selector with the higher leftmost number wins. If equal, compare the next number, and so on.


Calculate the Specificity Weight

The declaration with the highest specificity/weight value will be applied to the element.

Example

Example Code

html
Try it Yourself
Live Output Preview:

More Specificity Examples

Understand how specificity works in different scenarios:

Equal specificity: the latest rule wins

If the same rule is written twice in the external style sheet, the latest rule wins:

Example

Example Code

css
Try it Yourself
h1 {background-color: yellow;}

h1 {background-color: red;}

ID selectors beat attribute selectors

Look at the following code lines - Here, the first rule has higher specificity than the second, and will therefore be applied:

Example

Example Code

css
Try it Yourself
#myDiv {background-color: yellow;}
div[id=myDiv] {background-color: blue;}

A class selector beats element selectors

A class selector such as .intro beats h1, p, div, etc:

Example

Example Code

css
Try it Yourself
.intro {background-color: yellow;}
h1 {background-color:
  red;}

The universal selector (*)

The universal selector (*) has no specificity weight value:

Example

Example Code

css
Try it Yourself
* {background-color: yellow;}
h1 {background-color: red;}

Inline style vs external style sheet

The embedded style sheet is closer to the element to be styled. So in the following situation the last rule will be applied:

Example

Example Code

css
Try it Yourself
Live Output Preview:


Interactive Code Examples

CSS Example 1

html
Try it Yourself
Interactive runnable CSS example from EA Academy.
Live Output Preview:

CSS Example 2

css
Try it Yourself
Interactive runnable CSS example from EA Academy.
Live Output Preview:

CSS Example 3

css
Try it Yourself
Interactive runnable CSS example from EA Academy.
Live Output Preview:

CSS Example 4

css
Try it Yourself
Interactive runnable CSS example from EA Academy.
Live Output Preview:

CSS Example 5

css
Try it Yourself
Interactive runnable CSS example from EA Academy.
Live Output Preview:

CSS Example 6

css
Try it Yourself
CSS syntax and property demonstration.
/*From external CSS file:*/
#content h1 {background-color: red;}


/*In HTML file:*/
<style>
#content h1 {background-color: 
yellow;}
</style>