Need Some CSS
Wed, 2006-04-05 23:00
I need some CSS where I have a grid table with rows that alternate in color. I can do this in Javascript or PHP, of course, but how do I do it purely with CSS, if that's even possible? Probably not, right?










No, you need to at least set the id attribute of the <td>s.
No, you need to at least set the id attribute of the <td>s.
Isn't that obvious?
CSS is meaningless without html I'd say.
I'd use class instead of id, cause you don't want separate ids and css bits for every row.
Maybe you can think of a clever trick with :first-line , but I can't :-P
I don't think there's a pure CSS-solution, just some PHP-HTML-CSS-combo like this:
<html> <head> <style type="text/css"> .line0 { background-color:#00ff00; } .line1 { background-color:#0000ff; } </style> </head> <body> <table> <?php for ($x=0;$x<10;$x++) { echo '<tr class="line'.($x%2).'"><td>Test</td><td>'.$x.'</td></tr>'; } ?> </table> </body> </html>I'd make it a php function...
I'd make it a php function...
Well, I think that depends.
I think usually you won't do it the way I've shown in the example.
Let's say you get your data from a database and use a WHILE-loop to output everything.
You just add a counter and use the modulo-divison to decide which CSS-class should be used for the line.
A function for that is, in my oppinion, absolutely not necessary.