Archive

Author Archive

Zebra Stripe Tables – Alternating Row Colors

April 9th, 2010 admin No comments

Recently I received the following question:

Currently i’m working on a table within css and am trying to create a table with related data, separated by different colored backgrounds. A lot like many programs that host information in rows. Where I am failing, is the positioning of the data within that zebra table through CSS. Can you give a brief description, or a description in full, how I could create this?

The general concept of accomplishing this is pretty simple.  You are going to be needing 2 different css classes, which you can find examples of below.  It works using inheritance from the row instead of applying the styles to every single table cell.

tr.even td {
	background-color: #FFF;
	color: #000;
}
tr.odd td {
	background-color: #F5F5F5;
	color: #000;
}

The css classes will be taking care of the actual look and feel of the zebra stripe. The trick is going to be getting the styles on the proper rows. The best approach would be to do it on the server side.  A common method is to use a counter and use modulus to find out whether or not the row is even.  If there isn’t a remainder, that means the row is even!.

for ($i=0; $i<10; $i++) {
print “<tr class=’”. $i % 2 == 0 ? “even” : “odd” .”‘>”;
print “<td>Stripe</td>”;
print “</tr>\n”;
}

The above is a relatively brief description, if you would like more detail.  Feel free to leave comments and I will be glad to add more!

Categories: CSS Tags:

Looping Structures

October 3rd, 2009 admin No comments

Looping is a common concept in any programming language.  It is commonly used to loop through a group or list of common elements.  Before we start though, I want to point out that loops can be used for many different things.

Well lets begin, there are 3 basic looping structures, of which are explained below.

  • While: A while is commonly used when you do not know the size of the list.  For example when you are reading in a file.  These are read in many different ways, line by line, character by character, etc.  Any way you look at it though, at the beginning of the loop, you do not know how many reads you are going to have to execute.  There could be a range of zero to infinity.
  • do while: A do while is not as common.  It is used for the exact same reason as the while loop, except its code will always be executed at least once.
  • For: I personally use this loop the most.  It is used when you know the exact size of the list.  For example.   If you are looping through the english alphabet.  You know there is going to be 26 elements in the list, so you should use a for loop.

I hope this tutorial has helped you understand the basics of looping.  If you have any questions, please leave comments below and I will update this post!

Categories: Programming Fundamentals Tags: