Please look at the code snippet below.
The structure is
<ul>
<li>text1</li>
<li> <ul><li> text2 </li>
<li> text3 </li>
</ul>
</li>
</ul>
I have the CSS as follows:
li:hover
{
background-color: yellow;
}
It works fine for the first li
but when I hover over the second item (in the sub-ul), then all items in the sub list are highlighted.
What I need is for a single row to be highlighted at a time, regardless of the relationship of the list item.
I tried
li:child-only:hover
but it did not work. All other answers on S.O. are approaching jQuery to handle this issue. Can this be solved using only CSS?
ul {
list-style-type: none;
}
li:hover {
color: blue;
cursor: pointer;
background-color: yellow;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<ul>
<li>Lv1: First Item</li>
<li>Lv1: Second Item</li>
<li>
<ul>
<li>Lv2: First Item</li>
<li>
<ul>
<li>Lv3: First Item</li>
<li>Lv3: Second Item</li>
<li>Lv3: Third Item</li>
</ul>
</li>
<li>Lv2: Second Item</li>
<li>Lv2: Third Item</li>
</ul>
</li>
</ul>