tags:

views:

56

answers:

5

Im trying to apply different background color to even and odd items in a UL.The code below works in Safarii but not in IE6. Is this a limitation in IE6 or is the CSS code incorrect?

ul#caseStudies {   
float: left;   
list-style: none;   
margin: 0;   
width: 100%;
}
ul#caseStudies li {   
border: 1px solid #999;   
float: left;   
margin: 0 10px 10px 0;   
width: 100px;   
}  

ul#caseStudies li:nth-child(even) {background: #CCC;}
ul#caseStudies li:nth-child(odd) {background: #C9C;}
</style>


<ul id="caseStudies">
    <li>1</li>
    <li>2</li>
</ul>
+2  A: 

IE6 does not support the :nth-child pseudo-element. If you need to support that browser you either need to:

  1. use an odd class to style some list items differently; or
  2. use Javascript.

As an example of (2) this is trivial in jQuery:

$("#caseStudies > li:nth-child(odd)").addClass("odd");
cletus
it will have to be done with JS, or by manually adding different classes to the li's
Mailslut
Yes. It is the proper code, and a limitation of IE6. Your best solution is to keep the code you have, and add this JS as suggested if you need it to also work in IE6. On the other hand, it may be perfectly reasonable to let IE6 users live without this small styling bonus. They won't even know they're missing it. That's your call, but you're on the right track!
Eric Meyer
A: 

nth child is a css3 selector and IE has no support for that.

Take a look at

CSS contents and browser compatibility

Use two classes for the even and odd list items and then apply style.

rahul
Not really, nth child has been there even on css2.
The Elite Gentleman
A: 

At this juncture it is probably no longer worth your energy to support the IE6 browser, let the poor thing die already. You are worrying over 10% of possible users.

Mimisbrunnr
And while 10% is certainly a large percentage to have your site not work in at all, for some alternating colours it's alright. People using IE6 don't expect pretty sites.
Bart van Heukelom
Since when is 10% insignificant? That's millions of users for some sites.
cletus
Imagine internet fee a company will pay if they update all their PC's to IE8....last time I heard, it's called cutting cost? :-)
The Elite Gentleman
It's not that 10% is insignificant, it's more that this is, as Bart said, not mission critical, and that IE6 is terrible when it comes to meeting standards. It necessitates crazy hoops for one to jump through, and will hopefully be gone soon.
Mimisbrunnr
sadly our company is in that 10%,we havnt updated our browsers yet...
manraj82
A: 

Yes

IE6 doesn't support this :nth-child

ul#caseStudies li:nth-child(even) {background: #CCC;}
ul#caseStudies li:nth-child(odd) {background: #C9C;}

li:whatever doesn't work for IE, only <a> is supported with

a:hover, a:active, a:visited

Use JQuery instead or create 2 CSS classes for odd and even for IE6.

The Elite Gentleman
A: 

Try using JS. The following code was taken from quirksmode.org:

function zebraLists() {
    var lists = getElementsByTagNames('ol,ul');
    for (var i=0;i<lists.length;i++) {
        var items = lists[i].childNodes;
        if (lists[i].parentNode.className === 'floater') continue;
        var counter = 1;
        for (var j=0;j<items.length;j++) {
            if (items[j].nodeName == 'LI' && !items[j].getElementsByTagName('li').length) {
                counter++;
                if (counter % 2 == 1)
                    items[j].className = 'odd';
                else
                    items[j].className = '';
            }
        }
    }
}

This way, the class name 'odd' is applied to the odd <li> elements of the list.

Dor