Over a million developers have joined DZone.

MooTools Zebra Tables Class

DZone's Guide to

MooTools Zebra Tables Class

· Web Dev Zone ·
Free Resource

Have you seen our HERE Twitch channel to livestream our Developer Waypoints series?

Tabular data can oftentimes be boring, but it doesn't need to look that way! With a small MooTools class, I can make tabular data extremely easy to read by implementing "zebra" tables -- tables with alternating row background colors.


.highlight		{ background:#d5fcdc; }
.even { background:#fff; }
.mo { background:#e3f1fb; }
.odd { background:#eee; }
.zebra th { padding:5px; background:#ddd; border-bottom:1px solid #999; text-align:left; font-weight:bold; }
.zebra td { padding:5px 20px 5px 5px; border-bottom:1px solid #ddd; }

The above CSS is extremely basic. I've styled the <TH> tag so that it stands out from table rows. There are four states of rows in my zebra table: highlighted (or "clicked on"), regular even and odd, and a mouseover state -- these are represented by the "even", "mo", "highlight", and "odd" classes. I've added padding and a bottom border to the <TD>'s for presentation.


<table class="zebra" cellpadding="0" cellspacing="0">
<td>Actor In A Leading Role</td>
<td>Daniel Day-Lewis</td>
<td>There Will Be Blood</td>
<td>Actress In A Leading Role</td>
<td>Marion Cotillard</td>
<td>La Vie en Rose</td>
<td>Actor In A Supporting Role</td>
<td>Javier Bardem</td>
<td>No Country For Old Men</td>
<td>Actress In A Supporting Role</td>
<td>Tilda Swinton</td>
<td>Michael Clayton</td>
<td>Joel Coen and Ethan Coen</td>
<td>No Country For Old Men</td>
<td>Diablo Cody</td>

The above table is a simple, standard table. The only detail of note is that this table is given the "zebra" class, which signals to MooTools that this table should be zebra-ized.

The Javascript Class

/* classes */
var ZebraTables = new Class({
initialize: function(table_class) {

//add table shading
$$('table.' + table_class + ' tr').each(function(el,i) {

//do regular shading
var _class = i % 2 ? 'even' : 'odd'; el.addClass(_class);

//do mouseover
el.addEvent('mouseenter',function() { if(!el.hasClass('highlight')) { el.addClass('mo').removeClass(_class); } });

//do mouseout
el.addEvent('mouseleave',function() { if(!el.hasClass('highlight')) { el.removeClass('mo').addClass(_class); } });

//do click
el.addEvent('click',function() {
//click off
//click on


The class accepts one parameter, which is the class given to tables that should be Zebra-ized. Upon initialization, the class cycles through each table row. During the row looping, the row is given its odd or even CSS class, and a listener is added to the row to highlight the row upon mouseover. The above javascript could be placed into an external javascript file.

The Javascript Implementation

/* do it! */
window.addEvent('domready', function() {
var zebraTables = new ZebraTables('zebra');

To implement ZebraTables, all you need to do is add the above code to any given page.

Click here to experience beautifully formatted tabular data!

Do you have any suggestions for my zebra tables? Let me know!

Developer Waypoints is a live coding series from HERE, which will teach you how to build with maps and location data.


Published at DZone with permission of

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}