Change Background Color of Parent Container on Child Hover (CSS Only)
While the question about selecting parent elements with CSS is often marked as a duplicate, it overlooks the need for practical solutions. In particular, the issue of changing the background color of a parent container when hovering over its child can be addressed through a CSS-only approach.
Pointer-Events and Hover:
To achieve this effect, we can manipulate pointer events and the :hover pseudo-class:
Example:
div { height: 200px; width: 200px; text-align: center; pointer-events: none; } div:hover { background: #F00; } div > a { pointer-events: auto; }
This solution effectively captures the hover event on the child element, allowing the parent container's background to change when the child is hovered, all without using JavaScript.
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3