CSS last-child Selector :last-child

What is CSS :last-child Selector?

CSS :last-child selector matches only last child element of its parent element.

Syntax

General syntax of :last-child selector,

:last-child {
  property: value;
  property: value;
  ...
}

li:last-child {
  property: value;
  property: value;
  ...
}

Example

CSS :last-child selector select only last <li> element of its <ul> parent element.

<!DOCTYPE html>
<html>
<head>
  <title>CSS :last-child Selector</title>
  <style type="text/css">
    li:last-child {
      background:#ff0000;
    }
  </style>
</head>
<body>
  <ul>
    <li>first item</li>
    <li>second item</li>
    <li>third item</li>
    <li>forth item</li>
    <li>fifth item</li>
  </ul>
</body>
</html>

Run it...   »

Browser Compatibility

  • Google Chrome 2+
  • Mozilla Firefox 3+
  • Internet Explorer 9+
  • Opera 9.5+
  • Safari 4+

Note: Here details of browser compatibility with version number may be this is bug and not supported. But recommended to always use latest Web browser.