CSS Selector :last-child

The :last-child selector selects elements that are the last elements defined by their parent element.

p:last-child is equal to p:nth-last-child(1).


CSS Version

CSS Syntax

:last-child {
   style properties 

Browser compatibility

:last-child Yes 9.0 Yes Yes Yes


The following code shows the :last-child selector.

<style type="text/css">
:last-child {<!--   www .ja  v  a  2 s  .c  om-->
  border: thin black solid;
  padding: 4px;
  <a href="http://java2s.com">Visit the website</a>
    I like <span>HTML</span> and <span>CSS</span>.
  <a href="http://w3c.org">Visit the W3C website</a>

Click to view the demo

The body element is the last child of the html element and is matched by the selector.