← Back to CSS Course | Chapter 6: Selectors & Styling | Lesson 15 of 22

CSS Specificity

Imagine styling a button on your website. You write a rule telling all buttons to be red, but then you write another rule telling buttons with the class blue-btn to be blue. The browser has to decide which rule wins. CSS Specificity is the scoring system the browser uses to resolve this conflict. It calculates a score for every CSS selector based on how specific it is. The selector with the highest score wins the conflict, and its style is applied to the element.

The Specificity Score Calculation

Specificity is calculated as a four-part score (0,0,0,0). From highest to lowest, the columns represent inline styles, IDs, classes/attributes/pseudo-classes, and element tags.

Note: Keep your selectors simple to avoid creating specificity conflicts that are hard to debug.

Warning: Do not write deeply nested selectors as they artificially inflate your specificity score.

Example: The Specificity Score Calculation

css
<style>
p { color: black; }
.text { color: blue; }
#main { color: red; }
</style>
<p id="main" class="text">Highest specificity wins: red</p>

Element vs. Class Specificity

Element selectors target HTML tags directly, while class selectors target custom classes. A class selector always wins over an element tag selector because it is more specific.

Note: Use classes for styling components to keep your CSS rules reusable.

Warning: Styling base elements directly can cause accidental styling leaks across your page.

Example: Element vs. Class Specificity

css
<style>
p { color: black; }
.highlight { color: green; }
</style>
<p class="highlight">Class beats element: green</p>

Class vs. ID Specificity

An ID selector points to a single, unique element on your page. Because IDs are unique, they carry much more specificity weight than group styling classes.

Note: Use classes for styling elements, and reserve IDs for unique anchors and scripting.

Warning: Avoid using IDs as your primary CSS styling selectors, as it makes your stylesheets difficult to reuse.

Example: Class vs. ID Specificity

css
<style>
.text { color: green; }
#unique { color: purple; }
</style>
<p id="unique" class="text">ID beats class: purple</p>

Inline Styles Override

Inline styles are declared directly inside your HTML elements using the style attribute. Because they are defined directly on the element, they have a specificity score of (1,0,0,0) and override any stylesheet rules.

Note: Only use inline styles for quick debugging or dynamic changes styled by JavaScript.

Warning: Relying on inline styles makes it difficult to maintain design consistency across your site.

Example: Inline Styles Override

css
<style>
p { color: green; }
</style>
<p style="color: orange;">Inline style wins: orange</p>

The !important Exception

The !important declaration is a nuclear option in CSS specificity. When appended to a property value, it bypasses the standard specificity score calculation completely, forcing the browser to apply that rule.

Note: Avoid using the !important tag to force style overrides because it ruins layout inheritance and makes troubleshooting difficult.

Warning: Overusing !important leads to specificity wars that make your stylesheets unmanageable.

Example: The !important Exception

css
<style>
p { color: green !important; }
</style>
<p style="color: orange;">!important beats even the inline style: green</p>
Common Mistakes
  1. Using standard HTML element tags for global styling and then using !important to resolve conflicts.
  2. Writing long, compound selectors (e.g., body div.container ul li.item a) which inflate specificity scores unnecessarily.
  3. Using ID selectors to style group components instead of using reusable classes.
Chapter Summary
  • The browser uses a four-part score calculation to determine CSS Specificity and resolve styling conflicts.
  • Inline styles and IDs carry the highest specificity scores, easily overriding classes and element selectors.
  • Avoid using !important to force style overrides, as it complicates stylesheet maintenance and troubleshooting.
Browser Support

Standard CSS specificity scoring rules and !important declarations are supported natively by all web browsers.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.