Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IE: nth-child() using odd/even isn't working

Tags:

My table (that works perfectly on Chrome, FireFox and Opera) is not displaying correctly on Internet Explorer.

The background remains white! (I am using IE-8)

CSS code:

/*My Table*/ .my_table{ border-collapse:collapse; font:normal 14px sans-serif,tahoma,arial,verdana; margin:5px 0; }  .my_table th{ color:#fff; background:#5E738A; border:1px solid #3C5169; text-align:center; padding:4px 10px; }  .my_table td{ color:#555; border:1px solid #C1CAD4; text-align:center; padding:2px 5px; }  .my_table tr:nth-child(even){ background:#E6EDF5; }  .my_table tr:nth-child(odd){ background:#F0F5FA; } 
like image 461
ajax333221 Avatar asked Nov 18 '11 21:11

ajax333221


People also ask

How do you find the nth child of an element?

Use the querySelector() method to get the nth child of an element, e.g. document. querySelector('#parent :nth-child(3)') . The :nth-child pseudo class returns the element that matches the provided position.

What's the difference between the nth-of-type () and Nth child () selectors?

The nth-of-type is very similar to the nth-child pseudo-class. The main difference is that it specifically considers the type of the element getting selected before checking any other logic. Let's use our example from above but apply nth-of-type instead.

What is nth-of-type CSS?

The :nth-of-type selector allows you select one or more elements based on their source order, according to a formula. It is defined in the CSS Selectors Level 3 spec as a “structural pseudo-class”, meaning it is used to style content based on its relationship with parent and sibling elements.


1 Answers

As a good workaround, jQuery has added this to their project and achieving this using JavaScript is acceptable:

For my CSS, I would have

.my_table tr.even{     background:#E6EDF5; }  .my_table tr.odd{     background:#F0F5FA; } 

And I would use jQuery to do this:

$(document).ready(function() {     $(".my_table tr:nth-child(even)").addClass("even");     $(".my_table tr:nth-child(odd)").addClass("odd"); }); 
like image 181
Abdul Munim Avatar answered Dec 02 '22 23:12

Abdul Munim