Working on a current project I discovered a bug in Safari's[foot]Perhaps a WebKit bug, but I haven't tested older versions of Chrome. There's no bug in Chrome as of version 15.0.874.121. If you are aware of the root of this bug, and its implications to Chrome, please
let me know.[/foot] implementation of
nth-child
. It seems to be fixed in version 5.1, so this only affects older Safari versions. I was unable to find this specific bug documented easily, so I've done so here.[foot]If you know of other places it's documented, especially the Safari or WebKit bug report, please
let me know.[/foot]
The
W3 spec for
nth-child
allows for whitespace in certain places:
Whitespace is permitted after the "(", before the ")", and on either side of the "+" or "-" that separates the an
and b
parts when both are present.
So these are valid:
:nth-child( 3n + 1 )
:nth-child( +3n - 2 )
:nth-child( -n+ 6)
:nth-child( +6 )
While these are not:[foot]Valid and invalid examples from the aforementioned W3 specification.[/foot]
:nth-child(3 n)
:nth-child(+ 2n)
:nth-child(+ 2)
The code I was working with was:
li:nth-child(4n + 4)
That should be valid, right? Well, Safari versions less than 5.1[foot]Tested in 5.0.3 and 4.1.3[/foot] ignored the applied styles until the whitespace was removed:
li:nth-child(4n+4)
Moral: for backwards compatibility, it's best to not use whitespace in
nth-child
expressions.