CSS/basics
Inheritance
Inheritance lets certain CSS properties automatically pass from a parent element to its children, reducing repetition. Not all properties inherit by default — mostly text-relate...
3 min readcss#css#inheritance
Introduction
Inheritance lets certain CSS properties automatically pass from a parent element to its children, reducing repetition. Not all properties inherit by default — mostly text-related ones do.
Subtopics
- Inherited vs. non-inherited properties
- Controlling inheritance:
inherit,initial,unset,revert - The cascade and how inheritance fits into it
Syntax
css
body {
font-family: Arial, sans-serif;
color: #333;
}
/* p and span inherit font-family and color automatically */
.child {
color: inherit; /* explicitly inherit from parent */
}
.reset {
all: initial; /* reset to browser default, ignoring inheritance */
}
.flexible {
border: unset; /* removes any set/inherited value */
}
inherit— forces a property to take its parent's computed value, even if it doesn't inherit by default (e.g.,border).initial— resets the property to the CSS specification's default value.unset— acts asinheritif the property naturally inherits, orinitialif it doesn't.revert— resets to the browser's built-in stylesheet value.
Important Properties
| Keyword | Purpose | Syntax | Example |
|---|---|---|---|
inherit | Force inherit from parent | prop: inherit; | border: inherit; |
initial | Reset to spec default | prop: initial; | color: initial; |
unset | Smart reset | prop: unset; | margin: unset; |
revert | Reset to browser default | prop: revert; | display: revert; |
all | Applies to every property at once | all: value; | all: unset; |
Common Use Cases
- Beginner: setting
font-familyonce onbodyso all text inherits it. - Practical UI: a button component using
color: inheritto match surrounding text color. - Real-world: a design-system reset using
all: unseton<button>to strip browser defaults before applying custom styles.
Common Errors
- ❌ Expecting
borderto inherit automatically → ✅borderis non-inherited by default; explicitly setborder: inherit;if needed. - ❌
.box { all: initial; }on a component expecting inherited fonts to remain → ✅ useall: revert;or set fonts explicitly afterward. - ❌ Assuming
width/heightinherit → ✅ box-model properties do not inherit; set them explicitly per element. - ❌
color: inherit;on<a>expected to look like plain text but link still shows default blue/underline → ✅ also resettext-decoration: inherit;since links have separate defaults. - ❌ Confusing
unsetwithinitialand getting unexpected inherited values → ✅ check whether the property inherits by default before choosingunsetvsinitial.
Common Mistakes
- Assuming every property inherits (only text/typography-related ones mostly do).
- Overusing
!importantinstead of relying on natural inheritance. - Not resetting inherited link/button styles when needed.
Quick Reference
css
body { color: #333; font-family: sans-serif; } /* inherited by children */
.child { color: inherit; }
.reset-all { all: unset; }
Inherited by default: color, font-*, line-height, text-align, visibility.
Not inherited: margin, padding, border, width, height, background.
LB
Author
Lakshya Bansal