Which One Of The Following Overrides The Others

6 min read

Which One Overrides the Others? Understanding CSS Specificity and the Cascade

When building websites, one of the most common frustrations developers face is understanding why certain styles apply and others don't. You add a CSS rule expecting a blue background, but your element remains red. You write what should be a powerful selector, yet an simpler rule keeps winning. This confusion stems from one fundamental concept in CSS: the cascade and specificity determine which one overrides the others Not complicated — just consistent..

Understanding which styles override others is crucial for writing maintainable, predictable CSS. Whether you're a beginner learning your first stylesheets or an experienced developer debugging complex styles, mastering this concept will save you countless hours of frustration.

What is CSS Specificity and the Cascade?

CSS stands for Cascading Style Sheets, and the word "cascading" is the key to understanding how styles override each other. The cascade is the algorithm that determines which CSS rules apply when multiple rules target the same element. Specificity, on the other hand, is the measure of how specific a selector is in targeting an element.

When browsers render a webpage, they collect all the CSS rules that apply to each element. That said, if there's only one rule targeting an element, the process is straightforward. On the flip side, when multiple rules exist, the browser must decide which one wins. This is where specificity and the cascade work together.

The cascade processes styles in the following order:

  1. Origin: User agent styles (browser defaults) have the lowest priority
  2. Normal declarations: Regular CSS rules
  3. Important declarations: Rules marked with !important
  4. Animations and transitions: These temporarily override normal rules

Within each level, specificity determines the winner. Higher specificity always overrides lower specificity, regardless of the order in which rules appear in your stylesheet.

How Specificity is Calculated

Specificity is calculated using a three-part value written as (a, b, c), where:

  • a = number of ID selectors
  • b = number of class selectors, attribute selectors, and pseudo-classes
  • c = number of type selectors (elements) and pseudo-elements

Let's break this down with examples:

Selector Specificity
* (universal) (0, 0, 0)
p (0, 0, 1)
.container (0, 1, 0)
#header (1, 0, 0)
div p.highlight (0, 1, 2)
`#nav .

The key principle is: higher specificity always wins. Plus, a selector with specificity (0, 1, 0) will override any selector with specificity (0, 0, 5). The leftmost value is the most significant, followed by the middle, then the rightmost Which is the point..

The Hierarchy of Selectors: What Overrides What

Understanding the hierarchy helps you predict exactly which style will win. Here's the complete hierarchy from lowest to highest priority:

1. Universal Selector and Inherited Styles

* {
  color: black;
}

The universal selector has zero specificity and is easily overridden by any other rule.

2. Element Selectors (Type Selectors)

p {
  color: blue;
}

Element selectors target all paragraph elements but can be easily overridden by classes or IDs.

3. Class Selectors, Attribute Selectors, and Pseudo-Classes

.highlight {
  color: yellow;
}

Classes have higher specificity than element selectors, so they override them.

4. ID Selectors

#main-title {
  color: red;
}

IDs have even higher specificity than classes. An ID selector will always override a class selector targeting the same element.

5. Inline Styles

This text

Inline styles have higher specificity than any external or internal stylesheet rule, except when !important is used Not complicated — just consistent..

6. The !important Declaration

p {
  color: purple !important;
}

The !important declaration is the highest priority in CSS. It overrides everything, including inline styles. On the flip side, when two rules both use !important, their specificity is compared to determine the winner Nothing fancy..

Important Rules to Remember

Rule 1: Specificity Trumps Order (Usually)

When selectors have equal specificity, the rule that appears last in the stylesheet wins. Even so, this only applies when specificities are equal Easy to understand, harder to ignore. But it adds up..

p {
  color: blue;
}
p {
  color: red; /* This wins because it comes last */
}

Rule 2: The Cascade Works Top-Down

Styles defined earlier in your stylesheet can be overridden by styles defined later, but only if the selectors have equal or lower specificity.

Rule 3: ID Selectors Are Powerful

One ID selector beats any number of class selectors. This selector:

#header

has higher specificity than:

.container .nav .menu-item a

Rule 4: Don't Chain IDs

There's no benefit to using multiple IDs in a selector:

### This is unnecessary and wasteful
#header #logo

This has the same specificity as just #logo, but it's harder to maintain and less efficient Worth keeping that in mind..

Rule 5: The !important Exception

Using !important should be a last resort. It breaks the natural cascade and makes debugging extremely difficult. If you find yourself using it frequently, your CSS architecture likely needs improvement.

Common Scenarios and Examples

Scenario 1: Link Styles

a {
  color: blue; /* (0, 0, 1) */
}
a:hover {
  color: red; /* (0, 1, 1) - wins because of pseudo-class */
}

Scenario 2: Navigation Menus

.nav-link {
  color: white; /* (0, 1, 0) */
}
.nav-link.active {
  color: yellow; /* (0, 2, 0) - wins */
}

Scenario 3: Overriding Inline Styles

/* This won't work to override inline styles */
p {
  color: red !important;
}

Actually, this will work because !important has the highest priority, even over inline styles That's the whole idea..

Frequently Asked Questions

Does the order in my stylesheet matter?

Yes, but only when specificities are equal. If you have two rules with the same specificity, the one that appears later in the stylesheet will apply Easy to understand, harder to ignore..

Why is my class not overriding an element selector?

It should. If your class selector isn't working, check for specificity conflicts. There might be a more specific selector targeting the same element.

Should I use IDs or classes for styling?

Classes are generally preferred because they can be reused and have moderate specificity. IDs have very high specificity, which makes them difficult to override when needed.

What happens when two !important rules conflict?

When both rules use !important, the browser falls back to comparing their specificities. The rule with higher specificity wins.

Can I calculate specificity for complex selectors?

Yes. Count the IDs (a), classes and pseudo-classes (b), and elements and pseudo-elements (c). Compare them from left to right. Here's one way to look at it: (1, 2, 1) beats (1, 1, 10) And it works..

Conclusion

Understanding which CSS rules override others is fundamental to writing effective stylesheets. The cascade and specificity work together to determine the final appearance of every element on your webpage.

Remember these key takeaways:

  • Specificity is calculated as (IDs, classes, elements)
  • Higher specificity always wins, regardless of order
  • IDs beat classes, classes beat elements
  • Use !important sparingly and as a last resort
  • Plan your specificity intentionally to avoid conflicts

By understanding these principles, you can write cleaner, more predictable CSS and troubleshoot styling issues quickly. On top of that, the more you work with CSS, the more intuitive specificity calculations become. Keep practicing, and soon you'll be able to predict exactly which style will win without even thinking about it And that's really what it comes down to. Simple as that..

What Just Dropped

Just Went Online

Cut from the Same Cloth

A Few More for You

Thank you for reading about Which One Of The Following Overrides The Others. We hope the information has been useful. Feel free to contact us if you have any questions. See you next time — don't forget to bookmark!
⌂ Back to Home