Problem: How to change the background color of the parent container when hovering over the child element using only CSS?
Compatibility:
Steps:
div { pointer-events: none; }
div:hover { background: #F00; }
div > a { pointer-events: auto; }
Explanation:
When the child element is hovered, the parent div is also hovered because of the pointer-events property. However, the parent's hover pseudo-class is ignored due to the pointer-events: none setting. By enabling pointer events on the child, the hover event triggers solely on the child, causing the parent's background color to change as desired.
Note: In IE 11 and Edge, the child element must have display: inline-block or display: block for pointer events to function properly.
The above is the detailed content of How to Change a Parent's Background Color on Child Hover with CSS?. For more information, please follow other related articles on the PHP Chinese website!