1

I've been banging my head against this for a couple hours now. Maybe it can't be done, maybe I've missed something obvious.

I need to highlight the li I'm hovering over, and each li after that but not before, and only if I'm also hovering over a specific child of that li.

So in the code example, if I hover over the second li's div.doAThing, the second through to last li should highlight. I'm not sure how to get the selection working.

li
{
  background-color: #3e3e3e;
}

li:hover, li:hover ~ li
{
  background-color: #aaf500;
}
<ul id="theList">
  <li>
    <div>Data</div>
    <div>One</div>
    <div class='doAThing'>All</div>
  </li>
  <li>
    <div>Data</div>
    <div>One</div>
    <div class='doAThing'>All</div>
  </li>
  <li>
    <div>Data</div>
    <div>One</div>
    <div class='doAThing'>All</div>
  </li>
  <li>
    <div>Data</div>
    <div>One</div>
    <div class='doAThing'>All</div>
  </li>
</ul>
Christopher Roy
  • 286
  • 2
  • 3
  • 12

2 Answers2

1

As far I know, it can't be done with only CSS. You need some way to target the parent, which isn't really possible with only CSS. I don't know if your able to use JavaScript/jQuery in your project, but I've put together an example of how you could do it. Basically, when you hover a .doAThing element, we add a class hover to its parent (the li). And then we can easily target those specific elements in a similar way you did in the code you provided. Hope it helps. Please see the following SO links for further information:

Is there a CSS parent selector?

Complex CSS selector for parent of active child

$('.doAThing').hover(function() {
  $(this).parent().addClass('hover');
}, function () {
  $(this).parent().removeClass('hover');
});
li {
  background-color: #3e3e3e;
}
.hover, .hover ~ li {
  background-color: #aaf500;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<ul id="theList">
  <li>
    <div>Data</div>
    <div>One</div>
    <div class='doAThing'>All</div>
  </li>
  <li>
    <div>Data</div>
    <div>One</div>
    <div class='doAThing'>All</div>
  </li>
  <li>
    <div>Data</div>
    <div>One</div>
    <div class='doAThing'>All</div>
  </li>
  <li>
    <div>Data</div>
    <div>One</div>
    <div class='doAThing'>All</div>
  </li>
</ul>
Gustaf Gunér
  • 2,272
  • 4
  • 17
  • 23
0

I had the same issue, i just used child of

li:hover > * {
  font-size: 17px;
}

Hope it helps

Fernando B
  • 864
  • 2
  • 12
  • 27