The CSS cascade algorithm is the process browsers use to resolve conflicts when multiple CSS declarations apply to the same element and property. It sorts declarations by origin and importance, then by specificity, then by source order, and finally by context (such as encapsulation or scoping), with the last remaining declaration winning. This deterministic system ensures that styles are applied consistently, even when rules from different sources compete.
Background: Why the Cascade Exists
CSS was designed to separate document structure from presentation, allowing multiple style sources to coexist. Without a defined conflict resolution mechanism, the same element could receive contradictory styles from user agents, user stylesheets, and author stylesheets. The cascade provides a clear hierarchy: user agent styles (browser defaults) have the lowest priority, followed by user styles, then author styles, and finally author styles marked !important. This order ensures that authors can override browser defaults, users can override author styles for accessibility, and critical author declarations can override user preferences when necessary. The cascade also considers specificity and source order to resolve conflicts within the same origin.
How the Cascade Algorithm Works
The cascade algorithm evaluates each declaration for a given property on an element and assigns it a weight based on several criteria. The declaration with the highest weight wins. The criteria, in order of precedence, are:
- Origin and Importance: Declarations are sorted by their origin (user agent, user, author) and whether they are marked
!important. The order from lowest to highest priority is: user agent normal, user normal, author normal, author!important, user!important, user agent!important. Note that!importantreverses the origin order: user agent!importanthas the highest priority, followed by user!important, then author!important. - Specificity: Within the same origin and importance level, declarations are compared by specificity. Specificity is calculated as a four-part value (inline, IDs, classes/attributes/pseudo-classes, elements/pseudo-elements). A higher specificity wins.
- Source Order: If origin, importance, and specificity are equal, the declaration that appears last in the source order wins.
- Context: For declarations in different encapsulation contexts (e.g., shadow DOM), the context determines priority. Styles from the outer context generally win over inner context styles, but this is nuanced and depends on the type of encapsulation.
These steps are applied in sequence: first, declarations are filtered by origin and importance; then, among those, specificity is compared; then source order; and finally context. The cascade also handles inheritance separately: inherited values are considered only if no declaration matches the element.
Specificity: The Tiebreaker Within an Origin
Specificity is a key part of the cascade. It is calculated as a four-tuple (a, b, c, d):
- a: 1 if the declaration is in a style attribute (inline style), else 0.
- b: The number of ID selectors.
- c: The number of class selectors, attribute selectors, and pseudo-classes.
- d: The number of element selectors and pseudo-elements.
For example, the selector #nav .item:hover has specificity (0, 1, 2, 0) because it has one ID, one class, and one pseudo-class. The selector ul li a has specificity (0, 0, 0, 3). When comparing, the tuple is compared lexicographically: a higher a wins; if equal, a higher b wins, and so on. Universal selector * has no specificity (0,0,0,0). Combinators like >, +, ~, and whitespace do not affect specificity.
Source Order and the Role of !important
Source order is the final tiebreaker when specificity is equal. If two declarations have the same origin, importance, and specificity, the one that appears later in the CSS source wins. This applies across stylesheets: the order of <link> and <style> elements in the HTML matters. The !important flag changes the cascade order by elevating a declaration's origin priority. For example, an author !important declaration overrides a user normal declaration, but a user !important declaration overrides an author !important declaration. This allows users to enforce accessibility preferences.
Common Problems and Misconceptions
A frequent misconception is that specificity alone determines the winner. In reality, origin and importance are evaluated first. For instance, an author style with low specificity will override a user agent style with high specificity because author normal has higher priority than user agent normal. Another common issue is the misuse of !important, which can lead to maintenance difficulties. Overusing !important breaks the natural cascade and makes debugging harder. Additionally, inline styles have high specificity (due to the a component) but can be overridden by author !important declarations. Understanding the cascade helps avoid these pitfalls.
How to Control the Cascade
Developers can manage the cascade by:
- Using low-specificity selectors and relying on source order.
- Avoiding
!importantunless necessary (e.g., for utility classes). - Using CSS layers (
@layer) to explicitly order styles without specificity hacks. Layers are sorted by their declared order, and unlayered styles have higher priority than layered ones. - Leveraging inheritance and custom properties for theming.
CSS layers, introduced in recent years, add another dimension to the cascade: declarations in later layers override those in earlier layers, regardless of specificity. This provides a more explicit way to manage conflicts.
Frequently Asked Questions
What is the difference between cascade and inheritance?
The cascade determines which declaration wins when multiple rules apply to an element. Inheritance, on the other hand, propagates computed values from parent to child when no declaration specifies a value for the child. Inheritance is considered only after the cascade has resolved all declarations for the element.
Does specificity always override source order?
Yes, within the same origin and importance level. Specificity is compared before source order. However, origin and importance are evaluated first, so a low-specificity author style can override a high-specificity user agent style.
How does !important affect the cascade?
!important reverses the origin order for important declarations. Author !important overrides author normal, but user !important overrides author !important, and user agent !important overrides all. This allows users to enforce their preferences.
What are CSS layers and how do they interact with the cascade?
CSS layers (@layer) allow grouping of styles into named layers. Layers are ordered by their first declaration. Styles in later layers override those in earlier layers, regardless of specificity. Unlayered styles have higher priority than layered styles. Layers provide a way to control the cascade without increasing specificity.
Why does my inline style not apply?
Inline styles have high specificity but can be overridden by author !important declarations or by user !important declarations. Also, if the property is not inherited and no other rule applies, the inline style should apply. Check for conflicting !important rules or higher-priority origins.
Can I override browser default styles?
Yes, author normal styles override user agent normal styles. However, user agent !important styles (rare) cannot be overridden by author styles. In practice, browser defaults are easily overridden by author styles.