Inheritance
In CSS,inheritance controls what happens when no value is specified for a property on an element.
CSS properties can be categorized in two types:
- inherited properties, which by default are set to thecomputed value of the parent element
- non-inherited properties, which by default are set toinitial value of the property
Refer toany CSS property definition to see whether a specific property inherits by default ("Inherited: yes") or not ("Inherited: no").
In this article
Inherited properties
When no value for aninherited property has been specified on an element, the element gets thecomputed value of that property on its parent element. Only the root element of the document gets theinitial value given in the property's summary.
A typical example of an inherited property is thecolor property. Consider the following style rules and the markup:
p { color: green;}<p>This paragraph has <em>emphasized text</em> in it.</p>The words "emphasized text" will appear green, since theem element has inherited the value of thecolor property from thep element. It doesnot get the initial value of the property (which is the color that is used for the root element when the page specifies no color).
Non-inherited properties
When no value for anon-inherited property has been specified on an element, the element gets theinitial value of that property (as specified in the property's summary).
A typical example of a non-inherited property is theborder property. Consider the following style rules and the markup:
p { border: medium solid;}<p>This paragraph has <em>emphasized text</em> in it.</p>The words "emphasized text" will not have another border (since the initial value ofborder-style isnone).
Notes
Theinherit keyword allows authors to explicitly specify inheritance. It works on both inherited and non-inherited properties.
You can control inheritance for all properties at once using theall shorthand property, which applies its value to all properties. For example:
p { all: revert; font-size: 200%; font-weight: bold;}This reverts the style of the paragraphs'font property to the user agent's default unless a user stylesheet exists, in which case that is used instead. Then it doubles the font size and applies afont-weight of"bold".
Overriding inheritance, an example
Using our previous example withborder, if we explicitly set the inheritance withinherit, we get the following:
p { border: medium solid;}em { border: inherit;}<p>This paragraph has <em>emphasized text</em> in it.</p>We can see here another border around the word "emphasized text".
Specifications
| Specification |
|---|
| CSS Cascading and Inheritance Level 5> # css-inheritance> |
See also
- CSS values for controlling inheritance:
inherit,initial,revert,revert-layer, andunset - CSS error handling
- Introducing the CSS cascade
- Learn: Handling conflicts
- Learn: cascade layers
- CSS cascading and inheritance module
- CSS syntax guide
- CSS syntax module
- At-rules
- Initial,computed,used, andactual values
- Value definition syntax
- CSS nesting module