CSS :last-child Selector

The CSS :last-child selector targets the last child element within its parent. It applies styles exclusively to the last child element based on its position in the parent container, regardless of the element type or class.

Syntax:

:last-child {
//property
}

Example: In this example we applies limegreen background, white color, italic font-style, and 1.875em font-size to the last <p> element, “I am last child.

HTML
<!DOCTYPE html>
<html>

<head>
    <style>
        p:last-child {
            background: limegreen;
            color: white;
            font-style: italic;
            font-size: 1.875em;
        }
    </style>
</head>

<body>

    <p>I am first child.</p>
    <p>I am second child.</p>
    <p>I am third child.</p>
    <p>I am last child.</p>

</body>

</html>

Output:

Supported Browsers: