Select the first & last element with a class
Do you want to select the first and last element with a specific class? It's possible and you can do it using 2 different methods!
/* First element with class "cat" */
.cat:not(.cat ~ *) {}
/* OR */
:nth-child(1 of .cat) {}
/* Last element with class "bird" */
.bird:not(:has(~ .bird)) {}
/* OR */
:nth-last-child(1 of .bird) {}
See the Pen Untitled by Temani Afif (@t_afif) on CodePen.
More CSS Tips
- A fancy hover effect with rotating squares Transform your image into little rotating squares with a fancy hover effect.
- A CSS-only Ribbon A simple code to create a fancy CSS Ribbon.
- CSS-only custom range slider Use CSS mask to create a border-only tooltip with a gradient coloration.
- A color wheel with gradient Use the new color interpolation to create a color wheel using conic-gradient().