Grow your CSS skills. Land your dream job.

:last-child

Last updated on:

The :last-child selector allows you to target the last element directly inside its containing element. 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 and want to make the last paragraph smaller, to act as a conclusion to the content (like an editor's note):

<article>
  <p>Lorem ipsum...</p>
  <p>Dolor sit amet...</p>
  <p>Consectetur adipisicing...</p>
  <p>Last paragraph...</p>
</article>

Instead of using a class (e.g. .last), we can use :last-child to select it:

p:last-child {
  font-size: 0.75em;
}

Using :last-child is very similar to :last-of-type but with one critical difference: it is less specific. :last-child will only try to match the very last child of a parent element, while last-of-type will match the last occurrence of a specified element, even if it doesn't come dead last in the HTML. In the example above the outcome would be the same, only because the last child of the article also happens to be the last p element. This reveals the power of :last-child: it can identify an element with relation to all of its siblings, not just siblings of the same type.

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

Check out this Pen!

Other Resources

Browser Support

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

:last-child 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.

Comments

  1. Permalink to comment#

    312312312312

  2. Permalink to comment#

    tha a gud site lol

  3. Doesn’t work if the elements have display property set to none. It still considers the hidden element as the last child. I guess technically that is correct, but unintended in many cases.

  4. Ramesh Chowdarapally
    Permalink to comment#

    Hi Chris, does ‘last-child’ supports for list also, i mean to li:last-child?
    I have tried but not working.

    • Nicolas
      Permalink to comment#

      Yes you can use it on list:

      `   ul.page-nav li:last-child {
          border-bottom: 0px;
          }`
      
  5. Karl
    Permalink to comment#

    Do I necessarily need to specify a selector before :last-child?
    Maybe I don’t know if the last element is a paragraph, a div container, or whatever.

    #my-container > :last-child {
        ...
    }
    

    Is this correct, too?

    • Rico Mossesgeld
      Permalink to comment#

      You’ll have to use the * selector, to tell the browser to consider all of the elements:

      #my-container > *:last-child {

      }

  6. Karl
    Permalink to comment#

    According to this answer on Stack Overflow, the universal selector is not necessary. In the meantime, I tried it on different sites and I can confirm that it works perfectly without the universal selector.

  7. Oh great that * selector worked for me. Thanks to @Rico Mossesgeld

  8. Miloš
    Permalink to comment#

    Nice article, thanks!

  9. manirathinam
    Permalink to comment#

    I use more li. how can change particular li (ex 3rd li )

    • Nico
      Permalink to comment#

      You can use the nth-child() selector

      your li:nth-child() {your style}

      just put the number of the li, ex: nth-child(3)

Leave a Comment

Posting Code

Markdown is supported in the comment area, so you can write inline code in backticks like `this` or multiline blocks of code in in triple backtick fences like this:

```
<div>Example code</div>
```

You don't need to escape code in backticks, Markdown does that for you. If anything screws up, contact us and we can fix it up for you.

*May or may not contain any actual "CSS" or "Tricks".