Using the :last-child Selector

Description

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

Example

The following code shows the :last-child selector.


<!DOCTYPE HTML>
<html>
<head>
<style type="text/css">
:last-child {<!--   w w w.  j  av a2  s.c o  m-->
  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.





















Home »
  HTML CSS »
    CSS »




CSS Introduction
CSS Background
CSS Border
CSS Box Layout
CSS Text
CSS Font
CSS Form
CSS List
CSS Selectors
CSS Others
CSS Table