CSS Specificity
In this page:
/* specificity: inline > #id > .class > element */
element { }
.class-name { }
#id-name { }
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.
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.
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.
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.
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.
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