Mar 102015
Article HTML

In the HTML language, tables are the natural way to display information structured as as set of records composed of several fields.

But the default look used by browsers to render HTML tables is not very appealing.

This post explains how to improve the look of HTML tables with just a few CSS rules.

HTML Table Structure

A table in HTML is enclosed in the tags “<table>” and “</table>”.

In most cases, the table has a header with the column names, followed by the set of records that the table is made of. The table header is enclosed in the tags “<thead>” and “</thead>”, and the main body is enclosed in tags “<tbody>”, “</tbody>”.

In the table header there is a record enclosed in “<tr>”, “</tr>”. Inside the record are the column names, each enclosed in “<th>”, “</th”>

The table body is made of a set of records, Each record enclosed in “<tr>”, “</tr>”. A record contains a set of values, enclosed in “<td>”, “</td>”.


A table in HTML format as in the above example is rendered by browsers as a series of rows and columns:

default rendering of a HTML table in Firefox

Adding CSS style to the table

To improve the presentation of the table, a CSS class “footable” is assigned to the <table> tag:

Then we start adding style rules for the “footable” class in the CSS stylesheet used by the page.

Let’s start by changing the font type and adding a table border:

We can see that in these rules also the font size and a dark grey color have been applied to the table. The “widht: 100%;” rule forces the table width to the width of the container element.

The rules above make the table to be displayed a little better:

table with border

Next, we will be adding horizontal lines between rows, and vertical lines between columns. We will also be adding a background color to the table header. Finally, the column names in the header and the column values in the body will be displayed left-justified. To do this, the following style rules are added:

table with background color

And finally, to achieve a professional look, the table corners can be slightly rounded:

and a ligth color gradient can be applied to the header background:

With these changes, the table looks like this:

table with rounded borders

As a final touch, we can make records to be highlighted when the cursor is placed on top of them, by changing the background color. A simple CSS rule can do the trick:

we can check the resulting effect, by placing the cursor over a row in the table:

table with highlighted row

And that’s all!

 Posted by at 9:32 am

 Leave a Reply