Master CSS From Scratch

Clear, interactive, and structured CSS lessons designed for beginners.

CSS Specificity

CSS specificity determines which CSS rule takes priority when multiple selectors target the same HTML element.


What is CSS Specificity?

When more than one CSS rule applies to the same element, the browser needs to determine which rule should be used.

CSS specificity is one of the mechanisms used to decide which selector has greater priority.

Interactive Specificity Visual

Hover over each level to see its priority. Higher specificity generally gives a selector more priority when declarations compete for the same property.

1
Element Selector p, div, h1
10
Class / Attribute / Pseudo-Class .card, [type], :hover
100
ID Selector #header, #main
1000*
Inline Style style="..."
Colorful layered abstract design representing CSS hierarchy
CSS Hierarchy

CSS specificity can be understood as a hierarchy of selector priority.

Basic Idea of Specificity

Element 1
→
Class 10
→
ID 100
→
Inline 1000*

Simple Explanation

You can think of specificity as a priority system. When competing declarations have otherwise comparable conditions, a more specific selector generally wins.

Basic Example

p {
    color: blue;
}

.text {
    color: red;
}

#message {
    color: green;
}

Suppose the same paragraph has the class text and the ID message. More than one rule matches the element.

<p id="message" class="text">
    Hello CSS
</p>

The ID selector has greater specificity than the class and element selectors, so the final text color becomes green.

Output

Hello CSS

Specificity Levels

Selector Type Example Typical Specificity Weight
Universal Selector * 0
Element Selector p 1
Class Selector .card 10
Attribute Selector [type="text"] 10
Pseudo-Class :hover 10
ID Selector #main 100
Inline Style style="..." Higher priority in normal author CSS

Class vs ID

.course {
    color: #2563eb;
}

#course {
    color: #7c3aed;
}
<p id="course" class="course">
    Learn CSS
</p>

Result

Both rules match the paragraph, but the ID selector has greater specificity than the class selector.

Multiple Selector Specificity

Specificity can also be thought of as a combination of selector types rather than simply counting selectors.

p {
    color: blue;
}

.course p {
    color: red;
}

#main .course p {
    color: green;
}
p 1 element
.course p 1 class + 1 element
#main .course p 1 ID + 1 class + 1 element

Pseudo-Class Specificity

Pseudo-classes such as :hover, :focus, and :checked contribute to specificity similarly to class selectors.

.button:hover {
    background-color: #7c3aed;
}

Example

The selector contains one class and one pseudo-class, so both contribute to the selector's specificity.

Attribute Selector Specificity

input[type="text"] {
    border-color: #2563eb;
}

Attribute selectors contribute to specificity at the same general level as class selectors.

What about !important?

The !important annotation changes the normal cascade priority and should be used carefully.

.message {
    color: red !important;
}

Important

Avoid using !important as a general solution for specificity problems. Excessive use can make styles harder to maintain and override.

Specificity and Source Order

When competing declarations have the same origin and specificity, the later applicable declaration can win through source order.

.title {
    color: #2563eb;
}

.title {
    color: #7c3aed;
}

Result

Because both selectors have the same specificity, the later declaration takes precedence.

Practical Example

Output

Specificity Comparison

Element 1 p
Class 10 .card
ID 100 #main
Inline 1000* style

CSS Specificity Best Practices

  • Keep selectors as simple as possible.
  • Avoid unnecessarily long selector chains.
  • Prefer reusable classes for component styling.
  • Avoid depending heavily on IDs for styling.
  • Use !important only when there is a clear reason.
  • Keep CSS organized so that overrides are predictable.

Real-World Uses

  • Managing component styles
  • Resolving conflicting CSS rules
  • Building reusable UI components
  • Maintaining large stylesheets
  • Debugging unexpected styling

Important Note

Specificity is only one part of the CSS cascade. Origin, importance, inheritance, layer rules, and source order can also affect which declaration is ultimately applied.

Summary

CSS specificity helps determine which competing CSS selector has priority. Element selectors have lower specificity than classes, while IDs have higher specificity. Inline styles can have strong priority, and !important changes normal priority. Understanding specificity makes CSS easier to debug and maintain.