CSS Reference ยท Custom Properties, Cascade and Global Keywords
The inherit keyword
Reference for the CSS inherit keyword: force a property to take its parent's computed value, even for non-inherited properties like border.
The inherit keyword makes a property take the computed value of the same property on the parent element. Some properties, such as color and font-family, already inherit by default. inherit is most useful for the others, such as border, background and padding, and for undoing a browser default that stops inheritance.
A classic case is links: browsers set their own color on <a> and <button>, so a { color: inherit } makes them match the surrounding text.
a { color: inherit; text-decoration: inherit; } .child { border: inherit; }
Try it
Syntax
property: inherit;
- Works on: every property
- Result: the parent's computed value for that property
- At the root element: there is no parent, so the property takes its initial value
- Pseudo-elements: inherit from their originating element (for
::before, the element itself)
Values
| Syntax | Description |
|---|---|
property: inherit |
Copies the parent's computed value, regardless of whether the property inherits by default. |
--custom: inherit |
Custom properties inherit anyway; this makes it explicit. |
Usage notes
- Computed, not specified. If the parent has
width: 50%, the child withwidth: inheritgets that percentage, resolved against the child's own containing block, not the parent's resulting pixel width. For relative units such asem, the already-computed absolute length is inherited. - Explicit beats default. For inherited properties (text color, font,
line-height,visibility), the keyword is useful only to override something else, like the browser's link color. - Non-inherited properties.
inheritonborder,background,margin,paddingordisplaycopies the parent's value, which is rarely what you want for layout. Use it sparingly. - Form controls.
font: inheritis a common fix so inputs and buttons use the page font instead of the system control font. - Compare:
initialignores the parent;unsetinherits only where the property normally inherits.
Browser support
Supported in all browsers.
Related
initial,unset,revert- Inheritance
all- Lessons: Inheritance, Global values