When multiple styles apply to
the same property (e.g.
color
)
on the same element (e.g. one specific
button
),
the cascade is used to determine
which style has priority.
While we often refer to that as specificity in conversation, that term actually refers to one small step of the process. The overall priority of a declaration is called its Cascade Precedence . But those two are directly related – one being a subset of the other. Selectors with higher specificity also have a higher cascade precedence.
For every set of styles declared on the same property of the same element, we start at the top of the cascade process. At each step, we group the styles into categories. The top category with styles in it is the only category that moves on to the next step. Any other styles can be eliminated from consideration.
In the first step, we separate out normal styles from ❗️important styles – and for most of the way down, we only need to look at one side or the other. It's like a single-elimination bracket, only one style can win it all!
Who applied the style, and how important is it?
Is it coming from the main document, or embedded shadow DOM?
importantSeparately for normal and important styles, inline styles win over selectors.
importantLayers stack in the order they are declared, with the first layer having lowest priority, building to the last layer, and then un-layered styles. The order is reversed for important styles.
importantThe selector with the most IDs wins. If there is a tie for the most IDs, repeat the process for each selector type.
#id
.class
|
:pseudo-class
|
[attribute]
element
|
::pseudo-element
*
|
:where()
Last style wins!
For properties without any valid style at the end of the cascade, we have to move beyond the cascade. Inheritance (looking at parent values) and then Defaulting (using the initial value) fill in any remaining gaps.