CSS Cascade & Inheritance
Learn how CSS determines which styles apply to elements and how properties pass from parents to children
What is the CSS Cascade?
The CSS cascade is the system that determines which style rules apply to an element when multiple rules could apply. It follows this priority order:
Whether !important
is used and the rule's origin
How specific the selector is
Which rule appears last in the CSS
Understanding Specificity
Specificity Calculation
Specificity is calculated using a 4-value system (a, b, c, d):
- a: Inline styles (1 if present, 0 otherwise)
- b: Number of ID selectors
- c: Number of class, attribute, and pseudo-class selectors
- d: Number of element and pseudo-element selectors
Specificity Examples
Note: When comparing specificities, start from left (a) and move right. A selector with higher a value wins regardless of b, c, or d values.
Cascade Example
Try This Code
/* Specificity example */ div#main .content p { /* Specificity: 1-1-2 */ color: blue; } .container p { /* Specificity: 0-1-1 */ color: red; } p { /* Specificity: 0-0-1 */ color: green; }
Importance and Source Order
!important Rule
The !important
flag changes the priority of a declaration, making it override any other declaration regardless of specificity.
Warning: Overusing !important
makes CSS harder to maintain. Use it sparingly!
Source Order
When two rules have equal importance and specificity, the one that appears later in the CSS wins.