I'm trying to select the first element in a list which doesn't have the class="disabled". Anyone know how to pull this off?
Asked
Active
Viewed 1,758 times
1
-
1Without the HTML we can't possibly comment sensibly – Paulie_D Dec 01 '15 at 23:27
-
1@Paulie_D why? The question is perfectly clear and valid without sample HTML. No clue why this question is getting downvotes and close votes. – Niels Keurentjes Dec 01 '15 at 23:36
-
My questions are angel crack. – ProfessorManhattan Dec 01 '15 at 23:42
3 Answers
2
This is sadly not possible directly in CSS, due to the way selectors are organized.
You can however hack around it by using both a positive and a negative selector, first selecting all non-disabled items and styling them, and then using the general sibling selector ~
to unstyle the others:
.disabled {
background:red;
color:white;
}
li:not(.disabled):after {
content:" (THIS IS THE FIRST NON DISABLED ITEM)";
}
li:not(.disabled) ~ li:not(.disabled):after {
content:"";
}
<ul>
<li class="disabled">Item</li>
<li class="disabled">Item</li>
<li>Item</li>
<li class="disabled">Item</li>
<li>Item</li>
<li class="disabled">Item</li>
<li>Item</li>
</ul>

Niels Keurentjes
- 41,402
- 9
- 98
- 136
1
This should do the tricks, full credits to BoltClock♦
li:not(.disabled) {
/* Every li without class .disabled, including the first */
background: red;
}
li:not(.disabled) ~ li:not(.disabled) {
/* Revert above declarations for every such element after the first */
background: none;
}
<ul>
<li class="disabled">disabled</li>
<li class="disabled">disabled</li>
<li class="not-disabled">not-disabled</li>
<li class="disabled">disabled</li>
<li class="disabled">disabled</li>
<li class="disabled">disabled</li>
<li class="disabled">disabled</li>
<li class="not-disabled">not-disabled</li>
</ul>

Community
- 1
- 1

Mathijs Rutgers
- 785
- 4
- 20
0
Try this code:
li:first-child:not(.disabled),
li.disabled + li:not(.disabled) {
/* ...your code. */
}

Jonathan Zúñiga
- 645
- 3
- 13
- 25