Live editor for all the examples will be added soon.
CSS specificity is a fundamental concept that helps determine which styles are applied to an element when multiple rules target the same element. In other words, specificity defines the priority or weight of CSS selectors, controlling how styles are applied. Understanding specificity allows you to efficiently manage conflicting styles and avoid using the !important
rule too often.
In this guide, we will explore how CSS specificity works, how different types of selectors contribute to specificity, and how to control and prioritize styles effectively.
1. What is CSS Specificity?
CSS specificity is a system used by browsers to determine which CSS rule should be applied when multiple styles target the same element. Each selector in CSS has a specific weight based on its type (ID, class, element, etc.). The more specific a selector is, the more priority it gets in the cascade.
For example, an ID selector is more specific than a class selector, which in turn is more specific than an element selector.
Example:
/* Element selector (low specificity) */
p {
color: blue;
}
/* Class selector (medium specificity) */
.text {
color: red;
}
/* ID selector (high specificity) */
#highlight {
color: green;
}
If you have a <p>
element with both a class .text
and an ID #highlight
, the ID selector will take priority, and the text will be green because ID selectors have the highest specificity.
2. How CSS Specificity is Calculated
CSS specificity is calculated using a four-part system, often referred to as A-B-C-D, where:
- A: Inline styles (directly applied using the
style
attribute in HTML). - B: ID selectors (
#id
). - C: Class selectors (
.class
), attributes ([type="text"]
), and pseudo-classes (:hover
,:nth-child
). - D: Element selectors (
div
,p
,h1
) and pseudo-elements (::before
,::after
).
Specificity Hierarchy:
- Inline styles (
A
) have the highest specificity. - ID selectors (
B
) are more specific than classes. - Class selectors, attribute selectors, and pseudo-classes (
C
) come next. - Element selectors and pseudo-elements (
D
) have the lowest specificity.
Each type of selector is given points based on the specificity calculation:
- Inline styles:
1-0-0-0
- ID selectors:
0-1-0-0
- Class selectors:
0-0-1-0
- Element selectors:
0-0-0-1
Example of Specificity Calculation:
/* Element selector */
p {
color: blue; /* Specificity: 0-0-0-1 */
}
/* Class selector */
.text {
color: red; /* Specificity: 0-0-1-0 */
}
/* ID selector */
#highlight {
color: green; /* Specificity: 0-1-0-0 */
}
If the HTML is as follows:
<p id="highlight" class="text">This text will be green.</p>
- The element selector (
p
) has a specificity of0-0-0-1
. - The class selector (
.text
) has a specificity of0-0-1-0
. - The ID selector (
#highlight
) has a specificity of0-1-0-0
.
The ID selector has the highest specificity, so the text will be green.
3. Specificity in Action
3.1 Element Selectors
Element (or type) selectors have the lowest specificity. They target all instances of a specific element, such as all <p>
tags or all <h1>
headings.
Example:
p {
color: blue; /* Specificity: 0-0-0-1 */
}
In this case, all paragraph elements (<p>
) will have blue text. However, this rule can easily be overridden by a more specific class or ID selector.
3.2 Class Selectors
Class selectors have higher specificity than element selectors. They allow you to target elements with a specific class, giving you more control over your styling.
Example:
p.blue-text {
color: red; /* Specificity: 0-0-1-1 */
}
This rule targets all <p>
elements with the class blue-text
. It has a specificity of 0-0-1-1
, making it more specific than a plain element selector.
3.3 ID Selectors
ID selectors have the highest specificity among normal CSS selectors. They are used to target elements with a unique ID.
Example:
#highlight {
color: green; /* Specificity: 0-1-0-0 */
}
Because ID selectors are highly specific (0-1-0-0
), they can override both class selectors and element selectors.
4. Handling Conflicts: Last Rule Wins
When two or more CSS rules with the same specificity target the same element, the rule that comes last in the stylesheet is applied. This principle is known as the “last rule wins”.
Example:
/* Both selectors have the same specificity: 0-0-0-1 */
p {
color: blue;
}
p {
color: red;
}
In this case, since both rules have the same specificity, the second rule (color: red
) will be applied because it comes last in the stylesheet.
5. Combining Selectors to Increase Specificity
You can increase the specificity of a CSS rule by combining multiple selectors. Each added selector increases the specificity value, making the rule more specific.
Example:
/* Single class selector: 0-0-1-0 */
.button {
background-color: lightgray;
}
/* More specific selector: 0-0-2-0 */
div .button {
background-color: blue;
}
In this case, the rule targeting div .button
will take precedence over the rule targeting .button
alone, because it has higher specificity.
6. Overriding Specificity with !important
The !important
declaration can override the natural cascade and specificity rules, forcing a style to be applied regardless of other styles.
Syntax:
element {
property: value !important;
}
Example:
p {
color: blue !important;
}
p {
color: red;
}
In this example, the text will be blue because the first rule uses !important
, which gives it the highest priority, regardless of specificity.
Caution:
!important
should be used sparingly because it can make your CSS harder to debug and maintain. It overrides all other rules, even ones with higher specificity or inline styles.
7. How Inline Styles Affect Specificity
Inline styles, applied directly to an HTML element via the style
attribute, have a very high specificity (the highest in normal CSS) and will override all other styles except those marked with !important
.
Example:
<p style="color: green;">This text is green.</p>
Even if there are external or internal CSS rules targeting this paragraph, the inline style will override them due to its high specificity.
8. Specificity Calculation Examples
Example 1: Combining IDs, Classes, and Elements
/* Element selector (specificity: 0-0-0-1) */
h1 {
color: blue;
}
/* Class selector (specificity: 0-0-1-0) */
.title {
color: red;
}
/* ID selector (specificity: 0-1-0-0) */
#header {
color: green;
}
<h1 id="header" class="title">Hello, World!</h1>
The text will be green because the ID selector has the highest specificity (0-1-0-0
).
Example 2: Multiple Class and Element Selectors
/* Specificity: 0-0-2-0 */
.container .text {
font-size: 18px;
}
/* Specificity: 0-0-1-1 */
.text p {
font-size: 16px;
}
<div class="container">
<p class="text">This text will have 18px font size.</p>
</div>
In this case, the first rule (0-0-2-0
) has higher specificity, so the text will have an 18px font size, even though the second rule targets the same element.
9. Best Practices for Managing Specificity
9.1 Keep Selectors Simple
Avoid using overly complex selectors. Instead of relying on increasing specificity, aim to use simple, reusable class selectors that can be easily overridden if needed.
9.2 Minimize the Use of IDs in CSS
Although ID selectors are powerful, they can be too specific, making them hard to override. For reusable and flexible styles, it’s better to use class selectors (.class
) rather than ID selectors (#id
).
9.3 Limit the Use of !important
Use the !important
declaration sparingly.
It can solve short-term problems but may lead to long-term issues with specificity conflicts and debugging.
9.4 Group Common Styles
Group related styles together using class selectors instead of overloading specificity. This keeps your CSS more modular and easier to maintain.
Example:
/* Good practice */
.button {
background-color: blue;
font-size: 14px;
}
.button:hover {
background-color: darkblue;
}
/* Avoid combining too many selectors to increase specificity unnecessarily */
div .section .button:hover {
background-color: darkblue;
}
9.5 Use CSS Variables for Consistent Styling
CSS variables (custom properties) don’t depend on specificity and can help reduce specificity conflicts by providing a consistent way to manage common values like colors and spacing.
Example:
:root {
--main-color: blue;
}
.button {
background-color: var(--main-color);
}
Conclusion
Mastering CSS specificity is essential for writing clean, maintainable, and effective CSS. By understanding how specificity is calculated and how it influences the application of styles, you can better control which styles are applied in conflicting situations.