CSS Specificity

In the large stage of cascading style with colors, fonts and layouts, we have an invisible variable – **CSS Specificity**. It works as a kind of arbitrator to determine which rules govern the rules when many rules control an individual element.

Understanding CSS Specificity

Browser uses specificity as a scoring system and weight to decide which CSS rule should be applied to an element when a couple of rules may apply. When one million rules are placed on top of one another in a period of fun, specificity is the invisible hand that shapes order.

How Specificity Works

Every CSS selector has a numeric value that determines what as spoiled its role over another. When two rules fight for impact, the rule with a higher specificity score is considered victorious over the other rule(s).

Calculating Specificity

Specificity is usually calculated in terms of four things:

  1. Inline styles (Example: style= " color: red ";) — Highest specificity.
  2. ID selectors (Example: #header).
  3. Class selectors, attribute selectors, pseudo-classes (Examples: .menu, [type="text"], :hover).
  4. Element selectors and a pseudo element selectors (Examples: h1, p, ::before).

You can think of each categories as a column of numbers, when comparing rules if they are printed on the same document they are compared column for column, the highest significance in the first column to the lowest in the last columns.

Examples of Specificity in Action

Imagine these rules competing to style a heading:


       h1 {
       color: blue;
       }
       .header h1 {
       color: green;
       }
       #main-header h1 {
       color: red;
       }
                  

Which color will the heading wear?

Answer: It will be red because the ID selector #main-header is more specific than a class or element selector.

Inline Styles and Specificity

Inline styles included within each HTML element have the highest specificity of all, and thus override the ID selector unless there was an inline style or !important declaration.


       <  h1 style= " color: purple;"> Welcome </h1>

This will override all external or internal CSS unless !important is a element of the rule declaration.

What About the !important Declaration?

,When a declaration is marked as !important, it receives royal decree and disregards the normal specificity calculations. In the event you applied an !important to multiple rules, as long as they are under the same specificity level, they will compare and the winner will be the one with the most specificity.

You should not overuse !important to the point of generating high amounts of very messy code that you will have difficulty working with afterwards.

Common Specificity Mistakes

Even professionals can make mistakes. Here are for warning of common mistakes:

Elegance lies in simplicity — a stylesheet should be readable, logical, and clean.

Tips to Manage Specificity Effectively

To slay the dragon of specificity these are some time tested suggestions:

Conclusion: The Silent Judge of Styles

It really shouldn't be a surprise, but CSS Specificity is the unannounced judge of order to ensure some effort for order in the beautiful creative chaos that is web design. With its knowledge in hand, you will be able to craft styles that are predictable, powerful and very, very beautiful.

So, write your selectors with care, know the weight they carry, and let your code dance effortlessly upon the browser's canvas!

Try it Yourself