Sub topic 3
Inheritance: Which Properties Pass Down and Why
Assume you know basic HTML nesting, CSS selectors, and how to inspect an element in browser developer tools. Inheritance is the process by which some CSS values pass from a parent element to its descendants.
Which Properties Inherit
Each element gets a value for every CSS property. If no declaration wins for an inherited property, the element usually receives its parent’s computed value.
Text-related properties such as color, font-family, and line-height inherit. Box properties such as margin, padding, border, and width do not. This prevents one element’s layout from unexpectedly changing all its descendants.
CSS-Wide Keywords
These keywords let you control a property’s value deliberately:
inheritcopies the parent’s value, even for a normally non-inherited property.initialresets the property to its specification-defined initial value.unsetacts likeinheritfor inherited properties and likeinitialfor other properties.revertrolls back the declaration to a value from an earlier cascade origin, such as the browser’s default style.revert-layerrolls back to the previous cascade layer.allapplies a keyword to almost every property at once, exceptdirectionandunicode-bidi.
button {
font: inherit;
color: inherit;
border: initial;
}
.card {
all: unset;
}Use unset for broad cleanup. Use revert when you want browser or user styles restored. Avoid all unless you also restore the properties your component needs.
Custom Properties: Inheritance You Control
A custom property is a CSS variable whose name begins with --. Custom properties inherit by default, so a descendant can use a value supplied by an ancestor.
:root { --gap: 1rem; }
.compact { --gap: 0.25rem; }
.stack > * + * {
margin-top: var(--gap);
}A .stack inside .compact uses the smaller gap. var(--gap, 1rem) provides a fallback if the property is missing or invalid.
Debugging with Developer Tools
Inspect the unexpected element and open the Styles panel. Crossed-out declarations lost in the cascade. Look for a more specific selector, a later rule, a layer, or !important.
Then open Computed. It shows the final value and often identifies the rule that supplied it. Compare the element with its parent to determine whether the value was inherited. Check custom properties on each ancestor when var() produces an unexpected result.
Exercise
A paragraph has no color declaration, but its parent has color: navy. What color does the paragraph use, and how could you force it to use the browser default?
Show answer
It inherits navy. Apply color: revert to roll back to an earlier cascade origin.