CSSÂ Specificity
1 April 2025 | Category: CSS
CSS Specificity is a set of rules browsers follow to determine which CSS styles should be applied to an element when there are conflicting rules. Understanding specificity is essential for ensuring that your styles are applied as expected and to avoid issues where styles don’t work due to specificity conflicts.
🔹 1. What is Specificity?
Specificity is a way for the browser to decide which CSS rule is more “specific” when multiple rules apply to an element. The browser calculates specificity using a weighting system. Each type of CSS selector (e.g., type selectors, class selectors, id selectors) has a certain value.
🔹 2. How Specificity Works
The browser gives weight to each selector type in your CSS rule. The higher the number, the more specific the rule is, and the more likely it will be applied.
Specificity is calculated based on a 4-value system (A, B, C, D):
- A: Inline styles (1 point)
- B: ID selectors (100 points)
- C: Class selectors, attribute selectors, and pseudo-classes (10 points each)
- D: Type selectors and pseudo-elements (1 point each)
Format of Specificity:
- Inline styles:
1, 0, 0, 0
- ID selectors:
0, 1, 0, 0
- Class selectors, pseudo-classes, and attributes:
0, 0, 1, 0
- Type selectors (like
div
,p
,h1
) and pseudo-elements:0, 0, 0, 1
🔹 3. How Specificity Is Calculated
Specificity is calculated by breaking down your selector into four components, based on the rule:
- Inline styles: If a style is applied directly to an element using the
style
attribute, it gets the highest specificity. This counts as 1, 0, 0, 0. - ID selectors: Each
#id
selector contributes 100 to the specificity score. Example:#header { color: red; }
→ specificity = 0, 1, 0, 0. - Class selectors, attribute selectors, and pseudo-classes: Each
.
class,[]
attribute, or:
pseudo-class selector adds 10 to the specificity score. Example:.button { color: blue; }
→ specificity = 0, 0, 1, 0. - Type selectors and pseudo-elements: Each element selector (like
p
,div
,h1
) adds 1 to the specificity score. Example:div { color: green; }
→ specificity = 0, 0, 0, 1.
Example of Specificity Breakdown:
div p a
→ specificity = 0, 0, 0, 3
(3 type selectors)#header .menu-item
→ specificity = 0, 1, 1, 0
(1 ID selector + 1 class selector).container .button:hover
→ specificity = 0, 0, 2, 0
(1 class selector + 1 pseudo-class)body div#content p:hover
→ specificity = 0, 1, 1, 2
(1 ID selector + 2 type selectors + 1 pseudo-class)
🔹 4. How Specificity Conflicts Work
If two selectors have the same specificity, the one later in the CSS file will override the earlier one. If they have different specificity, the more specific selector will win.
Example:
/* Selector with lower specificity */
p {
color: blue;
}
/* Selector with higher specificity */
#container p {
color: red;
}
In this example, even though both styles target the p
tag, the #container p
rule (with ID specificity) will override the p
tag rule (with type specificity) and the paragraph will be red.
🔹 5. Practical Examples of Specificity
Example 1:
<div id="header">
<p class="text">Hello</p>
</div>
<style>
p {
color: green; /* Specificity = 0, 0, 0, 1 */
}
.text {
color: blue; /* Specificity = 0, 0, 1, 0 */
}
#header p {
color: red; /* Specificity = 0, 1, 0, 1 */
}
</style>
In this case:
- The
#header p
rule wins because ID selectors (specificity = 0, 1, 0, 1) have the highest specificity. - The paragraph text will be red.
Example 2:
<div>
<p class="description" style="color: orange;">Text</p>
</div>
<style>
p {
color: green; /* Specificity = 0, 0, 0, 1 */
}
.description {
color: blue; /* Specificity = 0, 0, 1, 0 */
}
</style>
Here:
- Inline styles have the highest specificity. The inline style applied to the
p
tag (style="color: orange;"
) overrides all other CSS rules, so the text will be orange.
🔹 6. CSS Specificity Hierarchy
- Inline Styles: Applied directly to the element, has the highest specificity.
- IDs:
#id
- Classes, attributes, and pseudo-classes:
.class
,:hover
,[type="text"]
- Elements and pseudo-elements:
div
,h1
,::before
🔹 7. Best Practices for Managing Specificity
- Avoid excessive use of IDs in CSS selectors to prevent unnecessary specificity.
- Use classes to apply styles wherever possible, as they have moderate specificity.
- Use descendant selectors with caution, as they can make the CSS more specific than necessary.
- Keep your CSS organized to avoid unintended conflicts by using a naming convention (e.g., BEM – Block Element Modifier).
🔹 8. Specificity Summary
Selector Type | Specificity |
---|---|
Inline styles | 1, 0, 0, 0 |
ID selectors | 0, 1, 0, 0 |
Class selectors | 0, 0, 1, 0 |
Attribute selectors | 0, 0, 1, 0 |
Pseudo-classes | 0, 0, 1, 0 |
Type selectors | 0, 0, 0, 1 |
Pseudo-elements | 0, 0, 0, 1 |
🔹 9. Conclusion
CSS specificity plays a crucial role in determining which style rules are applied to an element. By understanding how it works, you can avoid conflicts and write more maintainable CSS. Always consider the specificity of your selectors and try to avoid creating overly specific rules unless absolutely necessary.