CSS Specificity: Classes, IDs, and !important
Lesson 3 of 41 in the CSS3 learning path.
Why does my CSS rule lose to another rule?
Specificity is the score a selector earns, and it explains why a rule you wrote later still loses. An element selector scores one point in the type column. A class, an attribute selector, or a pseudo-class scores one point in the class column, which beats any number of elements. An ID scores one point in the ID column and beats classes. The sample paints the same button three ways so you can see the element color lose to the class, and the class lose to the ID.
The important flag is not a specificity score. It creates a new layer that beats normal author rules, including IDs. Reach for it only when you cannot edit the losing rule, such as a third-party widget, and leave a comment that names the reason. In a codebase you own, raise a class or rewrite the selector instead. The orange button exists to show the override, not as a pattern to copy.
Syntax & Parameters
button { } /* (0, 0, 1) */
.enroll { } /* (0, 1, 0) */
#checkout { } /* (1, 0, 0) */
.force { } /* !important is a layer, not a higher score */
Example
Interactive preview availablebody {
margin: 0;
font-family: "Segoe UI", system-ui, sans-serif;
line-height: 1.45;
color: #1c2430;
background: #f4f1ea;
}
.page {
max-width: 46rem;
margin-inline: auto;
padding: 1.25rem;
}
h1 { margin: 0 0 0.35rem; font-size: 1.5rem; }
.lead { margin: 0 0 1rem; color: #5c6b7a; }
.choices {
display: flex;
flex-wrap: wrap;
gap: 0.75rem 1rem;
margin: 0;
padding: 0;
list-style: none;
}
.choice {
display: flex;
flex-direction: column;
align-items: flex-start;
gap: 0.35rem;
width: 11rem;
}
button {
margin: 0;
padding: 0.55rem 0.75rem;
border: 0;
border-radius: 8px;
background: #e7e5e4;
color: #1c2430;
font: inherit;
font-weight: 600;
cursor: pointer;
}
.enroll {
background: #0f766e;
color: #f8fafc;
}
#checkout,
#gift {
background: #1d4ed8;
color: #f8fafc;
}
.force {
background: #c2410c !important;
color: #fff7ed !important;
}
.choice span {
color: #5c6b7a;
font-size: 0.75rem;
}
Expected Output
Four buttons sit in a wrapping row, each with a short score caption underneath. Syllabus is stone gray. Enroll is teal with white text. Checkout is blue, even though it also has the enroll class. Gift is orange, even though an ID rule tries to make it blue. The captions name the winner: the element, the class, the ID, and the important flag. The row aligns to the start and wraps if the frame is narrower than the four 11rem columns. Text on the teal, blue, and orange buttons is light so the label stays readable.
Line-by-Line Walkthrough
- Read specificity as three columns: IDs, then classes (and attribute selectors and pseudo-classes), then elements and pseudo-elements.
- button is (0, 0, 1). It paints every button stone gray, and that is the color you see on Syllabus.
- .enroll is (0, 1, 0). One class beats any number of elements, so Enroll is teal even though the element rule also matches.
- #checkout is (1, 0, 0). One ID beats any number of classes, so Checkout stays blue even with class enroll on the same button.
- #checkout, #gift shares one declaration block, and each ID still has its own score of (1, 0, 0). The comma does not add the scores together.
- .force is only (0, 1, 0), so without the important flag it would lose to #gift and the gift button would stay blue.
- !important moves that declaration into a stronger cascade layer, which is why the gift button is orange instead of blue.
- The important flag is not a fourth column. Adding it does not make the selector "more specific". It skips the normal author layer.
- Source order never gets a vote here, because none of these competing rules tie. A later gray button rule would still lose to .enroll.
- An inline style attribute would score (1, 0, 0, 0) and beat the ID too. That is another reason to keep attributes out of the markup.
Use the orange button as a demonstration of the last resort. In a codebase you own, change #gift or drop the ID before you reach for the flag.
Common Pitfalls & Security
A lost rule is usually a stronger selector, not a broken file. Reaching for the important flag to "win" turns the next conflict into a war you cannot see in source order.
- IDs in reusable components mean a second instance on the page is invalid HTML, and the ID rule cannot be shared safely.
- Two classes, such as .enroll.ready at (0, 2, 0), still lose to a single ID. Adding classes will not beat #checkout.
- !important on a utility class beats your own ID, then a second !important is the only way back, and the stylesheet becomes a stack of flags.
- A widget stylesheet loaded later does not beat your class unless its selector is stronger or it uses the important flag. Check the score before you reorder files.
- Third-party CSS that flags its rules can cover your buttons. Sandbox that file, and do not give end users a style attribute that outranks your controls.
Best Practices & Modern Standards
Stay in one column of the score when you can: classes for components, a second class for a variant, and no IDs in the stylesheet. If a rule loses, raise the class you control or narrow the winner. Custom properties are the better override for a theme color, because a component can set --accent without a specificity fight.
Save !important for a bug you cannot edit, such as a third-party widget, and comment the reason next to the flag. Resets belong in :where() so they stay at zero specificity and every real class beats them. Layout variants belong in flex or grid classes, not in an ID that exists to win a color argument.
Real-World Use
A course checkout row has four actions that look like one component and then diverge. The quiet Syllabus button can stay on the element rule. Enroll is the class everyone reuses. Checkout is a one-off ID left over from an old page, and it stubbornly stays blue. Gift proves what an important flag does to that ID.
The fix in a real billing screen is to delete the ID selector and add a modifier class, so the next campaign button does not need another flag.
Frequently Asked Questions
How do I calculate a selector score quickly?
Count IDs, then classes, attribute selectors, and pseudo-classes, then elements and pseudo-elements. Ignore the universal selector. button is (0, 0, 1), .enroll is (0, 1, 0), and #checkout is (1, 0, 0). Compare the ID column first, because the higher column wins even when the other selector has more pieces.
Does a rule that comes later always override an earlier one?
Only when specificity ties and both rules are in the same cascade layer. A later element rule does not beat an earlier class, and a later class does not beat an earlier ID. Check the score in DevTools before you move lines around. Order is the tie-breaker, not the main rule.
Is !important part of the specificity number?
No. It puts that one declaration in a layer above normal author rules, so it beats IDs, classes, and inline styles that are not themselves important. The selector score underneath stays the same. Two important declarations then fall back to specificity and source order, which is how the wars start.
What should I do instead of adding an ID to win?
Add a class on the element you mean to style, and remove the ID from the stylesheet even if the HTML still has an ID for a form label or a fragment. Two classes can express a variant without beating the whole page. If the color is a theme, override a custom property on the component.