CSS Portal

:last-child CSS Pseudo Class

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!

Description

The :last-child pseudo-class in CSS is used to select an element that is the last child of its parent. It allows developers to target the final element within a container without needing to add extra classes or identifiers. This pseudo-class is particularly useful when styling lists, tables, or any group of elements where the final item requires distinct styling, such as removing a border, adding spacing, or applying a unique color.

Unlike combinators, :last-child works solely based on the element's position relative to its parent. For example, in a list of items, you can style only the last item differently while leaving the rest unaffected. It's important to note that it considers all types of child elements, so if the last child is a different element type than expected, it may not apply styles as intended. In such cases, combining it with a type selector can ensure specificity. For instance, li:last-child will select only the last <li> inside a list, ignoring any other element types that may appear last.

This pseudo-class can also be combined with other selectors and pseudo-classes to achieve more refined control. For example, using :nth-child() alongside :last-child allows conditional targeting within more complex structures. Additionally, it often works hand-in-hand with properties like margin, padding, and border to adjust spacing and visual separation for the last element.

Here’s a simple example using :last-child:

<ul>
  <li>Item 1</li>
  <li>Item 2</li>
  <li>Item 3</li>
</ul>
ul li:last-child {
  color: red;
  font-weight: bold;
  border-bottom: none;
}

In this example, only the last <li> ("Item 3") will appear in red and bold, and any bottom border applied to other list items would be removed. This demonstrates how :last-child can simplify styling patterns without additional markup.

It’s a versatile pseudo-class that greatly enhances CSS’s ability to style elements based on their structural context within the DOM, making layouts more maintainable and semantically clear.

Syntax

:last-child {
  /* ... */
}

Example

<ul class="container">
<li>First Item</li>
<li>Second Item</li>
<li>Third Item</li>
<li>Last Item</li>
</ul>
/* Styling for all list items */
li {
padding: 15px;
list-style: none;
background-color: #f4f4f4;
border-bottom: 2px solid #333; /* Adds a line under every item */
}

/* Targeting only the very last list item */
li:last-child {
border-bottom: none; /* Removes the line from the last item */
background-color: #e0e0e0; /* Changes color to make it stand out */
font-weight: bold;
}

Browser Support

The following information will show you the current browser support for the CSS :last-child pseudo class. Hover over a browser icon to see the version that first introduced support for this CSS psuedo class.

This psuedo class is supported by all modern browsers.
Desktop
Chrome
Edge
Firefox
Opera
Safari
Tablets & Mobile
Chrome Android
Firefox Android
Opera Android
Safari iOS
Samsung Internet
Android WebView
-

Last updated by CSSPortal on: 31st December 2025

If this site has been useful, we’d love your support! Consider buying us a coffee to keep things going strong!