CSS Specificity: A Beginner’s Guide to Taming Your Stylesheets 🎨

Evelyn Taylor
4 min readAug 1, 2023

--

Have you ever felt frustrated when your styles don’t apply as expected on your web page? 😩 Or maybe you’ve spent hours scratching your head, wondering why some elements just won’t change their appearance no matter what you try.

Well, fear not!

In the world of CSS (Cascading Style Sheets), there’s a powerful concept called specificity that can help you make sense of it all.

So, grab a cup of coffee ☕, sit back, and let’s dive into the wonderful world of CSS specificity.

Imagine you have a webpage with a heading that you want to style.

You’ve carefully crafted a CSS rule to give it a beautiful red color, like this:

h1 {
color: red;
}

But wait! Nothing happens. The heading stubbornly refuses to turn red. What’s going on? 🤔

Well, my friend, the culprit here is specificity.

CSS specificity determines which styles should be applied when there are conflicting rules targeting the same element.

It’s like a pecking order or a hierarchy for styles, and understanding it will save you from many headaches.

Specificity is calculated based on the different components of a CSS selector. Let’s take a look at these components and see how they stack up:

  1. Inline Styles: These are styles defined directly on the HTML element using the style attribute. They have the highest specificity. So if you have a heading like this:
<h1 style="color: blue;">Hello, world!</h1>

No matter what styles you define elsewhere, the color of this heading will be blue.

2. IDs: Selectors that use the id attribute have a higher specificity than class selectors and element selectors. An id should be unique within a page, so when you target an element using its id, like this:

#my-heading {
color: green;
}

The specificity of this rule will override any other rules that target the same element but with classes or element names.

3. Classes, Attributes, and Pseudo-Classes: Selectors that use classes, attributes, or pseudo-classes have a medium specificity. For example:

.my-heading {
color: purple;
}

a:hover {
color: orange;
}

In this case, the .my-heading rule will take precedence over the element selector (h1) but will be overridden by an ID selector targeting the same element.

4. Element Selectors: These selectors have the lowest specificity. They target elements based on their tag name, such as h1, p, or div. If no other rules with higher specificity override them, element selectors will be applied.

Now, let’s explore how the specificity values are calculated. Imagine you have these conflicting rules:

h1 {
color: red;
}

#my-heading {
color: blue;
}

Since IDs have higher specificity, the #my-heading rule will take precedence, and the heading will be blue.

Specificity is like a points system, where inline styles, IDs, classes/attributes/pseudo-classes, and element selectors each have their value.

The more specific the selector, the higher its points. When there's a conflict, the rule with the highest points wins.

To make it clearer, let’s compare some examples:

  • h1 (element selector) has a specificity of 0️⃣-0️⃣-0️⃣.
  • .my-heading (class selector) has a specificity of 0️⃣-1️⃣-0️⃣.
  • #my-heading (ID selector) has a specificity of 0️⃣-0️⃣-1️⃣.

You can think of the specificity values as three digits: ABC. The A represents the inline styles, the B represents IDs, and the C represents classes/attributes/pseudo-classes.

The rule with the highest specificity will have the highest value for A, B, or C. For example, a rule with a specificity of 1️⃣-0️⃣-0️⃣ will always override a rule with a specificity of 0️⃣-1️⃣-0️⃣.

Now that you have a basic understanding of specificity, you can use it to your advantage when styling your web pages. But remember, with great power comes great responsibility.

Overusing inline styles or relying heavily on IDs can make your CSS difficult to maintain and lead to specificity wars.

To keep your stylesheets organized and manageable, follow these best practices:

  1. Start with a solid HTML structure: Properly structure your HTML using semantic tags, classes, and IDs. This will make it easier to write targeted CSS rules.
  2. Use classes and attributes wisely: Classes are your best friends for applying styles to multiple elements. Keep them semantic and reusable, and apply them to the elements you want to style.
  3. Limit the use of IDs: IDs should be unique and used sparingly. Reserve them for cases where you need very specific styles or JavaScript functionality.
  4. Leverage cascading: Understanding how styles cascade through the document is key. Styles applied to parent elements will affect their children, so plan your selectors accordingly.
  5. Use !important sparingly: The !important declaration can override specificity rules, but it should be used as a last resort. Overusing it can make your stylesheets harder to maintain and debug.

CSS specificity is a powerful concept that can save you from many styling headaches.

By understanding how specificity works and applying best practices, you can write clean, maintainable stylesheets and create stunning web designs.

So go forth, conquer the specificity jungle, and let your web pages shine!

Connect with me on Medium ✍ : https://medium.com/@Evelyn.Taylor

--

--

Evelyn Taylor

A front-end enthusiast and dedicated development engineer, eager to expand knowledge on development techniques and collaborate with others.