Changing Parent Container Background Color on Child Hover with CSS
The question of how to alter the parent element's background color when its child is hovered is common. Typically, this type of question is considered a duplicate of the inquiry regarding whether CSS supports parent selectors.
While it's true that CSS doesn't offer direct parent selectors, there are CSS solutions that can address this specific concern.
Using Pointer Events and :hover
This technique involves three steps:
How it Works:
Example:
div { height: 200px; width: 200px; text-align: center; pointer-events: none; } div:hover { background: #F00; } div > a { pointer-events: auto; display: inline-block; }
<div> <h1>Heading</h1> <a href="#">Anchor Text</a> </div>
This solution is compatible with browsers including IE 11, Edge, Chrome, and Firefox. For IE 11 and Edge, the child element must have display: inline-block or display: block to enable pointer events.
The above is the detailed content of How Can I Change a Parent's Background Color on Child Hover Using CSS?. For more information, please follow other related articles on the PHP Chinese website!