0
Note the HTML and CSS below.
This is the initial CSS
ul ul li:nth-last-of-type(2){
background: #ff0000;
}
<ul>
<li>bla
<ul>
<li>bla bla
<ul>
<li>bla bla bla</li>
<li>Gostaria que não fosse selecionado</li>
<li>bla bla bla</li>
</ul>
</li>
<li>Selecionado</li>
<li>bla bla</li>
</ul>
</li>
<li>bla
<ul>
<li>Selecionado</li>
<li>bla bla</li>
</ul>
</li>
<li>bla</li>
</ul>
The manual solution would be to do this
ul ul ul li:nth-last-of-type(2){
background: initial;
}
But I didn’t want to do it like that. Supposing I added to ul
deeper in the hierarchy the li
down below.
<li>
bla bla bla
<ul>
<li>Agora esse não deve mais ser selecionado</li>
<li>Bla bla bla bla</li>
</ul>
</li>
ul ul ul ul li:nth-last-of-type(2){
background: initial;
}
That is, this CSS is not sustainable. Every new node on ul
I will need to maintain CSS every time by adding a new ul
in the hierarchy to restart the property. How do I make it dynamic?
How about adding a class to those
li
different?– Lucas
I was gonna comment on what @Lucas said
– Thallyson Dias
What is the criteria for knowing which one is going to be different? It might be interesting to detail the ultimate goal, in addition to the way you’re trying to do, so you don’t fall into a XY problem.
– Bacco
So people, the criterion I tried to inform in the description. I always read the deeper in the hierarchy should not be selected. If I add it to HTML, I don’t want to have to touch CSS. You understand?
– rplaurindo