The Lodge is members-only design/dev videos and Office Hours.

Next Office Hours Session: "Implementing an SVG Icon System" Nov 30 - 6:00 PM Eastern


Last updated on:

The :last-of-type selector allows you to target the last occurence of an element within its container. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling content.

Suppose we have an article with a title, several paragraphs and an image:

  <h1>A Title</h1>
  <p>Paragraph 1.</p>
  <p>Paragraph 2.</p>
  <p>Paragraph 3.</p>
  <img src="...">

We want to make the last paragraph smaller, to act as a conclusion to the content (like an editor's note). Instead of giving it a class, we can use :last-of-type to select it:

p:last-of-type {
  font-size: 0.75em;

Using :last-of-type is very similar to :nth-child but with one critical difference: it is less specific. In the example above, if we had used p:nth-last-child(1), nothing would happen because the paragraph is not the last child of its parent (the <article>). This reveals the power of :last-of-type: it targets a particular type of element in a particular arrangement with relation to similar siblings, not all siblings.

The more complete example below demonstrates the use of :last-of-type and a related pseudo-class selector, :first-of-type.

Check out this Pen!

Other Resources

Browser Support

Chrome Safari Firefox Opera IE Android iOS
Works 3.2+ Works 9.5+ 9+ Works Works

:last-of-type was introduced in CSS Selectors Module 3, which means old versions of browsers do not support it. However, modern browser support is impeccable, and the new pseudo-selectors are widely used in production environments. If you require older browser support, either polyfill for IE, or use these selectors in non-critical ways á la progressive enhancement, which is recommended.


  1. Brian FitzGerald
    Permalink to comment#

    I find it rather odd that: .myClass:last-of-type does not work, yet .myClass:first-of-type does work! I wonder why they didn’t keep this consistent (Chrome version 28.0)

    • Brian FitzGerald
      Permalink to comment#

      Odd – I’m tripping! This does actually seem to work as expected. Not sure what was causing my earlier issue.

    • Tiffany Tse
      Permalink to comment#

      I am having the same issue. Are you sure it was a mistake? Or perhaps it was a bug…

    • Pan Stav
      Permalink to comment#

      Same here..
      Chrome 31.0.1612

    • Marcos
      Permalink to comment#

      “last-of-type” only refers to the element, not the attributes …

  2. Miguel

    Marcos is right about last-of-type in Chrome it doesn’t work when I refer to a class. Thanks for that!

  3. Robert

    Thanks! I had a shady memory of this CSS-“trick” and had to check your site. Result! :)

  4. Paul Sanders
    Permalink to comment#

    It looks like these *-of-type selectors can be disrupted (in Chrome at least) by dynamically generated elements inserted into the parent container. For instance, if some js code inserts a div into the parent, your original last-of-type will lose track of the element to which it was attached.

  5. Sten Hougaard
    Permalink to comment#

    Too bad that “last-of-type” is not actually implemented correctly.

    A type should be what is specified before the :last-of-type, so if I have “.January” as the “type”, the “.January:last-of-type” should hit the last occurrence elements with the class “.January“, which it does not (using Chrome 39.0.2171.95 (64-bit), OS X).

  6. David
    Permalink to comment#

    Just started a web design class and I am having the same problem. How do I fix it?

  7. Alfonse
    Permalink to comment#

    So I tested this out and apparently it ONLY applies to HTML elements and not by class-names unfortunately.

    In the spec it states:

    The :last-of-type CSS pseudo-class represents the last sibling with the given element name in the list of children of its parent element.

    So doing something like this won’t work:

    .foo:last-of-type {color: red;}

    But doing this will work:

    p:last-of-type {color:red;}
  8. Neha
    Permalink to comment#

    Below worked for me:

    margin: 20px 13px 0px 0px;
    .PFourTiles:last-of-type {
    margin: 20px 0px 0px 0px;

  9. Kristina
    Permalink to comment#

    Question, is it possible to create link that shows the lastest img from the latest article in one specific category?

    I want to have one picture in my header that will change and my readers have the possibility to read more if they are interested.

    For example: I have several categories and I want a preview on my index page but only the first picture from the latest article in one of those categories..

    hard to explain but several bloggers use that type of preview right now and I want that as well, but I don’t use wordpress so I have to find a way to solve that problem..

    please help!

    (,,, – you get the picture..)

    kind regards Kristina

Leave a Comment

Posting Code

We highly encourage you to post problematic HTML/CSS/JavaScript over on CodePen and include the link in your post. It's much easier to see, understand, and help with when you do that.

Markdown is supported, so you can write inline code like `<div>this</div>` or multiline blocks of code in in triple backtick fences like this:

  function example() {
    element.innerHTML = "<div>code</div>";

There's a whole bunch of content on CSS-Tricks.

Search for Stuff   •   Browse the Archives

Get the Newsletter ... or get the RSS feed