๐Ÿงฉ CSS Types & Hierarchy
Estimated reading: 4 minutes 357 views

CSS Specificity: Mastering Selector Priority in Web Design


Introduction: CSS Specificity

CSS specificity is a crucial concept that governs how styles are applied when multiple CSS rules target the same element. In the complex world of web design, understanding CSS specificity helps developers manage CSS rule order, avoid style conflicts, and create more maintainable stylesheets.

This article unpacks how specificity works, how it’s calculated, and how best to handle it using smart practices. Whether you’re dealing with inline styles, ID selectors, or class selectors, mastering specificity ensures a clean and predictable styling experience.


What is CSS Specificity?

Specificity in CSS determines which style rule is applied to an element when more than one rule matches. Browsers use specificity scores to resolve these conflicts, based on how targeted the selectors are.

Example: An ID selector (#header) has more specificity than a class selector (.header-style) or a type selector (header).


The Specificity Hierarchy

CSS assigns different weights to selectors. Here’s the order from most to least specific:

  1. Inline styles โ†’ style="color: red;"
  2. ID selectors โ†’ #mainDiv
  3. Classes, attributes, and pseudo-classes โ†’ .btn, [type="text"], :hover
  4. Element and pseudo-element selectors โ†’ div, ::before
  5. Universal selector โ†’ *

Note: The higher a selector appears in this list, the greater its ability to override other styles.


How Specificity is Calculated

CSS specificity uses a point value system:

Selector TypeSpecificity Value
Inline styles (style="")1000
ID selectors (#id)100
Classes, attributes, pseudo-classes10
Elements, pseudo-elements1
Universal selector (*)0

Selector Examples and Scores

Selector Specificity
style="color: red;"1000
#header100
.btn:hover20
input[type="text"]11
p::after2
*0

Practical Examples of Specificity in Action

1. Inline vs. External Style

<p style="color: red;">This text will always be red.</p>

Inline styles override all other selectors.


2. ID vs. Class vs. Element

p { color: blue; }        /* Specificity: 1 */
.text { color: green; } /* Specificity: 10 */
#intro { color: red; } /* Specificity: 100 */
<p id="intro" class="text">Hello World</p>

Output: Text will be red due to the ID selector having the highest specificity.


3. Equal Specificity โ€” Rule Order

p { color: blue; }
p { color: green; } /* Wins because it's declared last */

The last rule wins due to CSS cascade.


4. Internal vs. External CSS

Internal styles placed after external stylesheets will override them if specificity is equal.


๐Ÿšจ Special Case: !important and Specificity

The !important rule overrides all normal specificity.

p { color: green !important; }
#intro { color: red; }

Output: Paragraph will be green, because !important trumps ID specificity.

Use !important sparingly โ€” best for quick fixes or debugging, not long-term styling.


Best Practices for Managing Specificity

Recommended Avoid
Use class selectorsOverusing ID selectors
Follow BEM namingFrequent !important usage
Keep selectors shallowComplex, nested selectors

Pro Tip: Use browser DevTools or tools like Specificity Calculator to inspect and debug specificity issues.


Comparison Table: Selector Types and Specificity

Selector Type Example Specificity
Inline Stylestyle="..."1000
ID Selector#mainDiv100
Class/Attribute/Pseudo-class.special, [type], :hover10
Element/Pseudo-elementdiv, ::after1
Universal Selector*0

Summary: CSS Specificity

CSS specificity is the key to understanding how conflicting styles are resolved.
Itโ€™s calculated based on the type of selector, with inline styles being the most powerful.
Classes are your best friend โ€” use them strategically.
Avoid !important unless absolutely necessary.
Use DevTools and tools to inspect specificity and avoid bugs in complex stylesheets.

Mastering CSS specificity leads to clean, scalable, and conflict-free design systems!


Frequently Asked Questions (FAQs): CSS Specificity

What happens if two selectors have the same specificity?

The one that appears later in the CSS wins, due to the CSS cascade.

How does the order of CSS rules affect which style is applied?

If specificity is equal, the last-declared rule wins.

Can !important be overridden?

Yes, but only by another !important rule with higher specificity.

Why should I avoid using IDs for styling?

Because they are too specific and hard to override. Prefer class selectors.

How do browser developer tools display specificity?

Most DevTools highlight overridden styles and show specificity scores to help with debugging.


Share Now :
Share

๐ŸŽฏ CSS Specificity

Or Copy Link

CONTENTS
Scroll to Top