Teddy
Teddy

Reputation: 4223

CSS selector with nth-child works but not last-child

I have a div, which contains 3 child divs.

I have a selector .parent-div .child-div:nth-child(1) which selects the first child div.

And .parent-div .child-div:nth-child(3) selects the last child div.

But, .parent-div .child-div:last-child selects nothing.

Any clues on what could be the issue? Does it have anything to do with float or absolute positioning?

Incidentally, I made the divs sortable using JQuery UI, which might have added some additional classes.

Edit: The .parent-div has 3 .childA-div's and one .child-clear div. So, the last .childA-div is not considered as a last-child because the real last child is of a different class.

I used nth-last-child(2) as suggested below.

Upvotes: 2

Views: 1060

Answers (1)

Sudharsan S
Sudharsan S

Reputation: 15393

use .child-div:nth-last-child(1) { }

The :nth-last-child(n) selector matches every element that is the nth child, regardless of type, of its parent, counting from the last child.

Upvotes: 2

Related Questions