CSS Specificity
In this page:
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
<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
<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
<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
<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
<style>
p { color: green !important; }
</style>
<p style="color: orange;">!important beats even the inline style: green</p>
- Using standard HTML element tags for global styling and then using !important to resolve conflicts.
- Writing long, compound selectors (e.g., body div.container ul li.item a) which inflate specificity scores unnecessarily.
- Using ID selectors to style group components instead of using reusable classes.
- 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.
Standard CSS specificity scoring rules and !important declarations are supported natively by all web browsers.
Chapter Quiz — Complete all 22 topics to unlock
0/22 topics done
Complete these topics first:
- CSS Combinators
- CSS Pseudo-classes
- CSS Pseudo-elements
- CSS Opacity
- CSS Navigation Bar
- CSS Vertical Navbar
- CSS Horizontal Navbar
- CSS Dropdowns
- CSS Advanced Dropdowns
- CSS Image Gallery
- CSS Image Sprites
- CSS Attribute Selectors
- CSS Forms
- CSS Counters
- CSS Specificity
- CSS Specificity Hierarchy
- CSS Variables
- CSS !important
- CSS Box Sizing
- CSS Media Queries
- CSS Pointer Events
- CSS Outline