I want to give a zebra stripe effect to my table rows. In all other browsers it can be done using CSS nth child element. But i want to do it IE 8 also. SO how can i do it?
You can use http://selectivizr.com/ JS which support css3 selector for IE.
With polyfill : Selectivizr is good enough.
Without polyfill: As IE8 supports first-child you can trick this to support nth-child in iE8 i.e
/*li:nth-child(2)*/
li:first-child + li {}/*Works for IE8*/
Although we cannot emulate complex selectors i.e nth-child(2n+1) or nth-child(odd) for IE8.
As an alternative to Selectivizr, you can use a little jQuery:
$('.your-list li:nth-child(3n+1)').addClass('nth-child-3np1');
Then just add a second selector in your CSS:
:nth-child(3n+1)
{
clear: both;
}
.nth-child-3np1
{
clear: both;
}
You can use http://selectivizr.com/ JS which support css3 selector for IE.
:nth-child()
support, but it does not work in practice. –
Bookcase The 'first-child' and '+' selectors are both available in IE7, and the '+' is additive.
Therefore 'nth-child' can be simulated by successively adding '+' selectors, so:
tr:nth-child(4)
becomes:
tr:first-child + tr + tr + tr
If all sibling elements are the same, then the '*' wildcard will suffice, as in:
tr:first-child + * + * + *
which will reduce the css file size if specifying lots of rows.
Note that spaces between selectors are not required, so the file size can be reduced further by leaving them out, so to select the 1st, 3rd and 5th rows:
tr:first-child,tr:first-child+*+*,tr:first-child+*+*+*+*
Of course one would not want to cater for very large tables!
If using the *
as a filler, make sure the :first-child
element and the last element is given the explicit tagname, because the rule will be tested against every element in the DOM, and specifying both those elements explicitly forces failure at whichever end a particular browser applies its rules to first, rather than failing after it has had to step over several elements in every sequence to eventually fail the rule for each of them. Don't make your browser work for no good reason!
tr:nth-child(4)
would be equivalent to *:first-child + * + * + tr
. –
Pullulate tr
? –
Sternum *
instead of a tr
, and a final tr
instead of a *
, better, especially when the condition was that they were all tr
s anyway? –
Sternum tr
to ensure the rule applies, so any version with at least one tr
is equivalent. Basically, the first one is the best to specify explicitly, as the rule engine can have the opportunity to reject the rule applying to any particular instance early on. –
Sternum li
elements in a list, a bunch of p
s on a page, and it will have to get to the fourth element to eventually fail it. Put the most restrictive test first, which forces failure on the first element for each sequence that doesn't start with a tr
. –
Sternum *
instead of a tr
, and a final tr
instead of a *
, better" • Because that is actually how the :nth-child
pseudoclass works: it refers to the nth child of its parent regardless of the children's types. tr:nth-child(4)
doesn't mean the 4th tr
child; it means the 4th child, if it's of type tr
. –
Pullulate tr
tag name and using *
would be minimal, because tr
elements occur only in contexts where no other element is valid. But if you're going to use a selector, it's important to understand what it actually means... –
Pullulate tr
) basically did. However, if indeed browsers do evaluate RTL, then an explicit last is the best (I have seen a lot of opinions, though not an explicit reference to a sentence in a browser spec that states RTL exclusively. Boris Zbarsky seems to indicate that there is quite a mixture in groups.google.com/forum/#!topic/mozilla.dev.tech.layout/…). –
Sternum © 2022 - 2024 — McMap. All rights reserved.
:nth-child()
support, but it does not work in practice. – Bookcase