This article was made for people that hate to manually assign classes over and over again to HTML elements like: tr's, td's, li's, etc. There is an easier way to do it without the fuss of making sure you didn't forget something. My solution is to use jQuery.
Ever since implementing jQuery in my web design, I've been able to design quicker and make my designs more interactive.
The code snippet I'll be using will help do the following:
- Create zebra stripes on your tables.
- Assign first-child and last-child classes automatically to overcome cross-browser issues.
- Assign hover classes for cross-browser compatibility so you are able to assign hover styles to elements you couldn't assign before.
- Plus more.
First Things First
The first thing you have to do is to include jQuery in the head or end of your HTML document. See jQuery for specific instructions beyond how I recommend.
What I do is include the following at the end of my document:
Zebra Striping
If you don't know what zebra striping is, you'll recognize it once I tell you. Zebra striping is when you alternate the colors of your HTML elements. It's most commonly used on table rows, so that will be the example I use and what my code covers. The easiest way to add zebra striping is to add an even and odd class to each tr. This can get monotonous and can be easy to make mistakes. So it's much easier to dynamically add these classes when possible.
Here is the code I use to add the odd and even classes:
First-Child / Last-Child
Sometime understanding what elements are the first and last elements can help to style things like list-items. I use first-child and last-child mostly to remove borders on menus, but there are plenty of other uses like changing text-alignment in a table column.
Here is the code that I use to add first-child and last-child classes to specific elements:
Hover
My code takes care of cross-browser compatibility issues with the psuedo-class :hover. In the past the only HTML tag that work with the :hover psuedo-class is <a>. Newer more modern browser will respond to this psuedo-class.
Here is the code that I use to add a hover class to elements:
Table Examples
Here's the table code I'm using:
| Header 1 | Header 2 | Header 3 |
|---|---|---|
| Table Data 1.1 | Table Data 1.2 | Table Data 1.3 |
| Table Data 2.1 | Table Data 2.2 | Table Data 2.3 |
| Table Data 3.1 | Table Data 3.2 | Table Data 3.3 |
| Table Data 4.1 | Table Data 4.2 | Table Data 4.3 |
| Table Data 5.1 | Table Data 5.2 | Table Data 5.3 |
Boring Table
| Header 1 | Header 2 | Header 3 |
|---|---|---|
| Table Data 1.1 | Table Data 1.2 | Table Data 1.3 |
| Table Data 2.1 | Table Data 2.2 | Table Data 2.3 |
| Table Data 3.1 | Table Data 3.2 | Table Data 3.3 |
| Table Data 4.1 | Table Data 4.2 | Table Data 4.3 |
| Table Data 5.1 | Table Data 5.2 | Table Data 5.3 |
Table w/ New Dynamic Classes Added
| Header 1 | Header 2 | Header 3 |
|---|---|---|
| Table Data 1.1 | Table Data 1.2 | Table Data 1.3 |
| Table Data 2.1 | Table Data 2.2 | Table Data 2.3 |
| Table Data 3.1 | Table Data 3.2 | Table Data 3.3 |
| Table Data 4.1 | Table Data 4.2 | Table Data 4.3 |
| Table Data 5.1 | Table Data 5.2 | Table Data 5.3 |
CSS Sample
Here's a sample of the CSS I use to style the table:
Horizontal Menu Items (Using UL LI)
Horizontal menus can be tricky when it comes to creating seperators. Why not just use a border instead of a pipe.
Horizontal menu done like this (less code):
- Item 1
- Item 2
- Item 3
- Item 4
- Item 5
Instead of like this (unnecessary code):
- Item 1
- |
- Item 2
- |
- Item 3
- |
- Item 4
- |
- Item 5
Without First-Child / Last-Child
With First-Child / Last-Child
CSS Sample
This is a sample of the CSS I use to remove the border:
Hover Input Elements
You've already seen an example of hovering a table row and having the whole row change colors, well I wanted to show you an input field.
Here's the example of hoverable input fields:
JQuery Sample
Here is the code that I use to add a hover class to elements:
CSS Sample
More
There are tons more things you can do with JQuery. It's very web designer friendly and easy to look at and understand. That's why JQuery is my JavaScript Library of choice.
I recommend taking some time and understanding the code I've given example to so you can leverage the ability to dynamically assign classes to your HTML documents. JQuery can help clean up your code by allowing you to:
- Keep your HTML as bare bones as possible.
- Not hardcode in unnecessary classes in case you decide to reuse the same HTML for a new design.
- Save the hassle of handcoding all the classes that makeup things like zebra stripes.