Home >Web Front-end >CSS Tutorial >How to Prevent Inheritance of CSS Styles Inside a Specific Div?
Problem:
In the provided HTML code, the CSS styles applied to img and h1 elements are also impacting the elements within the #mydiv div. How can this inheritance be prevented?
Solution:
Using the all Shorthand Property:
CSS Level 3 introduces the all shorthand property, which allows you to reset all properties of an element. By setting all: initial on the #mydiv div, all styles inherited from its ancestors will be blocked.
To allow for styling within the #mydiv div, apply all: unset to its descendants. This will enable inheritance within the div while preventing external CSS from affecting it.
<code class="css">#mydiv { all: initial; } #mydiv * { all: unset; }</code>
Manually Setting Individual Properties:
For broader browser support, manually set all known CSS properties to initial for the #mydiv div and inherit or initial for its descendants as applicable. This simulates the behavior of the all property.
Additional Considerations:
The above is the detailed content of How to Prevent Inheritance of CSS Styles Inside a Specific Div?. For more information, please follow other related articles on the PHP Chinese website!