<div class="commentList">
<article class="comment " id="com21"></article>
<article class="comment " id="com20"></article>
<article class="comment " id="com19"></article>
<div class="something"> hello </div>
</div>
I want to select #com19
?
.comment {
width:470px;
border-bottom:1px dotted #f0f0f0;
margin-bottom:10px;
}
.comment:last-child {
border-bottom:none;
margin-bottom:0;
}
That does not work as long as I do have another div.something
as actual last child in the commentList. Is it possible to use the last-child selector in this case to select the last appearance of article.comment
?
Something that I think should be commented here that worked for me:
Use
:last-child
multiple times in the places needed so that it always gets the last of the last.Take this for example:
What about this solution?
I guess that the most correct answer is: Use
:nth-child
(or, in this specific case, its counterpart:nth-last-child
). Most only know this selector by its first argument to grab a range of items based on a calculation with n, but it can also take a second argument "of [any CSS selector]".Your scenario could be solved with this selector:
.commentList .comment:nth-last-child(1 of .comment)
But being technically correct doesn't mean you can use it, though, because this selector is as of now only implemented in Safari.
For further reading:
If you are floating the elements you can reverse the order
i.e.
float: right;
instead offloat: left;
And then use this method to select the first-child of a class.
This is actually applying the class to the LAST instance only because it's now in reversed order.
Here is a working example for you:
:last-child
only works when the element in question is the last child of the container, not the last of a specific type of element. For that, you want:last-of-type
http://jsfiddle.net/C23g6/3/
As per @BoltClock's comment, this is only checking for the last
article
element, not the last element with the class of.comment
.html
css