Over a million developers have joined DZone.

MooTools Zebra Tables Class

DZone's Guide to

MooTools Zebra Tables Class

· Web Dev Zone
Free Resource

Start coding today to experience the powerful engine that drives data application’s development, brought to you in partnership with Qlik.

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!

Create data driven applications in Qlik’s free and easy to use coding environment, brought to you in partnership with Qlik.


Published at DZone with permission of David Walsh. See the original article here.

Opinions expressed by DZone contributors are their own.


Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.


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

{{ parent.tldr }}

{{ parent.urlSource.name }}