CSS Inheritance: Why Color Carries Over but Padding Stays on the Parent
CSS inheritance lets a child element take a computed property value from its parent. Set color on a container and text inside it uses that color unless a declaration on the child supplies another value. padding and border stay on the parent box by default.
Which CSS properties inherit?
Each CSS property defines whether it inherits by default. color, font-family, font-size, and line-height do. margin, padding, border, and background-color do not. A transparent child can reveal its parent’s background without inheriting its background color.
Inheritance passes the computed value, so a child does not recalculate the parent’s original units. If a parent’s font-size is 1.25rem and the root font size is 16px, a child without its own font size inherits 20px.
Compare inheritance inside one parent
Save this complete document in UTF-8, then open the file in your browser. Use the filename css-inheritance-example.html.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Compare inheritance inside one parent</title>
<style>
.panel {
color: #a61b1b;
border: 1px solid #bcbcbc;
padding: 1rem;
}
.panel .same-color { color: inherit; }
</style>
</head>
<body>
<div class="panel">
<h1>Heading</h1>
<p>A paragraph with <em>emphasized text</em></p>
<button type="button">Default button</button>
<button type="button" class="same-color">Inherited color</button>
</div>
</body>
</html>
The heading, paragraph, and emphasized text use the parent’s red color. The outer panel alone has the gray border and inner padding. The space around the heading or paragraph can come from default margins supplied by the browser.
Why a button can use a different color
A built-in button can have its own color in the browser’s style sheet. The second button opts into the parent color with color: inherit. Scope the selector to the component or states that should share that color.
Inspect both buttons in DevTools and compare the computed color. Add the same-color class to the first button and check that the two colors match. Remove the class to restore the original browser styling.
Choose between inherit, initial, and unset
| Value | What it does |
|---|---|
inherit |
Takes the parent’s computed value, even for a property that does not inherit by default. |
initial |
Uses the CSS property’s initial value, which can differ from the browser’s default styling for a particular element. |
unset |
Acts like inherit for an inherited property and like initial for other properties. |
Try adding .panel p { padding: inherit; } to the example. Now both the parent and its paragraph have padding. Use inherit selectively when the two boxes should share the property value.
W3C definition of the CSS-wide defaulting keywords
Find where the value came from
Find the property in the Computed panel in DevTools. Check whether the winning declaration applies to the element itself or whether the browser received the value from its parent. A declaration on the child supplies its value before inheritance is needed. Increasing the specificity of the parent’s rule alone will not override it.
To see how the browser resolves competing declarations on the same element, read CSS inheritance, specificity, and the cascade.




