473,394 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Table with rows of alternating colours

Greetings.

Is it possible with CSS to have an HTML <tablesuch that even-numbered
<trrows have one background colour, and odd-numbered <trrows have
another background colour? This makes wide tables easier to read.

I would like this to work by applying a CSS class to only the <table>
element. Applying two different classes to alternate <trelements is
undesirable, as then it would be impossible to add, delete, or sort table
rows without ruining the colour alternation.

If someone could point me to an example, I would be much obliged.

Regards,
Tristan

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= < In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you
Aug 14 '06 #1
14 2258
Tristan Miller <ps********@nothingisreal.comwrote:
>Is it possible with CSS to have an HTML <tablesuch that even-numbered
<trrows have one background colour, and odd-numbered <trrows have
another background colour? This makes wide tables easier to read.

I would like this to work by applying a CSS class to only the <table>
element. Applying two different classes to alternate <trelements is
undesirable, as then it would be impossible to add, delete, or sort table
rows without ruining the colour alternation.
Not possible with CSS 2.x, it requires classes.

It is part of CSS3, but afaik the required selectors haven't been
implemented by anyone yet.

--
Spartanicus
Aug 14 '06 #2
Rik
Spartanicus wrote:
Tristan Miller <ps********@nothingisreal.comwrote:
>Is it possible with CSS to have an HTML <tablesuch that
even-numbered <trrows have one background colour, and odd-numbered
<trrows have another background colour? This makes wide tables
easier to read.

I would like this to work by applying a CSS class to only the <table>
element. Applying two different classes to alternate <trelements
is undesirable, as then it would be impossible to add, delete, or
sort table rows without ruining the colour alternation.

Not possible with CSS 2.x, it requires classes.

It is part of CSS3, but afaik the required selectors haven't been
implemented by anyone yet.
Yup:
http://www.w3.org/TR/css3-selectors/#nth-child-pseudo
According to webdevout.net not supported.
I usually let PHP build my lists/tables, and echo a class 'even' every other
element.

Grtz,
--
Rik Wasmus
Aug 14 '06 #3
Greetings.

In article <23***************************@news1.tudelft.nl> , Rik wrote:
I usually let PHP build my lists/tables, and echo a class 'even' every
other element.
I'm completely new to PHP; I've ordered some O'Reilly books but they are
yet to arrive. Any chance you (or anyone else) could post some sample
code for constructing such a table?

Regards,
Tristan

--
_
_V.-o Tristan Miller [en,(fr,de,ia)] >< Space is limited
/ |`-' -=-=-=-=-=-=-=-=-=-=-=-=-=-=-= < In a haiku, so it's hard
(7_\\ http://www.nothingisreal.com/ >< To finish what you
Aug 14 '06 #4
"Rik" <lu************@hotmail.comwrote:
>webdevout.net
Haven't seen that resource mentioned before. Seems like a good quality
and well researched piece of work.

--
Spartanicus
Aug 14 '06 #5
Spartanicus wrote:
Tristan Miller <ps********@nothingisreal.comwrote:
>Is it possible with CSS to have an HTML <tablesuch that even-numbered
<trrows have one background colour, and odd-numbered <trrows have
another background colour? This makes wide tables easier to read.

I would like this to work by applying a CSS class to only the <table>
element. Applying two different classes to alternate <trelements is
undesirable, as then it would be impossible to add, delete, or sort table
rows without ruining the colour alternation.

Not possible with CSS 2.x, it requires classes.
Or Javascript.
--
Jack.
http://www.jackpot.uk.net/
Aug 14 '06 #6
Tristan Miller wrote:
Greetings.

In article <23***************************@news1.tudelft.nl> , Rik wrote:
I usually let PHP build my lists/tables, and echo a class 'even' every
other element.

I'm completely new to PHP; I've ordered some O'Reilly books but they are
yet to arrive. Any chance you (or anyone else) could post some sample
code for constructing such a table?

Regards,
Tristan

--
_
Here is an ASP snippet... basically, you make two classes (row_on,
row_off) which are in an array, and then reference the correct one by
using the Modulus thingy, which will give you 0 or 1, as you loop
through the collection of rows (recordset in this case).

I am sure it is very similar in PHP.

<%
Dim Row_Class(1)
Row_Class(0) = "row_on"
Row_Class(1) = "row_off"
%>
<HTML>
-----etc..
-----Then in Body, use this snip after getting a recordset or...
<BODY>
<table>
<%
If Not rsData.BOF Or Not rsData.EOF Then
r=0
Do While rsData.EOF <True
TempStr = TempStr & "<tr class=" & Row_Class(r Mod 2) & ">"
r = r+1
For i = 0 to UBound(Col_Header)
TempStr = TempStr & "<td>" & rsData.fields(i) & "</td>"
Next
TempStr = TempStr & "</tr>" & VbCrLf
rsData.MoveNext
Loop
Response.Write(TempStr)
End If
%>
</table>
</BODY>
</HTML>

Aug 14 '06 #7
Rik
Spartanicus wrote:
"Rik" <lu************@hotmail.comwrote:
>webdevout.net

Haven't seen that resource mentioned before. Seems like a good quality
and well researched piece of work.
Yup, I've seen the link here somewhere about 2 weeks ago, and was happily
surprised about the depth of checking, and it's even up-to-date (I was very
fed up with searches that mentioned IE until 5.0, Opera until 6.0 etc..).

All in all a very good site indeed. Thanks to the one who mentioned it, whom
I can't remember.

Grtz,
--
Rik Wasmus
Aug 14 '06 #8
Rik
Tristan Miller wrote:
Greetings.

In article <23***************************@news1.tudelft.nl> , Rik
wrote:
>I usually let PHP build my lists/tables, and echo a class 'even'
every other element.

I'm completely new to PHP; I've ordered some O'Reilly books but they
are yet to arrive. Any chance you (or anyone else) could post some
sample code for constructing such a table?

Well, lets say you already have an array (or a while loop fetching db
results):

<?php
$i=1;
foreach($array as $row){
$class = ($i % 2 == 0)? ' class="even"':'';
echo "<tr{$class}>";
// or
echo "<li{$class}>";
// and some further code producing the content
$i++;
}
?>

and in css ad one of the applicable stylerules:

td{background-color:#FFFFFF;}
tr.even td{background-color:#FFFF88;}
li{background-color:#FFFFFF;}
li.even{background-color:#FFFF88;}

possibly even for a specific id'ed list/table:

#specific td{background-color:#FFFFFF;}
#specific tr.even td{background-color:#FFFF88;}
#specific li{background-color:#FFFFFF;}
#specific li.even{background-color:#FFFF88;}

So other lists/tables won't be affected by mistake.

Grtz,
--
Rik Wasmus
Aug 14 '06 #9
Tristan Miller wrote:
Greetings.

In article <23***************************@news1.tudelft.nl> , Rik
wrote:
>I usually let PHP build my lists/tables, and echo a class 'even'
every other element.

I'm completely new to PHP; I've ordered some O'Reilly books but they
are yet to arrive. Any chance you (or anyone else) could post some
sample code for constructing such a table?

Regards,
Tristan
Table is an array.

$alternator = -1;
$rowClass[ -1 ] = 'class="even"';
$rowClass[ 1 ] = 'class="odd"';

foreach( $table as $row ) {
$alternator *= -1;
echo '<tr ' . $rowClass[ $alternator ] . '>' . $row . '</tr>';
}
Aug 14 '06 #10
Jack wrote:
Spartanicus wrote:
>Tristan Miller <ps********@nothingisreal.comwrote:
>>Is it possible with CSS to have an HTML <tablesuch that even-numbered
<trrows have one background colour, and odd-numbered <trrows have
another background colour? This makes wide tables easier to read.

I would like this to work by applying a CSS class to only the <table>
element. Applying two different classes to alternate <trelements is
undesirable, as then it would be impossible to add, delete, or sort
table
rows without ruining the colour alternation.

Not possible with CSS 2.x, it requires classes.

Or Javascript.
Indeed. Incredibly easy, in concert with CSS, using the JQuery library:

http://15daysofjquery.com/table-striping-made-easy/5/

[ Ref: http://jquery.com/ ]

--

*** Remove the DELETE from my address to reply ***

================================================== ====
Kevin Scholl http://www.ksscholl.com/
ks*****@comcast.DELETE.net
------------------------------------------------------
Information Architecture, Web Design and Development
------------------------------------------------------
We are the music makers, and we are the dreamers of
the dreams...
================================================== ====
Aug 15 '06 #11
Spartanicus <in*****@invalid.invalidwrites:
Tristan Miller <ps********@nothingisreal.comwrote:
Is it possible with CSS to have an HTML <tablesuch that even-numbered
<trrows have one background colour, and odd-numbered <trrows have
another background colour? This makes wide tables easier to read.

I would like this to work by applying a CSS class to only the <table>
element. Applying two different classes to alternate <trelements is
undesirable, as then it would be impossible to add, delete, or sort table
rows without ruining the colour alternation.

Not possible with CSS 2.x, it requires classes.
Now there's a challenge! Readers with any sort of
intellectual squeamishness or taste should not follow this
link: <URL:
http://www.cl.cam.ac.uk/~jf15/StyleT...ows/index.html
>. Readers with certain sorts of visual disorders might have
trouble too, but it does seem to be valid CSS.

Notes:

* doesn't work in IE6 (something to do with table
borders, I suspect). Haven't tried any other IEs,
just recent versions of Firefox, Opera and Konqueror.

* relies on images, though only 156 bytes of them.

* probably missing some settings that browsers may set
to unexpected values, but as I'm accessing the
machine via VNC over a slow link from a computer
with a 256 colour display, I don't have the patience
to do any more...

--
Jón Fairbairn Jo***********@cl.cam.ac.uk
http://www.chaos.org.uk/~jf/Stuff-I-dont-want.html (updated 2006-07-14)
Aug 15 '06 #12
Jón Fairbairn <jo***********@cl.cam.ac.ukwrote:
>Not possible with CSS 2.x, it requires classes.

Now there's a challenge! Readers with any sort of
intellectual squeamishness or taste should not follow this
link: <URL:
http://www.cl.cam.ac.uk/~jf15/StyleT...ows/index.html
http://homepage.ntlworld.ie/spartanicus/temp.png

--
Spartanicus
Aug 15 '06 #13
Spartanicus <in*****@invalid.invalidwrites:
Jón Fairbairn <jo***********@cl.cam.ac.ukwrote:
Not possible with CSS 2.x, it requires classes.
Now there's a challenge! Readers with any sort of
intellectual squeamishness or taste should not follow this
link: <URL:
http://www.cl.cam.ac.uk/~jf15/StyleT...ows/index.html

http://homepage.ntlworld.ie/spartanicus/temp.png
I forgot to say: it won't work if the original line spacing
isn't a whole number of pixels (I get the same effect as you
in firefox if I magnify the font size one step with Ctrl-+)
though I don't know if that was the problem for you, or if I
missed something else.

Mind you, I'd have thought that a non-integral line spacing
would produce undesirable effects and be something that a
browser wouldn't generally do, so I probably have forgotten
something, but I can't see what.

Also the Firefox and Opera I have behave differently if the
overall border of the table is an odd or even number of
pixels -- presumably one starts the background-image repeat
at the outside of the border and the other at the
inside. The effect is to give the odd/even colours a
different order in the two browsers.

--
Jón Fairbairn Jo***********@cl.cam.ac.uk
http://www.chaos.org.uk/~jf/Stuff-I-dont-want.html (updated 2006-07-14)
Aug 15 '06 #14
Jón Fairbairn <jo***********@cl.cam.ac.ukwrites:
Spartanicus <in*****@invalid.invalidwrites:
Jón Fairbairn <jo***********@cl.cam.ac.ukwrote:
>Not possible with CSS 2.x, it requires classes.
>
>Now there's a challenge! Readers with any sort of
>intellectual squeamishness or taste should not follow this
>link: <URL:
>http://www.cl.cam.ac.uk/~jf15/StyleT...ows/index.html
http://homepage.ntlworld.ie/spartanicus/temp.png

I forgot to say: it won't work if the original line spacing
isn't a whole number of pixels (I get the same effect as you
in firefox if I magnify the font size one step with Ctrl-+)
though I don't know if that was the problem for you, or if I
missed something else.
Hmm. Did you use Firefox's Ctrl-+ (or View=>Text
Size=>Increase)?

I just went through the first eighteen font sizes for about
half of the fonts on my machine, setting them as the default
font while viewing the page, and it worked in every case,
whereas Ctrl-+ makes it fail. Evidently that doesn't simply
increase the sizes of the fonts (and associated spacings)
used (which is what I would infer from "Increase text size),
but does something strange instead.

Opera's Zoom option does something even stranger, in that it
neither simply magnifies the rendered page nor preserves the
CSS properties (the CSS says border-bottom: 1px, but at some
magnifications the bottom borders dividing the three cells
are different thicknesses).

--
Jón Fairbairn Jo***********@cl.cam.ac.uk

Aug 16 '06 #15

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: LRW | last post by:
Is there some way to make a table have alternating colors for rows when you're generating the table data with a WHILE statement? You know, row 1 has a gray BG, row 2 is white, row 3 is gray, 4 is...
1
by: Alistair Birch | last post by:
Hi I want rows of a table to appear in alternating background colours. Having looked around the web I can't find any solution apart from waiting for the next version of CSS, so I tried building...
47
by: Matt Kruse | last post by:
http://www.mattkruse.com/temp/css_expressions.html One of the standard CSS questions is "how can I shade every other table row a different color with CSS?" The answers are usually 1) you can't...
2
by: Savvas | last post by:
Hi I am trying to make my datagrid look a bit better than the typical one. How do i show the datagrid to have alternating colours e.g. like in windows media player v.10? Thanks Savvas
117
by: phil-news-nospam | last post by:
Is there really any advantage to using DIV elements with float style properies, vs. the old method of TABLE and TR and TD? I'm finding that by using DIV, it still involves the same number of...
5
by: | last post by:
I'm making admin forms. I'm wondering if there is a way to have the server programmatically assign alternating colors in a regular table (not a datalist control). I notice ASP.NET 2 offers a...
5
by: eric.goforth | last post by:
Hello, I have some xml data that look like: <currencysummary rec_count="16"> <currency> <currency_description>$</_currency_description> <currency_code>1</_currency_code> </currency>...
13
by: Trev | last post by:
Is there any way to make table cell's bgColor alternate, simulating a flashing effect?
0
by: J. Cliff Dyer | last post by:
On Tue, 2008-04-29 at 13:14 -0500, Victor Subervi wrote: Not quite. You're passing one variable to the string formatting operator, and passing a tuple to the print function. The implicit...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.