← Back to CSS
06

CSS Specificity: How Selectors Win Conflicts

Learn how specificity scores are calculated, when one rule wins over another and why you should avoid !important.

📘 Theory

Calculating specificity

More specific selector, higher priority.

Each selector type contributes a weight. It is often represented as (a, b, c): a = inline styles and !important, b = IDs, c = classes, attributes and pseudo-classes, d = tags and pseudo-elements. In practice, think of: tag = 1 point, class or attribute = 10 points, ID = 100 points. A selector like `#header .nav a` has more specificity than just `a`.

You can consult the web.dev specificity guide and the MDN reference for detailed tables.

  • Tag (p, div): 1 point.
  • Class (.class), attribute [href], pseudo-class (:hover): 10 points.
  • ID (#id): 100 points.
  • Inline style (style="..."): 1000 points.

!important

Wins over almost everything, but complicates the code.

Adding !important to a declaration gives that rule priority over others from the same origin, even with lower specificity. The problem is that it becomes very difficult to override that style without using another !important, and the code becomes fragile. Use it only in specific cases (for example, accessibility utilities that must always win).

Best practices

Simple and predictable selectors.

Prefer classes (.card, .btn) to apply styles: they are easy to understand and override. Avoid depending on IDs for styling (besides, the ID must be unique on the page). Do not chain many selectors (.header .nav .list .item) if not necessary; a single class name is usually enough.

🧭 Key visuals

Specificity scale

Helps you compare selectors without resorting to trial and error.

Comparison of specificity between tag, class and ID selector.

Step-by-step specificity comparison

Explains the correct tie-breaking criteria without relying on intuition.

Comparison of selectors by columns IDs, classes and elements to decide the winner.

🧪 Learn by doing

Example Compare specificity Change the selectors and observe which color wins.

🏁 Challenges

Challenge Challenge: win with a class Make the paragraph blue using a more specific class selector.
Challenge Challenge: avoid !important Override the color with a more specific selector, without !important.

🧰 Resources

Test

Check your knowledge with a test about CSS.

Test for CSS

What is this?

I'm Cristian Eslava and I sometimes build websites so both you and I can learn and experiment. culTest

I made this in February 2026 to make learning easier for my students. The idea is to learn web development by practicing and to keep expanding the project with new topics, tests and challenges.

It draws inspiration from MDN, W3Schools, CodePen, Manz and many other web development references. I wanted to combine useful theory, runnable examples, challenges and the testing system I had already built for culTest. culTest

If you liked it, if you didn't, or if you want to get in touch, write to me at cristianeslava@gmail.com