JavaScript
  Home arrow JavaScript arrow Page 3 - Building Zebra Tables with CSS and JavaScr...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVASCRIPT

Building Zebra Tables with CSS and JavaScript
By: Alejandro Gervasio
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 7
    2008-06-11

    Table of Contents:
  • Building Zebra Tables with CSS and JavaScript
  • Building zebra tables with a server-side scripting language
  • Creating a zebra table using CSS and some basic structural markup
  • Creating a zebra table by styling only its even rows

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Building Zebra Tables with CSS and JavaScript - Creating a zebra table using CSS and some basic structural markup


    (Page 3 of 4 )

    To be frank, building zebra tables with CSS and structural markup is possibly one of the easiest things that you'll ever learn in your life, since the whole process requires only one HTML table! And the rest consists merely of defining the respective background colors for even and odd rows via CSS. Period.

    Below I defined a primitive (X)HTML file that displays a basic zebra table, similar to the one created with PHP, on the browser.

    The pertinent code sample is as follows:


    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

    <html xmlns="http://www.w3.org/1999/xhtml">

    <head>

    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />

    <title>Example on building a simple zebra table with CSS (both odd and even rows are styled)</title>

    <style type="text/css">

    body{

    padding: 0;

    margin: 0;

    background: #fff;

    }

    h1{

    font: bold 16pt Arial, Helvetica, sans-serif;

    color: #000;

    }

    p{

    font: normal 10pt Arial, Helvetica, sans-serif;

    color: #000;

    margin: 0;

    }

    #zebratable{

    width: 40%;

    text-align: center;

    }

    #zebratable tbody tr.evenrow td{

    background: #eee;

    padding: 10px;

    }

    #zebratable tbody tr.oddrow td{

    background: #ccc;

    padding: 10px;

    }

    </style>

    </head>

    <body>

    <h1>Example on building a simple zebra table with CSS</h1>

    <table id="zebratable">

    <tbody>

    <tr class="oddrow">

    <td><p>Content for cells of odd row goes here</p></td>

    </tr>

    <tr class="evenrow">

    <td><p>Content for cells of even row goes here</p></td>

    </tr>

    <tr class="oddrow">

    <td><p>Content for cells of odd row goes here</p></td>

    </tr>

    <tr class="evenrow">

    <td><p>Content for cells of even row goes here</p></td>

    </tr>

    <tr class="oddrow">

    <td><p>Content for cells of odd row goes here</p></td>

    </tr>

    <tr class="evenrow">

    <td><p>Content for cells of even row goes here</p></td>

    </tr>

    <tr class="oddrow">

    <td><p>Content for cells of odd row goes here</p></td>

    </tr>

    <tr class="evenrow">

    <td><p>Content for cells of even row goes here</p></td>

    </tr>

    <tr class="oddrow">

    <td><p>Content for cells of odd row goes here</p></td>

    </tr>

    <tr class="evenrow">

    <td><p>Content for cells of even row goes here</p></td>

    </tr>

    </tbody>

    </table>

    </body>

    </html>


    As shown in the above hands-on example, the zebra table is simply created by defining two different CSS classes, where each of them is attached alternatively to even and odd rows. This achieves the so-called "zebra" effect. As you can see for yourself, constructing this type of table using a client-side approach is a no-brainer process that can be tackled with minor hassles.

    In addition to the sample (X)HTML file coded previously, I included another screen shot that shows how the zebra table I just created looks:



    So far, so good, right? At this moment, you should feel pretty satisfied, because you learned how to build a zebra table via a few basic CSS styles, along with the corresponding table's markup. So, what's the next step? Well, as you possibly noticed, the table in question was styled with two different CSS classes. This could eventually be a waste of code, since it's feasible to achieve the same result using only one.

    Thus, assuming that you may want to learn how to create zebra tables using a single CSS class, jump ahead and read the next section. I'll be there waiting for you.

    More JavaScript Articles
    More By Alejandro Gervasio


       · Zebra tables are an elegant way to display tabular data on web pages. Though, it’s...
       · Where is the Javascript ?
       · Thank you for commenting on my article. As mentioned in the last section of this...
     

    JAVASCRIPT ARTICLES

    - Validating Digits and Dates with jQuery`s Va...
    - Validating Ranges, Emails, and URLs with jQu...
    - More Uses for the jQuery Tooltip Plug-in`s b...
    - Building Image-Based Tooltips with the jQuer...
    - Using the jQuery Tooltip Plug-in`s bodyHandl...
    - Using Rangelength, Min and Max with the Vali...
    - Using Minlength and Maxlength with the Valid...
    - Modifying Tooltip Coordinates with the jQuer...
    - Applying a Fade Out Effect with the jQuery T...
    - Tracking Mouse Movements with the jQuery Too...
    - Checking Online Forms with the Validator jQu...
    - Nested JavaScript Functions as Objects
    - The jQuery Tooltip Plug-in
    - Active Client Pages at the Server
    - ACP Tab Web Page







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 5 Hosted by Hostway
    Stay green...Green IT