Skip to content

CSS :last-child selector

CSS has a selector called :last-child which will apply styles to the last matching element. Assume that you have this CSS and HTML code:

p:last-child {
    background: red;
<!DOCTYPE html>

    <p>The first paragraph.</p>
    <p>The second paragraph.</p>
    <p>The third paragraph.</p>
    <p>The fourth paragraph.</p>


In this example the last paragraph will get a red background.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.