XML technologies / CSS / Advanced websites with CSS / Fixed-width layouts / The table

(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)

The final part of this layout is the tournament schedule table, summarizing the matches. As discussed earlier, tables should not be used for layout, but they are ideal for tabular data such as spreadsheets. This page has plenty of such data, already marked up correctly with the various HTML table tags. This makes styling the table’s individual regions easier.

Get inspired:
This layout’s designer drew inspiration from the CSS Table Gallery, an excellent reference for exploring different ways to present tabular data.

Our unstyled table is currently very readable and accessible, as the next figure shows, but it is not particularly attractive.

The unstyled table

Figure: The unstyled table.

Let us style it. First, make sure its content cannot move up beside the image positioned with float if the preceding content becomes shorter. We do this by setting the property clear to right.

table.schedule {
  clear: right;
  width: 100%;
  line-height: 1.4em;
  border-collapse: collapse;
  border: 4px solid #adbbca;
  color: #4f6480;
  background: #f0f0f0;
}

As the next figure shows, we set the table width to 100% and the line height to 1.4em. We also added a solid border and a background color.

The table after styling the table element

Figure: The table after styling the table element.

The property border-collapse, set here to collapse, ensures that no gaps appear between cells. Normally, small gaps are controlled by the attribute cellspacing of the element table.

You might think setting that attribute to 0 solves the problem. But a four-pixel border around every cell becomes eight pixels between cells, where adjacent borders add up; only outer borders remain four pixels. Setting the property border-collapse to collapse removes this effect by collapsing adjacent borders. A four-pixel border remains, exactly as intended. The next figure illustrates this:

Comparing cellspacing and border-collapse

Figure: Comparing the attribute cellspacing and the CSS property border-collapse.

When writing the schedule’s HTML, I used the element caption and supplied “Event Schedule” as the table heading. This element caption is best suited to describing our table. We will now style it to match the heading in the graphic design.

table.schedule caption {
  margin: 0;
  padding: 0;
  color: #032469;
  line-height: 2em;
  text-align: left;
  font-weight: bold;
}

When creating the document, I noted that we should use the tags <thead> and <tbody> for the table. They now make it easy to define different CSS rules for its regions. A rule for the selector table.schedule thead tr lets us set the header’s background and font.

table.schedule thead tr {
  color: white;
  background: #5e7796;
}

The next figure shows a considerable improvement, but much remains to match the design.

A blue table header in the thead element

Figure: A blue table header in the thead element.

The next rule targets the tags <th> inside the element thead. Other elements th occur in the left column, but this rule does not affect them. It adds padding and a four-pixel top border to the top row.

table.schedule thead tr th {
  padding: 0.4em 0.6em 0.4em 0.6em;
  border: 4px solid #adbbca;
}

Next comes the element tbody containing the table body’s cells. We do not want every cell bordered on all sides, only top and bottom borders on rows. Define these for elements tr inside tbody:

table.schedule tbody tr {
  border: 4px solid #adbbca;
}

Then style the elements th in tbody:

table.schedule tbody th {
  padding: 0.6em;
  border: 4px solid #adbbca;
}

Our page should now look like the following table.

Table rows clearly highlighted

Figure: Table rows clearly highlighted.

The design has horizontal stripes from alternating row background colors. Besides looking good, this improves readability. To create the stripes, add the attribute class="odd" to every second row in the HTML. Use this class to style those rows differently.

<tr>
  <th scope="row"><a href="#">Match 1</a></th>
  ...
  <td>Winner of Match 12</td>
</tr>
<tr class="odd">
  <th scope="row"><a href="#">Match 2</a></th>
  ...
  <td>Winner of Match 11</td>
</tr>
<tr>
  <th scope="row"><a href="#">Match 3</a></th>
  ...
  <td>Winner of Match 10</td>
</tr>
<tr class="odd">
  <th scope="row"><a href="#">Match 4</a></th>
  ...
  <td>Winner of Match 9</td>
</tr>

First define a background for the elements td inside tbody. While working on this element, I also added left padding to move text away from the border and align it with the headings:

table.schedule tbody td {
  background: #e9ecee;
  padding-left: 0.6em;
  border-bottom: 4px solid #ccc;
}

Now create a rule for elements tr of class odd, setting their row background:

table.schedule tbody tr.odd {
  background: #c4cfdb;
}

Then take the elements td between the tags <tr class="odd"> and </tr> and give them the background color specified in the design:

table.schedule tbody tr.odd td {
  background: #f0f0f0;
  color: #4f6480;
}

If these selectors look confusing, remember to read them from left to right:

  1. We have a table of class schedule, specified in the CSS rule by table.schedule.

  2. Inside the table is the element tbody.

  3. Next comes an element tr of class odd.

  4. Inside these elements tr are tags <td>.

The next figure shows the result:

Striped table after applying the odd class

Figure: Striped table after applying the odd class.

Finally, adjust the table’s links. There are two types: headings in the leftmost column and dates in the cells.

First style the links in elements th:

table.schedule tbody tr th a:link {
  font-weight: bold;
  color: #5e7796;
  text-decoration: underline;
}
table.schedule tbody tr th a:visited {
  font-weight: bold;
  color: #5e7796;
}
table.schedule tbody tr th a:hover {
  font-weight: bold;
  color: #5e7796;
  text-decoration: none;
}

Styling the links in elements td completes the table:

table.schedule tbody td a:link {
  color: #808000;
  text-decoration: underline;
}
table.schedule tbody td a:visited {
  color: #808000;
}
table.schedule tbody td a:hover {
  color: #808000;
  text-decoration: none;
}

The table is finished, completing the entire layout. It looks great, as the next figure shows.

  The styled table completes the layout

Figure: The styled table completes the layout.

  

 

 

 

back to top