Skip to main content
Welcome guest. | Register | Login | Post

Need Some CSS

6 replies [Last post]
supermike's picture
Offline
Joined: 2006-02-17

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?

a thing's picture
Offline
Joined: 2005-12-20

No, you need to at least set the id attribute of the <td>s.

libervisco's picture
Offline
Joined: 2006-05-04
"a thing" wrote:

No, you need to at least set the id attribute of the <td>s.

Isn't that obvious? Eye

CSS is meaningless without html I'd say.

tbuitenh's picture
Offline
Joined: 2005-12-21

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

Offline
Joined: 2006-03-28

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>
free-zombie's picture
Offline
Joined: 2006-03-08

I'd make it a php function...

Offline
Joined: 2006-03-28
"free-zombie" wrote:

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.

Comment viewing options

Select your