Values and Units in CSS : inherit, initial, unset,
Tiny CSS keywords can reset, copy, or undo styles with just one word.
nanadwumor

-
Keywords control styles without numbers
-
inherit copies, initial resets, unset smart reset
-
all applies to most properties, safer to limit
-
revert returns to browser/user defaults
RECOMMENDED ARTICLES
Values and Units in CSS : Keywords, Strings and other text values : Images
Did you know CSS can create images without using a single file? CSS uses URLs to display images. [crayon-68dfa53b89817981893504-i/] serves images by device quality. Gradients generate images with colors only. Useful for backgrounds, designs, and...
nth-of-type Pseudo-class
Learn how to use the nth-of-type() pseudo-class to style specific elements with precision and simplicity. The [crayon-68dfa53b89db3414812949-i/] pseudo-class selects elements by their position among siblings of the same type. It uses the formula...
Combining a class and pseudo-class
Can you combine class and pseudo-class to style elements? Learn how to precisely target and style elements with advanced pseudo-classes in CSS! Combining classes and pseudo-classes styles elements based on both their type and class, such as...
Units in CSS affect a number of properties. These include padding, margin etc
Keywords
Sometimes, you don’t want to use a number—you just want to give a simple instruction using a word, like none, which tells the browser not to show something. For example, if you don’t want underlines on your navigation links, you could write:
1 |
nav a { text-decoration: none; } |
On the other hand, if you do want underlines, you’d write:
1 |
nav a { text-decoration: underline; } |
Global Keywords: inherit, initial, unset
CSS has three special words that work with every property. Think of them as “magic commands” that you can use to control how styles behave:
inherit, initial, unset
Let’s break them down—starting with inherit.
inherit — Like a Hand-Me-Down
Imagine a parent wearing a blue shirt and white pants. If the child is told to wear what the parent is wearing, that’s what inherit does—it copies the style from the parent.
Normally, some styles like text color are automatically passed down (inherited), while others like borders are not. But using inherit, you can force any style to be passed down, even if it’s not by default.
Example
Let’s say you have a navigation bar styled like this:
1 2 3 4 |
.menu { background-color: green; color: yellow; } |
And your HTML looks like this:
1 2 3 4 5 |
<div class="menu"> <button>Home</button> <button>About</button> <button>Contact</button> </div> |
Here, the text inside the div should appear yellow, but the button elements might use the browser’s default colors (probably black text). That’s because buttons don’t automatically inherit text color.
To fix that, tell the buttons to copy the text color from their parent:
1 2 3 |
.menu button { color: inherit; } |
Now the buttons will display yellow text, just like their parent .menu.
Forcing Inheritance Where It Doesn’t Happen
Now let’s say you want a child element to copy a style that normally isn’t passed down like a border.
Borders don’t get inherited, but you can force it:
1 2 3 4 5 6 7 |
.alert { border: 2px solid red; } .alert span { border: inherit; } |
Now the <span> inside the .alert will get the same red border.
Or maybe you just want to reuse part of the border style, like the color:
1 2 3 |
.alert span { border-color: inherit; } |
This way, the <span> gets the same red color, but you can give it a different border width or style.
initial — Like Hitting the “Reset” Button
Think of initial like the “reset to factory settings” button. If you’ve ever reset a phone or a video game to its default settings, that’s exactly what initial does to a CSS style.
It tells the browser:
“Forget any styles I added here—just go back to how this property starts by default.”
Example :
Let’s say you made your paragraph text extra bold:
1 2 3 |
p { font-weight: bold; } |
Then later, you want a specific paragraph to go back to how it normally looks. You don’t need to remember what that default is—you can just reset it:
1 2 3 |
p.reset { font-weight: initial; } |
where .reset is a class that points to specific p elements. This makes the font weight normal again (which is the browser’s default for that property).
unset — The “Smart Reset”
Think of unset like a smart assistant that chooses between two actions:
If something is usually inherited (like text color), unset says:
“Okay, I’ll just copy from the parent.”
If it’s not usually inherited (like padding or borders), unset says:
“I’ll just go back to the default setting.”
So, unset is like a universal fixer. It automatically decides whether to use inherit or initial, depending on the situation.
Example:
Let’s say your site has a section styled like this:
1 2 3 4 5 |
.card { color: navy; margin: 30px; border: 1px solid gray; } |
Now you have a special box inside the card that you want to undo all styling for (but still keep inherited things like color when it makes sense):
1 2 3 |
.card .clean { all: unset; } |
And your HTML:
1 2 3 |
<div class="card"> <div class="clean">Reset styles here.</div> </div> |
This .clean box removes all applied styles except what naturally inherits, like color. It clears margins, borders, padding, fonts—everything—unless the property is normally inherited.
What is ‘all’ ?
all is a special keyword that applies your command (like unset or inherit) to almost every CSS property at once—except two special ones (direction and unicode-bidi).
Using:
1 2 3 |
.special-box { all: inherit; } |
…means:
“Copy every style from the parent element, not just a few.”
But that might be too much. Often, you only want to inherit a few styles like color and background, not layout or positioning.
So a better and safer version would be:
1 2 3 4 5 |
.special-box { color: inherit; background-color: inherit; font-weight: inherit; } |
This gives you more control and avoids weird side effects.
Bonus: revert (Upcoming Feature)
There was a new keyword being tested: revert.
It means:
“Ignore author styles (like yours) and go back to the browser’s or user’s defaults.”
Kind of like saying:
“Pretend the stylesheet never touched this element.”
Recent Comments