Home > Web Front-end > CSS Tutorial > How Can I Style a Child Element When Its Parent is Hovered?

How Can I Style a Child Element When Its Parent is Hovered?

Patricia Arquette
Release: 2024-11-29 13:11:10
Original
987 people have browsed it

How Can I Style a Child Element When Its Parent is Hovered?

Styling Child Elements on Parent Hover

Need to modify a child element's appearance when hovering over its parent? Enter CSS's mighty :hover selector.

To do this:

.parent:hover .child {
  /* Styling for the child element when the parent is hovered */
}
Copy after login

With this selector, you're targeting the child element when the parent, ".parent," is hovered. The descendant combinator (space) selects descendants matching ".child."

For instance:

.parent:hover .child {
  background-color: #ccc;
}
Copy after login

This turns the child element's background gray when hovering over the parent.

Live example:

<div class="parent">
  <p class="child">Hover me</p>
</div>
Copy after login
.parent {
  border: 1px dashed gray;
  padding: 1em;
  display: inline-block;
}

.child {
  background-color: white;
  padding: 0.5em;
  transition: all 0.5s;
}

.parent:hover .child {
  background-color: #ccc;
  transform: scale(1.1);
}
Copy after login

This example changes the child element's background to gray and slightly scales it up on parent hover.

Remember, your browser support is covered here. Dive into CSS and enhance your user experience!

The above is the detailed content of How Can I Style a Child Element When Its Parent is Hovered?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template