• Home
  • Amoeba
  • ASK
  • Community
  • Source Codes
  • Contact Us
Blue Orange Green Pink Purple

Alternate Table Row Color Styling or Zebra Table using Prototype

Posted in CSS & XHTML, Javascript & Libraries. on Friday, March 5th, 2010 by Aneeska Tags: css, prototype, zebra style
Mar 05

This tutorial teaches you to easily apply Zebra styling to your static/dynamic tables. The method of styling alternate rows with different colors is also known as Zebra styling. We normally achieve this by defining two sets of styles for odd and even rows of a table and manually adding these class names in to the table rows. When the data table is generated dynamically we use programming techniques to apply styles to alternate rows.

Here is a simple method using Prototype. This way, all you need to do is to define a class name for your table and prototype will automatically apply the odd table style to alternate rows of the table.

Let’s look at our table structure.

<table width="100%" border="0" cellspacing="0" cellpadding="0" class="ListTable">
  <tr>
    <th>ID</th>
    <th>TITLE</th>
    <th>DATE</th>
    <th>STATUS</th>
  </tr>
  <tr>
    <td>1</td>
    <td>Microsoft Launches first Android application</td>
    <td>Mar 05, 2010</td>
    <td>New</td>
  </tr>
  <tr>
    <td>1</td>
    <td>Another title</td>
    <td>Mar 05, 2010</td>
    <td>New</td>
  </tr>
  <tr>
    <td>1</td>
    <td>Google's new innovation</td>
    <td>Mar 05, 2010</td>
    <td>New</td>
  </tr>
  <tr>
    <td>1</td>
    <td>Microsoft Launches first Android application</td>
    <td>Mar 05, 2010</td>
    <td>New</td>
  </tr>
</table>

Note that I have defined “ListTable” as the class name for the table and nothing else.

Now in the head section of the html, include prototype Javascript Library.

<script type=”text/javascript” src=”prototype.js”></script>

You can download Prototype Library from here: http://www.prototypejs.org/download

This is the basic styles we will define for the table with a class “ListTable“:

<style>
.ListTable {
	border-left:1px solid #202020;
	border-right:1px solid #202020;
	font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
	font-size:11px;
	text-align:left;
	background-color:#1e1e1e;
}
.ListTable th, table.ListTable th {
	background-color:#161614;
	height:26px;
	vertical-align:middle;
	padding-left:10px;
	text-align:left;
	font-weight:normal;
	text-transform:uppercase;
	color:#b7b7b7;
}
.oddRow, tr.oddRow {
	background-color:#494949;
}
</style>

Now, add the below piece of Javascript in your html. This script will be called when the DOM is ready (similar to Window.onLoad).

<script>
document.observe('dom:loaded', function(){
	$$('table.ListTable tr:nth-child(odd)').invoke("addClassName", "oddRow");
});
</script>

Notice that we are applying a class named “oddRow” to alternate table rows in the table which has a class applied “ListTable“.

Here is a screen shot of the table generated using the above method.
zebra_table

Enjoy!


ASK a Question!

Bookmark and Share

1 Comment

  1. Atkinson on April 23rd, 2010

    Worked really great. wonderful tutorial. Expecting more useful codes and tutorials.



Leave a Reply

CAPTCHA Image CAPTCHA Audio
Refresh Image
  • Search a Topic


  • Got a Question?
      ASK and get a Solution! ASK
  • Categories
    • CakePHP
    • Computers
    • CSS & XHTML
    • Enterprise Web
    • Flash & Action Script
    • Flex
    • Javascript & Libraries
    • Perl Programming
    • PHP/MySQL
    • Programming Techniques
    • Regular Expression
    • Social Media
  • Recent Visitors
  • Recent Articles
    • How to Protect your SWF (AS2 or AS3). Prevent SWF Decompile using SWF Protector
    • PHP – Search in an Array for Values Matching a Pattern – Regex, Wildcard
    • PHP – How to Get Browser Properties and Capabilities – get_browser()
    • CakePHP – Search for records between two dates inclusively
    • Notebook/Laptop hangs frequently/random (on Low Battery when AC power is connected)
    • Swap Values Without Temp or A Third Variable in PHP
    • CakePHP – Auth Login Redirect Problem with Plugins
    • Convert PHP array to Javascript array
    • CakePHP – Accessing a model in AppController or in any Controllers
    • Save Images from Flash – Actionscript 3, Filereference.save, JPGEncoder
  • Archives
    • September 2010
    • July 2010
    • June 2010
    • May 2010
    • April 2010
    • March 2010
    • February 2010
    • January 2010
    • December 2009
  • Tags
      vertically center horizontally align dead center div amoeba ask credit card workbench decryption solutions kiosk questions horizontal center align vertical center align css trick solution css encryption as2 flash regex as3 Regular Expression action script mysql array CakePHP php

  • Donate

      If you find this site helpful, please donate to Amoeba Solution Kiosk. We will be using the money received for conducting technology awareness camps and seminars for school and college students in rural areas of Kerala, India.

  • Links
    • Amoeba Solutions
    • Blog Catalog
  • Home
  • Amoeba
  • ASK
  • Community
  • Source Codes
  • Contact Us

© Copyright Amoeba Solution Kiosk. All rights reserved.
An Amoeba Concept!

Back to Top