-1

When hovering over the span I would like the H1 element to turn blue. However, it does not. What am I doing wrong?

span:hover {
  background-color: red;
  color: white;
}
span:hover h1 {
  color: blue;
}
<span>HOVER</span>
<h1>test</h1>
Esko
  • 3,838
  • 2
  • 18
  • 36
Paul Stanley
  • 1,785
  • 19
  • 48

3 Answers3

3

The H1 is not a child of the span it's a sibling so you need the sibling combinator + https://developer.mozilla.org/en-US/docs/Web/CSS/Adjacent_sibling_combinator

span:hover {
  background-color: red;
  color: white;
}
span:hover + h1 {
  color: blue;
}
<span>HOVER</span>
<h1>test</h1>
JHeth
  • 4,879
  • 2
  • 20
  • 27
2

Try this..

Check this for what does the “+” (plus sign) CSS selector mean?

span:hover {
  background-color: red;
  color: white;
}
span:hover + h1 {
  color: blue;
}
<span>HOVER</span>
<h1>test</h1>
user3733831
  • 2,775
  • 7
  • 28
  • 60
1

Inorder to style the adjecent sibling you have to use + like span:hover + h1

span:hover {
  background-color: red;
  color: white;
}

span:hover + h1 {
  color: blue;
}
<span>HOVER</span>
<h1>test</h1>

span:hover h1 tries to find h1 node inside the span while its in hover state. This will be working when your layout is in below format only.

span:hover {
  background-color: red;
  color: white;
}

span:hover h1 {
  color: blue;
}
<span>
  HOVER
  <h1>test</h1>
</span>

Refer this document for much more understanding regarding css selectors.

Nitheesh
  • 17,055
  • 2
  • 18
  • 45