Welcome to dtsn.co.uk, if your new here please subscribe to my feed
14/04/2008

Of 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.

Promote
Jos Hirth

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.

Daniel

Hi Jos,

I totally agree with you, no one can seem to get this right, i wouldn’t recommend anyone using the CSS3 structural pseudo-classes, unless they are using prototype (which supports them all). I just thought i would draw peoples attention to them, they are powerful, and maybe one day they will be fully cross-browser compatable.

Comment