CSS Basic
CSS :nth-last-child() Selector
Photo Credit to CodeToFun
🙋 Introduction
The :nth-last-child()
selector in CSS allows you to select one or more elements based on their position from the end within a parent.
It’s a flexible pseudo-class that lets you apply styles to specific elements by counting from the last child element instead of the first.
This selector is especially useful when you need to style elements dynamically based on their reverse order.
💡 Syntax
The signature of the :nth-last-child()
Selector is as follows:
element:nth-last-child(n) {
/* CSS properties */
}
- element: Any valid HTML element.
- n: A number, keyword, or formula representing which child to target counting from the last.
Possible values for n:
- A number (
n
): Selects the nth child from the last. - A formula (
2n
): Selects every 2nd element from the last. - Keywords (
odd
,even
): Selects odd or even children from the last.
📝 Example
Let’s explore how to use the :nth-last-child()
selector in CSS.
☠️ HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS :nth-last-child() Selector Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<ul>
<li>Item 1</li>
<li>Item 2</li>
<li>Item 3</li>
<li>Item 4</li>
<li>Item 5</li>
</ul>
</body>
</html>
🎨 CSS
/* Style the last list item */
li:nth-last-child(1) {
color: red;
}
/* Style every second list item from the last */
li:nth-last-child(2n) {
background-color: lightblue;
}
In this example:
- The last list item (
Item 5
) will have red text. - Every second list item from the end (starting with
Item 4
) will have a light blue background.
💬 Usage Tips
- The
:nth-last-child()
selector is great for situations where you want to apply styles in reverse order without rearranging the HTML structure. - You can combine it with other pseudo-classes like
:first-child
,:nth-child()
, or:nth-of-type()
for even more specific targeting.
⚠️ Common Pitfalls
- Counting starts from the last child: Remember that the counting starts from the last child in reverse, which can sometimes be confusing if you are more accustomed to working from the first child.
- Empty text nodes: Be careful with empty text nodes or comments between elements. These may sometimes be considered child nodes, which can interfere with the expected results.
- Element types: This selector targets all elements unless you specify a type. For example, if you only want to target
li
elements, make sure to includeli:nth-last-child()
instead of just:nth-last-child()
.
🎉 Conclusion
The :nth-last-child()
selector offers powerful and flexible ways to style elements based on their position from the end within a parent. It can be especially useful when styling lists, tables, and other structured content where reverse order targeting is needed.
By mastering this selector, you can achieve cleaner and more efficient stylesheets.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (CSS :nth-last-child() Selector), please comment here. I will help you immediately.