I am struggling with the following code:
HTML:
<ul class="list">
<li class="list-item"> item 1 </li>
<li class="list-item list-item_active"> Active item </li>
<li class="list-item"> item 3 </li>
<li class="list-item"> item 4 </li>
<li class="list-item"> item 5 </li>
</ul>
CSS:
.list-item:nth-child(even) {
background: #eee
}
.list-item_active {
background: none;
}
Check out the jsfiddle link - http://jsfiddle.net/Re3xV/2/
Let's assume that .list-item
does not have any child elements that can be styled.
Issue : I need .list-item_active
to take precedence over .list-item:nth-child(even)
I am trying to figure out which of the following solutions is the fastest in terms of selector performance:
- ul .list-item_active
- li.list-item_active
- .list .list-item_active
- .list-item.list-item_active
- .list-item_active:nth-child(n)
- .list-item:nth-child(even):not(.list-item_active)
- .list-item[data-state="active"] (data-state="active" should be added to HTML)
I am considering using
.list-item_active {
background: none !important;
}
since it does not seem to impact performance significantly (although I am aware that using !important
is generally not recommended). However, I still want to determine which selector is the most efficient. Is there a way to automate such comparisons?