PDA

View Full Version : Alternating colors via CSS


cg9com
06-09-2003, 05:31 AM
I was trying to figure out how to alternate colors, say in each table cell like on this page, it can be accomplished through CSS.

I'm afraid it may only be possible in level 3 CSS, can anyone help me out?

pardicity3
06-09-2003, 05:45 AM
Hmm, short of just declaring two classes, each with a different background color, I am not sure of any other way of doing this.

Are you going to be pulling your data from a database? If so you can somewhat more easily assign the classes assuming you are using a server-side langauge to output your data.

That is a good question though. Something very interesting to ponder...I wonder if there is a way....W3 here I come....

cssangel
06-09-2003, 05:45 AM
<edit>sorry being silly</edit>:o

pardicity3
06-09-2003, 05:46 AM
cssangel-

cg9com is refering to how the background colors for each post on a specific thread alternate. Just like this thread here...of course you need to have more than one post in the thread to see the effect :).

cssangel
06-09-2003, 05:48 AM
yes I got it as soon as I posted :p
ca

cg9com
06-09-2003, 06:00 AM
Hi guys, thanks for the input.
I think I have found it. (at least what I was reffering to)

It can actually be done using the :nth-child pseudoclass, which isn't supported by any of my browsers. :rolleyes:

Heres an example:

table td:nth-child(even) {background-color:#000;}
table td:nth-child(odd) {background-color:#fff;}


I figured this idea would be pretty far out as far as vendor support, and it is.
I was just hoping I didn't have to make a class for each color, because then I would have to create a :hover for each class etc.
(working with a menu here)

pardicity3, what do you mean, I have plenty of server side access, but it still would not make this easier eh?

cg9com
06-09-2003, 06:27 AM
I was correct, level 3. which means I cant use it :mad: :mad: :mad:
http://www.w3.org/TR/css3-selectors/#nth-child-pseudo

brothercake
06-09-2003, 10:05 AM
You could use class names of your own - "firstChild" "secondChild" and so on, which you write into the elements in the DOM.

cg9com
06-10-2003, 04:16 PM
Hmm ... There's a thought, at first I was hoping for only CSS, but this could suffice, i'll work with it. :D