The :last-child selector matches every element that is the last child of its parent. This example specifies a background color for the <p> element that is the last child of its parent:
HTML file: | Displayed by browser: |
---|---|
<style>
p:last-child { background: #ff0000; } </style> <body> <p>The first paragraph.</p> <p>The second paragraph.</p> <p>The third paragraph.</p> <p>The fourth paragraph.</p> </body> |
The first paragraph. The second paragraph. The third paragraph. The fourth paragraph. |
Note: This is equal to nth-last-child(1).