Use cascade order, specificity, inheritance, and importance to explain which declaration wins a style conflict.
When more than one CSS declaration sets the same property on an element, the browser uses the cascade to decide which value applies. Understanding that decision helps you explain why a style appears and how to change it.
A browser starts with its own user-agent stylesheet: default styles for HTML elements. For example, headings may appear bold and links may be blue and underlined. An author stylesheet contains styles written by the page's developers and can override browser defaults. Users can also supply styles to adjust a page's appearance, such as its font size or colors.
The cascade considers the origin and importance of declarations before comparing selector specificity. For ordinary stylesheet declarations, important values outrank normal values. Important user styles can override important author styles, allowing a reader to enforce a preferred color or size. Among declarations that remain tied, other factors such as layers, specificity, and source order help determine the winner.
Predict the color of the header. All three author rules match it.
#header {
color: blue;
}
.header {
color: red;
}
header {
color: green;
}All three selectors match the same element and set color. The declarations are normal author styles in the same layer, so compare their specificity, a measure based on selector types. An ID selector is more specific than a class selector, which is more specific than an element selector. Therefore #header wins and the text is blue, even though the header rule appears later.
For these selector types, specificity can be written as three counts: IDs, classes, and elements. For example, #header is 1,0,0, .header is 0,1,0, and header is 0,0,1. Compare the counts from left to right; the first difference decides the result. Attribute selectors and pseudo-classes count in the class column, while pseudo-elements count in the element column. Inline styles have higher priority than these normal selector rules.
The table compares selectors using the same three counts. A selector can include more than one type, so header .nav has one class count and one element count.
| Selector | ID, class, element counts | Relative specificity |
|---|---|---|
#header | 1,0,0 | Highest |
header .nav | 0,1,1 | Higher than .nav |
.nav | 0,1,0 | Higher than div |
div | 0,0,1 | Lowest |
The class counts for header .nav and .nav tie, so the element count makes header .nav more specific. The ID count makes #header more specific than either of them.
If importance, layer, and specificity are tied, source order decides. Here both selectors have the same specificity, so the later declaration makes the text green:
.example {
color: red;
}
.example {
color: green;
}Adding !important to a declaration raises its priority over conflicting normal declarations:
.element {
color: green !important;
}The text is green: the important declaration wins over the normal inline color. Use !important sparingly; frequent use makes conflicts harder to debug and styles harder to maintain. When important declarations compete, the cascade still has to resolve the conflict.
Cascade layers group styles and give you another way to control their priority within the same origin. For example:
@layer main, components;
@layer main {
.button {
font-size: 16px;
}
}
@layer components {
.button {
font-size: 14px;
}
}Both rules now set font-size on the same button. For normal author rules in these layers, the later layer has priority, so the button’s text is 14px. If you mark both declarations important, layer priority reverses and the 16px value wins. Normal unlayered stylesheet rules outrank normal layered ones.
The examples below assume both selectors match the same element and set the same property. Except where !important is shown, assume normal author declarations in the same layer. Specificity is shown as ID, class, element counts; combinators such as the space and > do not add a count.
| Scenario | Competing declarations | Winner and reason |
|---|---|---|
| Class and element | p (0,0,1) vs. .note (0,1,0) | .note: the class count is higher. |
| Pseudo-class | .button (0,1,0) vs. .button:hover (0,2,0) | .button:hover when the button is hovered: the pseudo-class adds a class count. |
| Pseudo-element | .note::after (0,1,1) vs. p::after (0,0,2) | .note::after: the class count decides before the element count. Both rules target the same ::after pseudo-element. |
| Descendant combinator | button (0,0,1) vs. .component button (0,1,1) | .component button: the class count is higher. |
| Child combinator | .panel p (0,1,1) vs. .panel > p (0,1,1) | If both match, the later declaration wins; the combinators add no specificity. |
| Media query | .note (0,1,0) vs. .note (0,1,0) inside a later matching media query | The later declaration wins while the media condition matches. The media query itself does not increase specificity. |
| Important declaration | #header (1,0,0) vs. .header (0,1,0) with !important | The important .header declaration wins over the normal ID declaration. |
| Inline style | #header (1,0,0) vs. a normal inline style | The inline style wins over the normal ID rule. |
For the media-query example, the second rule only competes when its condition matches:
.note {
color: red;
}
@media (min-width: 600px) {
.note {
color: green;
}
}At widths of at least 600px, both declarations match, and the later one sets the color to green. Otherwise, the first rule sets it to red.
When a style is unexpected, inspect the element in browser developer tools and identify the declarations that set the property. Check their origin and importance, then consider layer priority and specificity. If those checks tie, look at source order. Some properties can also inherit a value from a parent when the child has no value of its own.
For example, if both button and .component button set a normal color in the same layer, .component button wins because it is more specific. If the competing button declaration is marked !important, that important color wins instead. This sequence lets you identify the cause before changing the stylesheet.
Try it yourself
HTML + CSS
!important raises a declaration's priority over normal declarations.