Formatting Tables w/CSS3



index
Disabled back button Next Section
printable version

Section 1: Overview

Tables, when processed by CSS, conform to a complicated set of algorithms referred to in the CSS specification as the CSS table model.

Display Properties

A handful of CSS display property values exist that apply specifically to tables. The list of these property values includes:

Section 2: Centering Tables

The most common way of centering a table is to set the margins to auto, like this:

table.center
{
margin-left:auto;
margin-right:auto;
}

<table class="center">
. . .
</table>

For more details on centering tables, visit this link.

Section 3: Table Styling (including borders)

We learned about borders, margins, and padding, all of which apply to formatting tables, in the CSS3 Box Model discussion.

There are several attributes that have been "obsoleted" with the advent of HTML5.

A series of examples will demonstrate how tables should now be formatted using CSS. Here is a basic table with no formatting:

Here is the same table with some formatting applied. It can also be viewed here.

Here is the CSS for the example above:

table
{
width: 600px;
font-size: 14px;
font-family: Helvetica, Arial, sans-serif;
border-collapse: separate;
border-spacing: 10px 5px;
table-layout: auto;
empty-cells: show;
}

The border-collapse Property

The border-collapse table property determines the table's border model.

This example shows the effect ot border-collapse:collapse. It can also be viewed here.

The border-spacing Property

The border-spacing table property controls the amount of separation between the borders of adjacent cells.

Spacing model.

The table-layout Property

The table-layout table property determines which layout algorithm a browser should employ as it renders the table.

The empty-cells Property

The empty-cells table property tells the browser whether or not to render styling on cells that have no content.

Padding

If you want some spacing within table cells, you must apply the padding property to each cell.

td, th
{
padding:5px;
}

Here are some tables to download and play with.

Section 4: Styling Columns

Styling tables from a column-based approach is a bit more complicated, with fewer options available.

Here is a sample table, without borders on cells, no background colors on even rows, and no empty-cells rule.

The following table can also be seen here.

Note that the #quantities column is set to 200 pixels in width and the cells in the #prices column are set to have a background color of #ddd.

Note especially the use of the <col> tag to specify specific formatting for table columns, and see this link for more details.

In addition, the cells in the thead and tfoot portion of the table retain their background color of #eee due to the influence of table layer stacking.

Stacking

As it pertains to determining a table cell's background color, there are six layers of stacking. (Assume for a moment that each of the following six layers has a unique background color specified on one of its elements.)

  1. The lowest layer, the table element itself.
  2. Column groups (override the table element).
  3. Individual columns (override column groups).
  4. Row groups (override individual columns).
  5. Individual rows (override row groups).
  6. Cell, the finest granularity and king of the hill!
Table layers.

The diagram shows how, looking down on the six rectangles, one may compute the background color of each cell. On the top layer, each cell that specifies a background is shown as a gray box with a black border. A cell without a background color in the top layer is depicted as a "window" onto the lower layers, which give its final background color. The same visual metaphor is used at each layer, so a row without a background acts as a window onto the row group layer, etc.

In the example code, cells – in this case, headings in the header and cells in the footer – beat out the background color declaration on the column.

Section 5: Structural Pseudo-Classes

The structural pseudo-classes are especially useful for formatting tables.


The first example shows the use of :last-child for a thicker border at the foot of the table:

If you look at the CSS you'll notice the following two declarations:

tfoot:last-child
{
border-bottom:thick solid black;
}

The :last-child selects last child element under the parent element, in this case the tfoot.


With reference to the original example (repeated below)...

If you look at the CSS you'll notice the following two declarations:

thead th:first-child
{
text-align: left;
}

tbody tr:nth-child( even )
{
background: #ddd;
}

The :first-child and :nth-child pseudo-classes are structural pseudo-classes that select elements based on where they occur in the markup.


Ordinal Pseudo-Class Selectors

Syntax Description
:first-child selects first child element under the parent element
:last-child selects last child element under the parent element
:first-of-type selects first child element of its type under the parent element
:last-of-type selects last child element of its type under the parent element


Algebraic Pseudo-Class Selectors

Syntax Description
:nth-child(n) nth child of parent
:nth-last-child(n) nth child of parent counting backwards
:nth-of-type(n) nth element of its type within the parent
:nth-last-of-type(n) nth element of its type counting backwards

Typical n values

Value Meaning
odd Every odd child or element
even Every even child or element
n The nth child or element
2n Same as even
3n Every third child or element (3,6,9,...)
2n+1 Same as odd
3n+1 Every third child or element starting with 1 (1,4,7,...)

For additional types of structural pseudo class, visit the following links:



So what good are structural pseudo classes? Here are some examples.

Let's look again at the table in the previous section (link):

After widening the Quantity column (column 2), the table might look better if that column was right justified.

Here is the result:

Note that the cells in the header and footer were not affected by the formatting.




The next example is based on the following table (link).

This table uses the following CSS:

/*
left-aligns the first column, including the header cells <th>,
the column cells <td>, and the footer cells <td>
*/
th:first-child, td:first-child
{
text-align: left;
}

/*
centers the second column, including the header cells <th>,
the column cells <td>, and the footer cells <td>
*/
th:nth-child(2), td:nth-child(2)
{
text-align: center;
}

/*
sets the background color of even numbered rows <tr> to
to silver
*/
tbody tr:nth-child(even)
{
background-color: silver;
}

Here are some links that explain CSS3 structural pseudo-classes in more detail:

Section 6: Styling Captions

The caption element, as you saw earlier, will generally appear above its parent table element.

The following table can also be seen here.

The caption-side property accepts one of three possible values: top, bottom, and inherit.

Section 7: CSS Tables

Note that this is not CSS for HTML tables, but CSS tables.

Section 8: Let's Play

Click the "Edit and Click Me >>" to see the results of the html code in the left panel in the panel on the right.

Then, play around with the html code, making changes and seeing the effect of those changes in the right panel.

Section 9: Resources