Short answer: No, it is impossible to prevent CSS inheritance. You can only redefine styles that are set by parents. See Specification:
Each element of an HTML document inherits all inherited properties from its parent, except for the root element ( html ), which does not have a parent. - W3C
In addition to overriding each inherited property. You can also use the initial keyword. color: initial; . It can also be used with all , for example. all: initial; which will reset all properties at once. Example:
.container { color: blue; font-style: italic; } .initial { all: initial; }
<div class="container"> The quick brown <span class="initial">fox</span> jumps over the lazy dog </div>
Supported browser tables, depending on whether I can use ...
all (there is currently no support in both IE and Edge, others are good)initial (There is currently no support in IE, others are good)
In some cases, you may find this useful using direct children selector > . Example:
.list > li { border: 1px solid red; color: blue; }
<ul class="list"> <li> <span>HEADING 1</span> <ul> <li>sub-heading A</li> <li>sub-heading B</li> </ul> </li> <li> <span>HEADING 2</span> <ul> <li>sub-heading A</li> <li>sub-heading B</li> </ul> </li> </ul>
The border style applies only to direct <li> s children, since border is an inherited property. But the color of the text applies to all children, since color is an inherited property.
Consequently, the > selector will only be useful with non-inherited properties when it comes to preventing inheritance.
Stickers Jan 10 '17 at 4:27 2017-01-10 04:27
source share