CSS :nth-last-child() Selector
The nth-last-child() selector in CSS is used for selecting elements based on their position among sibling elements, but starting the count from the end rather than the beginning. This selector is particularly useful for styling elements dynamically based on their order in reverse, allowing for flexible and responsive design solutions. Understanding how to effectively use the nth-last-child() selector can enhance your CSS skills and enable more precise control over the styling of your web pages.
Syntax and Usage
The nth-last-child()
selector takes a single argument that determines the pattern for matching elements from the end. This argument can be a number, keyword (odd or even), or a functional notation.
Syntax:
:nth-last-child(number) {
//CSS Property }
- number: The argument that specifies the pattern for matching elements. It can be a specific number,
odd
,even
, or a functional notation likeAn+B
.
Keywords:
- odd: Selects elements whose positions are odd numbers counting from the end (1, 3, 5, etc.).
- even: Selects elements whose positions are even numbers counting from the end (2, 4, 6, etc.).
Functional Notation:
- An+B: Selects elements whose position matches the pattern
An+B
from the end, whereA
andB
are integers. For every positive integer or zero value ofn
, the position of elements is determined.
Examples
Example 1: Selecting Every Odd Element
In this example, the formula 2n+1 is used to select every odd element counting from the end.
<!DOCTYPE html>
<html>
<head>
<title>CSS :nth-last-child Selector</title>
<style>
p:nth-last-child(2n+1) {
background: green;
color: white;
}
</style>
</head>
<body style="text-align:center;">
<h1 style="color:green;">
GeeksforGeeks
</h1>
<h2>
CSS :nth-last-child Selector
</h2>
<p>
A computer science portal for geeks.
</p>
<p>
Geeks classes an extensive classroom programme.
</p>
</body>
</html>
Output:
Example 2: Selecting Every Even Element
In this example, the formula 2n is used to select every even element counting from the end.
<!DOCTYPE html>
<html>
<head>
<title>CSS :nth-last-child Selector</title>
<style>
table {
border: 1px solid green;
margin: auto;
}
/* Selects the last three element */
tr:nth-last-child(3) {
background-color: green;
color: white;
}
</style>
</head>
<body style="text-align:center;">
<h1 style="color:green;">
GeeksforGeeks
</h1>
<h2>
CSS :nth-last-child Selector
</h2>
<table>
<tbody>
<tr>
<td>Merge sort</td>
</tr>
<tr>
<td>Quick sort</td>
</tr>
<tr>
<td>Insertion sort</td>
</tr>
<tr>
<td>Selection sort</td>
</tr>
</tbody>
</table>
</body>
</html>
Output:
The nth-last-child() selector in CSS allows developers to apply styles dynamically based on the reverse order of sibling elements. This capability is essential for creating sophisticated and responsive web designs. By leveraging this selector, you can enhance the flexibility and control of your CSS, leading to more maintainable and visually appealing web pages.
Supported Browsers: The browser supported by :nth-last-child selector are listed below:
- Apple Safari 3.1
- Google Chrome 4.0
- Edge 12.0
- Firefox 3.5
- Opera 9.0
CSS :nth-last-child() Selector – FAQs
What does the :nth-last-child() selector do in CSS?
The :nth-last-child() selector targets elements based on their position among siblings, counting from the last child instead of the first.
How do I style the third-to-last list item in a list?
Use the :nth-last-child() selector, like li:nth-last-child(3) { color: red; }, to style the third-to-last list item.
Can :nth-last-child() accept different formulas?
Yes, :nth-last-child() can accept different formulas, such as 2n for even children or 3n+1 for specific sequences.
Does :nth-last-child() work with all types of elements?
Yes, :nth-last-child() can target any type of element based on its position from the end within its parent.
Is the :nth-last-child() selector supported by all browsers?
Yes, the :nth-last-child() selector is supported by all modern browsers.