CSS specificity is the set of rules browsers use to determine which CSS style is applied when multiple rules target the exact same element. When a conflict occurs, the rule with the higher specificity value takes priority and "wins."
<style>
h1 {
background-color: lightcoral;
color: white;
padding: 10px;
}
h2 {
color: navy;
}
</style>
<h1>Internal CSS applies here.</h1>
<h2 style="color: lightblue; background-color: navy; padding: 10px;">
Inline CSS overrides internal CSS!
</h2>
Understanding where your CSS is located and how it is written is crucial for managing specificity.
style attribute.<style> tag within the <head> of an HTML document.<style> tag is placed after the <link> tag, and the selectors have the exact same specificity..css file and linked using the <link> tag.Important Note: Specificity is mainly determined by the Selectors themselves (e.g.,
#headervs.header), rather than whether the styles are defined internally or externally. If the selectors used in External and Internal CSS contain the exact same components, their specificity is identical. In that case, the styles will be implemented based strictly on the order of inclusion in the HTML document (the last one loaded wins).
Every CSS selector has a strict position in the hierarchy. When calculating which rule applies, browsers look at this priority order:
| Priority | Description |
|---|---|
| 1. Inline style | Highest priority. Directly applied to the HTML tag using the style attribute. |
| 2. ID selectors | Second highest priority. Identified by the unique id attribute of an element (e.g., #navbar). |
| 3. Classes, Attributes, and Pseudo-classes | Medium priority. Targeted using class names (e.g., .button), pseudo-classes like :hover, and attributes like [type="text"]. |
| 4. Elements and Pseudo-elements | Lowest priority. Applies directly to HTML tags (e.g., h1, div) and pseudo-elements like ::before. |
(Note: The Universal selector and inherited values have the absolute lowest specificity, yielding to all of the above).*
Let's look at a "battle" between different selectors targeting the exact same <h1> tags to see who wins!
<style>
h1 { background-color: lightgray; color: black; padding: 10px; }
.class-heading { background-color: lightblue; color: navy; }
#unique-heading { background-color: navy; color: white; }
</style>
<h1 id="unique-heading" class="class-heading">ID wins</h1>
<h1 class="class-heading">Class wins</h1>
<h1>Element only</h1>
<h1 style="background-color: lightcoral; color: white;">Inline wins</h1>
If you have complex selectors, you can calculate their "specificity score" using a 4-number system: (Inline, IDs, Classes/Attributes, Elements).
style="..." = 1,0,0,0#navbar = 0,1,0,0.menu .item:hover = 0,0,3,0 (Two classes, one pseudo-class)div p a = 0,0,0,3 (Three elements)Example Calculation:
/* Score: 0, 1, 1, 2 */
/* (0 Inline, 1 ID, 1 Class, 2 Elements) */
#main-nav .link-btn a:hover {
color: navy;
}
!important ExceptionThere is one rule that breaks all specificity hierarchies: !important.
If you append !important to a CSS declaration, it will override everything, even inline styles.
p {
color: red !important; /* This will beat IDs, Classes, and Inline styles! */
}
Best Practice: Avoid using
!importantunless absolutely necessary (like overriding third-party plugin styles). Relying on it makes your CSS incredibly difficult to debug and maintain.
Which of the following has the HIGHEST specificity priority in CSS?