Enforced inheritance
suggest changeSome properties are not automatically inherited by an element’s children. That is because those properties are usually meant to apply only to the element they are set on. Common examples are margin, padding, background, and display.
Sometimes inheritance is wanted anyway. The inherit keyword can be applied to any CSS property. It uses the parent’s computed value.
Assume the following stylesheet:
#myContainer {
color: red;
padding: 5px;
}
#myContainer p {
padding: inherit;
}
This will apply color: red to both the <h3> and <p> elements due to the inheritance nature of the color property. However, the <p> element will also inherit the padding value from its parent because this was specified.
<div id="myContainer">
<h3>Some header</h3>
<p>Some paragraph</p>
</div>
Found a mistake? Have a question or improvement idea?
Let me know.
Table Of Contents