1

My scenario is I want the hover effects of the parent element to be applied to the child elements, effectively over-riding styling settings of the children, and I want to this to be strictly CSS. I can do it easily enough with javascript, but is there a pure CSS approach? Thanks for any suggestions.

div {
}

div:hover {
    background-color: red;
    /*change contained span backgound-color to red as well*/
}

span {
    background-color: yellow;
}
<div>When I mouse over this div, I want the <span>the contained span background</span> to take on the hover settings of the parent div.</div>
s.kuznetsov
  • 14,870
  • 3
  • 10
  • 25
Len White
  • 892
  • 13
  • 25

2 Answers2

1

If I understand your question correctly, then add another hover specifying all-child elements as *, applying the inheritance rule.

div:hover * {
    background-color: inherit;
}

Snippet:

div {
}

div:hover {
    background-color: red;
    /*change contained span backgound-color to red as well*/
}

div:hover * {
    background-color: inherit;
}

span {
    background-color: yellow;
}
<div>When I mouse over this div, I want the <span>the contained span background</span> to take on the hover settings of the parent div.</div>
s.kuznetsov
  • 14,870
  • 3
  • 10
  • 25
0

div {
}



span {
    background-color: yellow;
}

div:hover span {
    background-color: red;
    /*change contained span backgound-color to red as well*/
    color:#fff;
}
<div>When I mouse over this div, I want the <span>the contained span background</span> to take on the hover settings of the parent div.</div>