CSS Basic
CSS :nth-last-of-type() Selector
Photo Credit to CodeToFun
🙋 Introduction
The :nth-last-of-type()
selector in CSS allows you to select elements based on their position within their parent, counting from the last element of that type.
This selector is useful when you need to target specific elements, starting from the end of a group of sibling elements.
💡 Syntax
The signature of the :nth-last-of-type()
Selector is as follows:
element:nth-last-of-type(n) {
/* CSS properties */
}
- element: This is the type of HTML element you want to target, such as div, p, li, etc.
- n: A number, keyword, or formula used to determine the element's position from the last.
Values for n
- odd or even: Targets odd or even elements.
- A specific number (e.g., 2): Targets the element at the nth position, starting from the last.
- A formula (e.g., 2n+1): Targets every nth element in a sequence, starting from the last.
📝 Example
Here is an example of how to use the :nth-last-of-type()
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-of-type() 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-of-type(1) {
background-color: lightblue;
}
/* Style the second last list item */
li:nth-last-of-type(2) {
background-color: lightgreen;
}
/* Style every second list item from the end */
li:nth-last-of-type(2n) {
font-weight: bold;
}
In this example:
- The last
<li>
is styled with a light blue background. - The second-to-last
<li>
is styled with a light green background. - Every second list item from the end is bold.
💬 Usage Tips
- The
:nth-last-of-type()
selector counts elements of a specific type, starting from the last element in a group of sibling elements. It only applies to elements of the same type (e.g., all<li>
tags). - Combine
:nth-last-of-type()
with other pseudo-classes or attributes for more specific styling. - The formula (
2n
,2n+1
, etc.) can be useful for alternating styles.
⚠️ Common Pitfalls
- Only targets specific element types: This selector only applies to elements of a specific type. For example, if you have a mix of
<p>
and<div>
elements, it will only count within the same element type, not across different types. - Zero-based index: Remember that
n
starts counting from 1, not 0, when using specific numbers like1
,2
, etc. - Complexity with formulas: Using formulas like
2n+1
might become confusing, especially when combined with other pseudo-classes. Test thoroughly to ensure it targets the correct elements.
🎉 Conclusion
The :nth-last-of-type()
selector is a flexible and powerful tool for selecting elements based on their position from the end of a group. It can simplify complex designs where you need to target specific elements in reverse order.
By understanding how to use this selector, you can create efficient, concise, and clean styles that enhance the visual structure of your webpage.
👨💻 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-of-type() Selector), please comment here. I will help you immediately.