Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I prevent CSS inheritance?

Tags:

css

People also ask

How do you avoid inheritance in CSS?

If you really want to avoid inheritance, then all: revert has you covered. All divs will be display: block and spans will be inline . All em tags will be italic and strong tags will be bold.

How do you remove inherited property in CSS?

The unset CSS keyword resets a property to its inherited value if the property naturally inherits from its parent, and to its initial value if not.

What CSS properties are not inherited?

CSS properties such as height , width , border , margin , padding , etc. are not inherited.


You either use the child selector

So using

#parent > child

Will make only the first level children to have the styles applied. Unfortunately IE6 doesn't support the child selector.

Otherwise you can use

#parent child child

To set another specific styles to children that are more than one level below.


There is a property called all in the CSS3 inheritance module. It works like this:

#sidebar ul li {
  all: initial;
}

As of 2016-12, all browsers but IE/Edge and Opera Mini support this property.


As of yet there are no parent selectors (or as Shaun Inman calls them, qualified selectors), so you will have to apply styles to the child list items to override the styles on the parent list items.

Cascading is sort of the whole point of Cascading Style Sheets, hence the name.


You can use the * selector to change the child styles back to the default

example

#parent {
    white-space: pre-wrap;
}

#parent * {
    white-space: initial;
}