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)
.
:last-child { style properties }
:last-child |
Yes | 9.0 | Yes | Yes | Yes |
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>
The body element is the last child of the html
element and is matched by the selector.