CSS nth-child selector basics

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Thanks Mazda, I tend to use them quite often (mainly the odd/even), but even the duplicate selectors come in handy.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Today we'll be going back to some CSS basics. I tend to use nth-child selectors in my articles.
But that made me realise I haven't really gone over the basics of using nth-child selectors.
Today we will be exploring the options of this powerful CSS selector and some examples.
Let's start off by using some basic selectors.
We can define which element we want to select. So lets say we want to select the third item.
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
li:nth-child(3) {
color: green;
}
Result:

We can select every odd or even code by using these attributes.
li:nth-child(odd) {
color: red;
}
Result:

li:nth-child(even) {
color: blue;
}
Result:

Something cool we do is select every x element, so let's say we want every fourth element:
li:nth-child(4n) {
color: purple;
}
Result:

Or if we want to include the first one:
li:nth-child(4n+1) {
color: purple;
}
Result:

We can also start from the second for instance:
li:nth-child(4n+2) {
color: purple;
}
Result:

We can even have a selector that states select everything but the first three elements.
li:nth-child(n+4) {
color: teal;
}
Result:

Another cool thing we can do it select only the first x amount. Let's get the first three
li:nth-child(-n+3) {
color: teal;
}
Result:

We can even select the last element.
li:last-child {
color: orange;
}
Result:

With this, we can also offset to get the second to last one.
li:nth-last-child(2) {
color: orange;
}
Result:

We can even combine selectors!
Let's say you want to get every even number from an odd list amount.
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
ul:nth-child(odd) li:nth-child(even) {
color: orange;
}
This will get all the odd ul and then all the even li in those.
Result:

Have a play around with this Codepen, try and change some selector to see what happens!
The nth-child selector has really good support and can be used in most of the browsers. Don't hesitate to make use of them.

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter