This content originally appeared on DEV Community and was authored by Shefali
CSS can be frustrating, especially when your styles don’t show up the way you expect. You write the code, but colors, fonts, or layouts look off, and you’re left scratching your head. What went wrong?
Most of the time, it’s because inheritance, specificity, and the cascade aren’t clear. Once you get these basics down, you’ll write cleaner, more predictable CSS and finally stop struggling with styling.
In this post, you’ll learn how inheritance, specificity, and the cascade work together to decide which styles actually get applied, with easy examples and tips you can start using today.
Before we get started, don’t forget to subscribe to my newsletter!
Get the latest tips, tools, and resources to level up your web development skills delivered straight to your inbox. Subscribe here!
Now, let’s jump right into it!
CSS Inheritance: What Gets Passed Down?
Inheritance means some CSS properties pass down from parent elements to their child elements.
Mostly, this happens with text styles like color, font-family, font-size, and line-height, etc.
Example:
body {
color: blue;
}
p {
/* p will be blue because it inherits from body */
}
Note: Not all CSS properties inherit. If you want to force inheritance for a property, then you can use:
p {
background: inherit;
}
Why it matters
Inheritance helps you write less code by applying styles to parent elements and letting children automatically follow. It keeps CSS clean and DRY (Don’t Repeat Yourself).
CSS Specificity: Who Wins When Styles Clash?
When multiple CSS rules target the same element, specificity decides which one actually applies.
Specificity Levels:
Selector Type | Power | Example |
---|---|---|
Inline Styles | ![]() ![]() ![]() ![]() |
<div style="color:red"> |
ID Selectors | ![]() ![]() ![]() |
#header {} |
Class / Attributes / Pseudo-classes | ![]() ![]() |
.box {}, a:hover {} |
Element Tags / Pseudo-elements | ![]() |
p {}, ::before {} |
Example:
If you write:
<h1 id="title" class="title">Hello</h1>
#title {
color: red; /* strong */
}
.title {
color: green; /* medium */
}
h1 {
color: blue; /* weak */
}
The text will be red because the ID selector wins.
Important tips:
- If two selectors are equal → last one wins.
Example:
.title {
color: red;
}
.title {
color: green;
}
The color will be green.
-
!important
will always win.
Example:
.title {
color: red !important;
}
.title {
color: green;
}
The color will be red.
Why it matters
Without understanding specificity, your CSS can behave unpredictably, and you’ll wonder why your styles don’t apply as expected.
CSS Cascade: The Final Decision Maker
The Cascade decides the final CSS by checking:
- Important rules (
!important
first) - Source type (browser defaults, user styles, your CSS)
- Specificity (strongest selector wins)
- Order in code (last rule wins if there’s a tie)
Example:
p {
color: green; /* normal */
}
p {
color: red !important; /* important */
}
The color will be red because !important
takes priority.
Why it matters
The cascade is what makes CSS stand for “Cascading Style Sheets”. It decides which rule actually gets applied when there are conflicts. Without it, browsers wouldn’t know which style to show.
Quick Summary
Topic | Simple Meaning | Why Important? |
---|---|---|
Inheritance | Child elements get parent styles | Saves time |
Specificity | Stronger selector wins | Solves rule conflicts |
Cascade | Final decision on styles | Controls rule order & priority |
That’s all for today!
For paid collaboration connect with me at : connect@shefali.dev
If you enjoy my work and want to support what I do:
Become a Patreon supporter
Or buy me a coffee
Every small gesture keeps me going!
This content originally appeared on DEV Community and was authored by Shefali