Home >Web Front-end >CSS Tutorial >How Can I Style Child Elements When Their Parent Element is Hovered?
Adjusting Child Element Styles on Parent Element Hover
You can alter the style of a child element when the parent element is hovered using CSS. To achieve this, employ the :hover pseudoclass.
.parent:hover .child { /* Styles applied here will affect child elements when the parent is hovered */ }
This rule selects all child elements within a parent element that is currently hovered. The styles applied within the curly braces will be applied to the child elements only when the parent element is hovered.
For instance, if you need to change the color of a drop-down menu when the parent menu item is hovered, you can use the following CSS:
.menu-item:hover .drop-down-menu { background-color: #ccc; }
This would change the background color of the drop-down menu to light gray when the corresponding menu item is hovered, providing a visual cue to the user.
The above is the detailed content of How Can I Style Child Elements When Their Parent Element is Hovered?. For more information, please follow other related articles on the PHP Chinese website!