Reputation: 1204
I know a bunch of the pseudo classes (first-child, last-child, nth-child) but I am having trouble selecting the first 2 children in a list or the last 2, the list is dynamic and changes all the time so i cant target based on counting the li's
<ul>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
<li></li>
</ul>
Upvotes: 34
Views: 40042
Reputation: 1428
For the first two children you can use:
ul li:nth-child(-n + 2) {
color: orange;
}
For the last two:
ul li:nth-last-child(-n + 2) {
color: orange;
}
Upvotes: 75