tags:

views:

259

answers:

6

Is there a way to make the rows and columns in an HTML table checkered between 2 different colors?

I made a spread-sheet like list of data and I am thinking it will look better and be easier on the eyes if the rows/columns were checkered between white and off-white colors.

Thanks!

A: 

Sure. Make them all the same white color (i.e. keep them at the default), then programmatically add an off-white style to every other TD (or span or div or li or...).

Don't forget that you'll need to start at "column" 0 or 1 depending on which row you're on.

Michael Todd
+2  A: 

See an example in Colorize - jQuery Table Plugin - it involves some Javascript, but you can use it as is, or read as an example.

Colorize is a jQuery plugin to add background color to alternate table rows, highlight a row/column on mouse over, and colorize a row/column when you click your mouse button over it. You can colorize as many table rows as you want. A repeat mouse click reverts the row to the original background color.

gimel
+5  A: 

What kind of browser support requirements do you have? Are you able to forgo support for IE7 and earlier? If so, you can do this purely with CSS using the n-th expression:

tr td
{
    background-color: #fff;
}

tr:nth-child(even) td:nth-child(odd)
{
    background-color: #ccc;
}

tr:nth-child(odd) td:nth-child(even)
{
    background-color: #ccc;
}
jrista
Thats pretty cool, unfortunately I need to support IE7 for this. But it is a very cool trick, thanks for sharing!
John Isaacks
You can use this for most other browsers...then use an IE check to implement an alternative, javascript-assisted verson just in IE. That way, you arn't always running javascript to accomplish this...which can sometimes cause a delay before the styles are actually applied (and may slow the performance of your site...depends on the speed of the users computer.)
jrista
Excellent answer
Traingamer
CSS 3, hasten thy coming! (and users, update your browser!)
rpflo
A: 

To do this with just HTML+CSS and make it work with all major browsers, you will have to add a class to every alternate row by hand:

<tr>
   <td>col1</td>
   <td>col2</td>
</tr>
<tr class="highlight">
   <td>col1</td>
   <td>col2</td>
</tr>
<tr>
   <td>col1</td>
   <td>col2</td>
</tr>
<tr class="highlight">
   <td>col1</td>
   <td>col2</td>
</tr>

etc...

Then use CSS to change the color of the rows:

tr {
  background-color: #FFF;
}
tr.highlight {
  background-color: #CCC;
}
Abinadi
+2  A: 

Of course you can. Here is a simple javscript function to take care of it for you:

function altRows() {
    var table = document.getElementById("tableToAlternateRowsIn");
    var tRows = table.getElementsByTagName("tr");
    var cssClassName;

    for(var i = 0; i < tRows.length; i++) {
        (i % 2 == 0) ? cssClassName = "odd" : cssClassName = "even";
        tRows[i].className = cssClassName;
    }
}

window.onload = altRows;

That should do the trick for you. Not tested tho, so might not work exactly as planned.

Jesper Karsrud
A: 

There are several articles which claim that zebra-striping (to say nothing for checkerboarding) do not help readability. Checker-boarding, while an interesting excersise in CSS, seems to me to be more distracting than helpful. See A List Apart: zebra striping - does it help among other places.

Traingamer
Thanks for the info!
John Isaacks
I have to disagree about zebra-striping. While checkerboarding creates a pattern that your eyes have to actively deal with (it constantly breaks your flow while reading l-to-r), zebra-striping conforms to your eyes natural flow and motion while you read. I've been part of several usability studies around zebra-striping at two different companies, and when done right (not too much contrast, not too little) the results are a resounding "Yes, its helpful." I wouldn't directly compare striping and checkerboarding like that.
jrista
I use zebra stripes when people ask for it. To me, it's not a big enough issue to lose a contract over. I think you've touched the main issue - if done properly, it 'looks nicer' and may actually help. A poor choice of contrasting colors/highlights, on the other hand...
Traingamer