Reputation: 3335
I have a list like this and I want it to change the font-weight to bold of the list item which I was hovering but instead of that it applies the changes for the whole list of all lists of my document (depends on my attempts)
li:hover {
cursor: pointer;
font-weight: bold;
}
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="2.css">
</head>
<body>
<ul class="tree" id="tree">
<li>Animals
<ul>
<li>Mammals
<ul>
<li>Cows</li>
<li>Donkeys</li>
<li>Dogs</li>
<li>Tigers</li>
</ul>
</li>
<li>Other
<ul>
<li>Snakes</li>
<li>Birds</li>
<li>Lizards</li>
</ul>
</li>
</ul>
</li>
<li>Fishes
<ul>
<li>Aquarium
<ul>
<li>Guppy</li>
<li>Angelfish</li>
</ul>
</li>
<li>Sea
<ul>
<li>Sea trout</li>
</ul>
</li>
</ul>
</li>
</ul>
</body>
</html>
I want to know is there a way to do it without adding IDs or classes to the elements.
And I want it to do in a dynamic way.
If there's a way to do it with JS it will be great too.
Upvotes: 2
Views: 1030
Reputation: 78850
Here's an approach that seems to work. To explain:
li
elements are explicitly given a font-weight: normal
so that font-weight: bold
on parents doesn't cascade.mouseover
event handler is placed on the top level to handle all descendent events. The true
useCapture
parameter for addEventListener
allows us to do this.li
target.font-weight
for the li
li
elements aren't notified when a descendent has already been boldedconst tree = document.getElementById('tree');
tree.addEventListener('mouseover', (event) => {
if (event.target.tagName === 'LI') {
event.target.style.fontWeight = 'bold';
event.stopPropagation();
}
}, true);
tree.addEventListener('mouseout', (event) => {
if (event.target.tagName === 'LI') {
event.target.style.removeProperty('font-weight');
}
}, true);
#tree li {
cursor: pointer;
font-weight: normal;
}
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="2.css">
</head>
<body>
<ul class="tree" id="tree">
<li>Animals
<ul>
<li>Mammals
<ul>
<li>Cows</li>
<li>Donkeys</li>
<li>Dogs</li>
<li>Tigers</li>
</ul>
</li>
<li>Other
<ul>
<li>Snakes</li>
<li>Birds</li>
<li>Lizards</li>
</ul>
</li>
</ul>
</li>
<li>Fishes
<ul>
<li>Aquarium
<ul>
<li>Guppy</li>
<li>Angelfish</li>
</ul>
</li>
<li>Sea
<ul>
<li>Sea trout</li>
</ul>
</li>
</ul>
</li>
</ul>
</body>
</html>
Upvotes: 2
Reputation: 31
Rest all is fine, just try this CSS code:
ul li ul li ul li:hover {
/* Here write your styling code */
}
I hope that this will help.
Upvotes: 1
Reputation: 71
ul li ul li ul li:hover {
cursor: pointer;
font-weight: bold;
}
Replace your styling with this and you'll be good to go.
Upvotes: 0
Reputation: 665
Following @Armin Guearmazi's answer, this should kinda work:
ul li ul li ul li:hover,
ul li ul li:hover,
ul li:hover {
cursor: pointer;
font-weight: bold;
}
ul li ul li:hover ul,
ul li:hover li,
ul li:hover ul {
cursor: normal;
font-weight: normal;
}
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<link rel="stylesheet" href="2.css">
</head>
<body>
<ul class="tree" id="tree">
<li>Animals
<ul>
<li>Mammals
<ul>
<li>Cows</li>
<li>Donkeys</li>
<li>Dogs</li>
<li>Tigers</li>
</ul>
</li>
<li>Other
<ul>
<li>Snakes</li>
<li>Birds</li>
<li>Lizards</li>
</ul>
</li>
</ul>
</li>
<li>Fishes
<ul>
<li>Aquarium
<ul>
<li>Guppy</li>
<li>Angelfish</li>
</ul>
</li>
<li>Sea
<ul>
<li>Sea trout</li>
</ul>
</li>
</ul>
</li>
</ul>
</body>
</html>
Upvotes: 2
Reputation: 1722
You can use css element selectors to select some tags contained in another one. Its syntax is very simple:
ul li ul li ul li:hover {
/*Your style here*/
}
See a demo
Upvotes: 0