CSS3 Selectors :last-child
Filed under CSS/HTMLOf all the CSS3 selectors the structural pseudo-classes have to be my favorite. In particular I love :last-child. The :last-child CSS selector is increabily powerful especially when positioning elements in a row. Instead of providing a last class on the very last element in the row, you can now use pure CSS to achieve the same effect.
CSS 2.1
/*Each Element*
.image { margin-right: 5px; height: 20px; width: 20px; float:left; background-color: red; }
/* remove the margin on the last element so it lines up
.image.last { margin-right: 0px; }
CSS 3
/* parent of the image element */
.parent div:last-child { margin-right: 0px; }
The Downside
Unfortunatly the bad aspect of this selector is that it does not fail gracefully. Meaning that a user in a none CSS3 complient browser (IE) it will fail horribly for, this means that everything will be out of line.
Example
If you look at this in anything but Firefox the last element will be on the next line.



Unfortunately :last-child (and all similar pseudo class selectors) are broken in Firefox for everything than the most simple kind of test cases. The reason for that is that those rules are applied whenever the page is drawn. This unfortunately includes partial still-loading renderings. And well, the final rendering step doesn’t fix that.
See: http://kaioa.com/node/39
Fortunately it’s more usable over in Opera, but even Opera has some issues with that stuff if the DOM is manipulated via JS.
Maybe Safari gets it right. Right now it isn’t really usable even if you ignore IE completely.
April 14th, 2008 at 6:41 pm