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
- Related to: first-child, last-of-type, :first-of-type
- Mozilla Docs
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.
312312312312
tha a gud site lol
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.
Hi Natalie. U got a nice smile.
LOL
@Eddy, quit creepin’, this isn’t FB.
Hi Chris, does ‘last-child’ supports for list also, i mean to li:last-child?
I have tried but not working.
Yes you can use it on list:
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.
Is this correct, too?
You’ll have to use the * selector, to tell the browser to consider all of the elements:
#my-container > *:last-child {
…
}
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.
Can confirm also, works fine without the universal selector.
Oh great that * selector worked for me. Thanks to @Rico Mossesgeld
Nice article, thanks!
I use more li. how can change particular li (ex 3rd li )
You can use the nth-child() selector
your li:nth-child() {your style}
just put the number of the li, ex: nth-child(3)
I’m having a problem in applying :last-child
Please see http://codepen.io/anon/pen/ZYaqgp
I want to remove the last bottom border in the widget area. Deactivating the second code block and activating the last – with the :last-child code – still removes all widget borders in that area.
Any suggestions please?
.home-middle-right > .entry:last-child
123412341234
Applying all anchor in ul instead last anchor
.loginNav li a:last-child { border-left:0;}
I am trying to put an endmark after the last paragraph inside the content in WordPress. Within the content I have a blockquote that WP automatically puts paragraphs inside. The p:last-child mark is being displayed inside the last paragraph of the blockquote instead of the last paragraph of the main content. Can’t figure out how to make it skip the paragraphs inside the blockquote.
Hey Trishah! This function should prevent WordPress from auto-adding paragraph tags:
Careful, though, because this will remove paragraphs from the entire content. Here’s the WordPress reference for the filter:
https://codex.wordpress.org/Function_Reference/wpautop#Disabling_the_filter
Hi Geoff. Thank you for the tip. I can see where your solution could really change the workflow when using the Visual Editor.
After thinking about this more, I devised a CSS solution that works. As the endmark was being placed inside the last paragraph of the section AND the last paragraph of the blockquote, I only needed to stop it inside the blockquote like this:
.content p:last-child::after {
font-family: 'Font Awesome 5 Pro';
content: '\f06c';
margin:0 0 0 15px;
color: #009acc;
}
.content blockquote p:last-child::after {
content: '';
}
Thanks again!
Awesome, and nice solution!
How to use pseudo-classes on the last li element.
What is the least complicated way to select the
last-child
but only if its not theonly-child
?In other words, is there a selector simpler than
:last-child:not(only-child)
?