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).

Summary

CSS Version
3

CSS Syntax

:last-child {
   style properties 
}

Browser compatibility

:last-child Yes 9.0 Yes Yes Yes




Example

The following code shows the :last-child selector.

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

Click to view the demo

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