How do I select the last nth child in CSS?
Table of Contents
CSS :nth-last-child() Selector
- Specify a background color for every
element that is the second child of its parent, counting from the last child: p:nth-last-child(2) {
- Odd and even are keywords that can be used to match child elements whose index is odd or even.
- Using a formula (an + b).
How do you target the last child in CSS?
CSS :last-child Selector

- Definition and Usage. The :last-child selector matches every element that is the last child of its parent. Tip: p:last-child is equal to p:nth-last-child(1).
- Browser Support. The numbers in the table specifies the first browser version that fully supports the selector.
- CSS Syntax. :last-child {
What is last child in CSS?
The :last-child CSS pseudo-class represents the last element among a group of sibling elements. /* Selects any
that is the last element among its siblings */ p:last-child { color: lime; } Note: As originally defined, the selected element had to have a parent.
Why last child is not working?

Pseudo-selectors like :last-child or nth-child or nth-of-type only select based on elements not classes or attributes. Either something is the last-child or it isn’t. If it’s the last element in a parent it will be selected. So, in your case, it’s testing for it to meet all the conditions.
How do you target your first and last child in CSS?
Using the following pseudo classes: :first-child means “select this element if it is the first child of its parent”. :last-child means “select this element if it is the last child of its parent”. Only element nodes (HTML tags) are affected, these pseudo-classes ignore text nodes.
What is the difference between last child and last of type?
The Last type of selector is used to match the last occurrence of an element within the container. Both selectors work in the same way but have a slight difference i.e the last type is less specified than the last-child selector. Example: This example implements the :last-child selector.
What is first child and last child in CSS?
The :first-child pseudo class means “if this element is the first child of its parent”. :last-child means “if this element is the last child of its parent”.
What is Last-of-type in CSS?
The :last-of-type CSS pseudo-class represents the last element of its type among a group of sibling elements. Note: As originally defined, the selected element had to have a parent. Beginning with Selectors Level 4, this is no longer required.