CSS Portal

:last-of-type 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-of-type pseudo-class in CSS is used to select an element that is the last sibling of its type within its parent container. Unlike the :first-child pseudo-class, which targets the very first child regardless of type, :last-of-type specifically considers the element’s type, allowing for more precise selection when multiple types of elements exist as siblings. This makes it especially useful in scenarios where you want to style the final occurrence of a specific element without affecting other types.

For example, if you have multiple paragraphs inside a div and only want to style the last paragraph differently, :last-of-type provides a clean solution. It can be combined with various CSS properties like margin, color, or border to achieve distinct visual effects.

Another common use of :last-of-type is in lists. For instance, in an unordered list (ul), you might want the last li item to have no bottom border or extra padding. Using :last-of-type ensures that only the final item of that type is affected, leaving the rest of the list intact.

Here is a practical example:

<ul>
  <li>Item One</li>
  <li>Item Two</li>
  <li>Item Three</li>
</ul>
li:last-of-type {
  font-weight: bold;
  color: red;
}

In this example, only "Item Three," the last li in the list, will be bold and red. Any additional elements of a different type within the same ul will not be affected.

The :last-of-type pseudo-class is also powerful when combined with combinators and other pseudo-classes, allowing developers to create complex and highly specific styling rules without adding extra classes or IDs. This helps maintain semantic and clean HTML structure while still achieving the desired visual styling.

Syntax

:last-of-type {
  /* ... */
}

Example

<div class="container">
<h2>My Journal</h2>
<p>This is the first paragraph.</p>
<p>This is the second paragraph.</p>
<p>This is the third paragraph (the last of its type).</p>
<img src="images/2.png" alt="Ending icon">
</div>
/* Target only the last paragraph in the container */
p:last-of-type {
background-color: #e8f4fd;
border-left: 5px solid #2196f3;
padding: 10px;
font-weight: bold;
}

/* Base styles for readability */
.container {
font-family: sans-serif;
line-height: 1.6;
max-width: 400px;
margin: 20px;
}

Browser Support

The following information will show you the current browser support for the CSS :last-of-type 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!