0

How when I hover on the <li>I can select my <a>element ?

a ul:hover {
  color: red;
}
<a href="#">Link</a>
<ul>
  <li>Element 1</li>
  <li>Element 2</li>
</ul>
j08691
  • 204,283
  • 31
  • 260
  • 272
F__M
  • 1,518
  • 1
  • 19
  • 34

2 Answers2

1

You can do this using :has like this:

a:has(+ ul:hover) {
  color: red;
}
<a href="#">Link</a>
<ul>
  <li>Element 1</li>
  <li>Element 2</li>
</ul>
j08691
  • 204,283
  • 31
  • 260
  • 272
0

For those browsers which support :has you can use:

<style>
  a:has(+ ul li:hover) {
    background: red;
  }
</style>
<a href="#">Link</a>
<ul>
  <li>Element 1</li>
  <li>Element 2</li>
</ul>

However, check on caniuse.com that there is enough support for your use case - Edge/Chrome/Safari support it but Firefox requires a flag to be set.

A Haworth
  • 30,908
  • 4
  • 11
  • 14