Home arrow JavaScript arrow Page 3 - Building Zebra Tables with CSS and JavaScript
JAVASCRIPT

Building Zebra Tables with CSS and JavaScript


This is the first article in a four-part series that will teach you how to build basic zebra tables using CSS, server-side scripting languages, and JavaScript. If this interests you in any way, I strongly advise you to start reading...NOW!

Author Info:
By: Alejandro Gervasio
Rating: 4 stars4 stars4 stars4 stars4 stars / 7
June 11, 2008
TABLE OF CONTENTS:
  1. · Building Zebra Tables with CSS and JavaScript
  2. · Building zebra tables with a server-side scripting language
  3. · Creating a zebra table using CSS and some basic structural markup
  4. · Creating a zebra table by styling only its even rows

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

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.


blog comments powered by Disqus
JAVASCRIPT ARTICLES

- More Top jQuery Tutorials for Beginners
- More Top jQuery Plugins for Menus
- Top jQuery Tutorials for Beginners
- New UI Framework and SDK for JavaScript Rele...
- JavaScript OpenPGP Tool, Node.js 0.6.3 Avail...
- Yahoo Releases Cocktails Language and Develo...
- Customizing jQuery Slideshows: Dynamic Contr...
- Customizing jQuery Slideshows: the animate()...
- Customizing jQuery Slideshows: slideUp() and...
- Customizing jQuery Slideshows: hide() and sh...
- Web Workers: Performing Calculations in Para...
- More Top JavaScript Frameworks and Libraries
- More Dynamic jQuery Styling Techniques
- The Top JavaScript Libraries
- The Top JavaScript Frameworks

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 8 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials