thelinh bui
thelinh bui

Reputation: 226

css child selector select the last div

<body>
    <div>content 1</div>
    <div>content 2</div>
    <div>content 3</div>
</body>

how do i select the last tag without using any id or class. I have already tried:

body:last-child {
        clear: both;
        background-color:red;
}

Please help

Thank you

but it's this is css3 so the browser (ipad safari) does not recoginsed it.

Upvotes: 2

Views: 4443

Answers (3)

Federico
Federico

Reputation: 3892

XXXX:last-child means that XXXX is the last child. So:

table tr:last-child { ... }

is applied to the last tr of the table.

In your case:

body > :last-child { ... }

works because > select for you the children of body, then you select the last one with :last-child

Upvotes: 0

Musa
Musa

Reputation: 97672

body:last-child selects a body tag that is the last child of its parent, try

body > :last-child

Upvotes: 7

Rob W
Rob W

Reputation: 348992

Use the child selector (>):

body > :last-child {
    clear: both;
    background-color:red;
}

Upvotes: 5

Related Questions