473,769 Members | 1,711 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 2296
Tristan Miller <ps********@not hingisreal.comw rote:
>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********@not hingisreal.comw rote:
>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.comwro te:
>webdevout.ne t
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********@not hingisreal.comw rote:
>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_Head er)
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.comwro te:
>webdevout.ne t

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{backgro und-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{backgro und-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

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

Similar topics

12
2894
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 white, etc, for as long as there's data. Here's what I'm using right now: $query_RSwho = "SELECT * FROM tbl_indv ORDER BY name ASC"; $RSwho = @mysql_query($query_RSwho, $connection) or die("Couldn't query: " .. mysql_error()); while...
1
4577
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 one myself, but I need some help to finish it. I use the getAttribute('rowIndex') to return the position of a row within the table and apply modulo 2 to return a 0 or 1 indicating an even or odd row number, from which I apply the alternating...
47
18904
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 (yet), 2) put a class on every other tr, or 3) use javascript to do the styling for you. Well, I've been playing the CSS expressions quite a bit lately and I've found a much simpler solution which works in IE5.5+, using "expressions". If
2
1768
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
18572
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 elements in the HTML to get everything just right. When you consider the class attribute on the DIV elements, there's not much size savings anymore for using DIV. There are other disadvantages to not using TABLE/TR/TD, such as the lack of ability...
5
3346
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 runat="server" attribute on tables, leading me to believe that this should be possible. Any ideas? I'm using C#. Thanks, -KF
5
1475
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> <currency>
13
2085
by: Trev | last post by:
Is there any way to make table cell's bgColor alternate, simulating a flashing effect?
0
155
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 parenthesization is not print '<tr bgcolor="%s">' % (bg, d) as I think you are suggesting, but rather it is
0
9415
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10198
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9978
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9848
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8860
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7392
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5432
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3947
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2810
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.