jQuery in Action, 2nd edition*
The moose likes HTML, CSS and JavaScript and the fly likes Alternate colors to table rows Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "Alternate colors to table rows" Watch "Alternate colors to table rows" New topic

Alternate colors to table rows

DVVN Sudhakara Rao

Joined: Oct 04, 2004
Posts: 17
In my JSP Page I have a html table. The table contains rows which are fetched from the database dynamically. I want to set different colors for the rows. Say for even number of rows I want to set red color. for odd number os rows I want to set green color. Is it possible to set like that. If yes could please anyone help me out how to do?Sorry for my bad Engling.I hope you got the problem understand.
Thanks in Advance
Eric Pascarello

Joined: Nov 08, 2001
Posts: 15376
HTML has no setting to do this automatically.

Therefore when you are writing out the rows with your sever side language in the database, that is when you need to assign the colors. Just check if the row is even or odd to determine what color you are to display.

<tr style="background-color:red">

Priyaa Vijay

Joined: Mar 10, 2005
Posts: 9

I tried specifying in my css file, different color for odd and even rows. But it is not getting reflected.

TABLE.data TR.odd { background-color: #d8ffff }
TABLE.data TR.evn { background-color: #ffdfff }

Should I have to specify in a different manner?

Thanks,<br />Priya
I agree. Here's the link: http://aspose.com/file-tools
subject: Alternate colors to table rows
Similar Threads
Datatable rows onMouseOut color change
Change the color of a JTable cell on mouse click
HTML Table "Stripes" with CSS
CellRenderer on the fly
Problem with row focus and scroll