Let’s say I have the following HTML:
<ul>
<li id="1">First issue.</li>
<li id="2" class="error">Second issue.</li>
<li id="3" class="error">Third issue.</li>
<li id="4">Fourth issue.</li>
<li id="5" class="error">Fifth issue.</li>
<li id="6" class="error">Sixth issue.</li>
<li id="7" class="error">Seventh issue.</li>
<li id="8" class="error">Eighth issue.</li>
<li id="9">Ninth issue.</li>
<li id="10" class="error">Tenth issue.</li>
</ul>
And the following CSS:
.error {
background-color: rgba(255, 0, 0, 0.2);
}
What I’m looking to do is to style the first and last <li> in a sequence styled different than other <li>s that are in the middle.
For example, #2, #5, #10 should all match the “first in sequence selector”; #3, #8, #10 should be matched by the “last in sequence selector.”
I’d like to apply the following styles to “first-in-sequence” and “last-in-sequence”, respectively:
.firstInSequence {
border-radius-top-right: 10px;
border-radius-top-left: 10px;
}
.lastInSequence {
border-radius-bottom-right: 10px;
border-radius-bottom-left: 10px;
}
Are there selectors I could use to do this natively in CSS?
As stated in the other answers, you can style the ‘first of sequence’ using the
:notand:first-childpseudo selector:I think I’ve found a solution for ‘last of sequence’ in this scenario with rounded corners. It uses a completely different method of accomplishing the finished product, but ends with a similar result:
The basic technique is to apply pseudo-elements with
radial-gradientsto list items that are not.error.after.errors, and move them up over the previous (.error) list item:Make sure
lihasposition:relative;.Finally, for a
border-radiuson the last one, uselast-of-type:Demo (Webkit only for simplicity)