-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
  • 4,109
  • 2
  • 22
  • 37
Paul Stanley
  • 1,999
  • 1
  • 22
  • 60

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
  • 7,067
  • 2
  • 23
  • 34
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,886
  • 9
  • 36
  • 68
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
  • 19,238
  • 3
  • 22
  • 49