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>
<html>
  <body>

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

  </body>
</html>

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

(source)

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.