Useful CSS tips:Tables
From MDC
Contents |
[edit] Centering
If you want to center a table, it is not correct to use
-
text-align: center
in its parent. The correct way is to apply
-
margin: 0px auto 0px auto
style to the table itself.
| Table with margin: 0px auto 0px auto; | ||
| cell content | cell content | cell content |
| cell content | cell content | cell content |
| cell content | cell content | cell content |
[edit] Borders
This is a standard table with non-zero cellspacing and 1px cell borders.
| Table with cellspacing="4" | ||
| cell content | cell content | cell content |
| cell content | cell content | cell content |
| cell content | cell content | cell content |
If you want to transform the table in a grid, you set cellspacing to 0 and the rendered tables is this.
| Table with cellspacing="0" | ||
| cell content | cell content | cell content |
| cell content | cell content | cell content |
| cell content | cell content | cell content |
The result is not as expected, and the reason is that the default border model is set to separate. With this model each cell has its own border, also if cells spacing is zero. In order to have a grid-like rendering, the collapse border model has to be set.
| Table with cellspacing="0" and border-collapse:collapse; | ||
| cell content | cell content | cell content |
| cell content | cell content | cell content |
| cell content | cell content | cell content |
[edit] Columns style
If you want to assign a particular style to certain table columns, the usual solution is to create a class style and explictly assign cells belonging to those columns to that class.
<style type="text/css">
td { background-color: #eeeeee; }
td.darkcol { background-color: #cccccc; }
</style>
<table>
<tr><td class="darkcol">Cell</td>
<td>Cell</td><td>Cell</td><td>Cell</td><td class="darkcol">Cell</td></tr>
<tr><td class="darkcol">Cell</td>
<td>Cell</td><td>Cell</td><td>Cell</td><td class="darkcol">Cell</td></tr>
<tr><td class="darkcol">Cell</td>
<td>Cell</td><td>Cell</td><td>Cell</td><td class="darkcol">Cell</td></tr>
<tr><td class="darkcol">Cell</td>
<td>Cell</td><td>Cell</td><td>Cell</td><td class="darkcol">Cell</td></tr>
<tr><td class="darkcol">Cell</td>
<td>Cell</td><td>Cell</td><td>Cell</td><td class="darkcol">Cell</td></tr>
</table>
This solution has the advantage to be cross-browser, but it also needs changing the HTML code, to explictly select table cells to be styled.
For browsers supporting adjacent selector there is an alternative pure CSS solution.
<style type="text/css">
/* Style for all rows */
tr { font: bold 16px Arial; }
/* Style for rows with a row before: 2nd, 3rd, 4th, ... */
tr + tr { font: 12px Arial; }
/* Style for all columns */
td { background-color: #cccccc; }
/* Style for rows with a row before: 2nd, 3rd, 4th, 5th */
td + td { background-color: #eeeeee; }
/* Style for rows with 4 rows before: 5th */
td + td + td + td + td { background-color: #cccccc; }
</style>
<table>
<tr><td>Cell</td><td>Cell</td><td>Cell</td><td>Cell</td><td>Cell</td></tr>
<tr><td>Cell</td><td>Cell</td><td>Cell</td><td>Cell</td><td>Cell</td></tr>
<tr><td>Cell</td><td>Cell</td><td>Cell</td><td>Cell</td><td>Cell</td></tr>
<tr><td>Cell</td><td>Cell</td><td>Cell</td><td>Cell</td><td>Cell</td></tr>
<tr><td>Cell</td><td>Cell</td><td>Cell</td><td>Cell</td><td>Cell</td></tr>
</table>
[edit] Fixed header
If a table contain a lot of rows, when the user has to scroll down to see all data, table header row scrolls up and becomes invisibile. You can have a table with fixed header and vertical scrolling, use the following code.
<style type="text/css">
table {
width: 20em; /* esthetics */
border-collapse: separate; /* by default */
/* border-collapse: collapse; /* Bug since 2002 with overflow-y: auto on tbody */
border-spacing: 0; /* workaround */
}
tbody {
height: 10em; /* define the height */
overflow-x: hidden; /* esthetics */
overflow-y: auto; /* allow scrolling cells */
}
td {
border-left: 1px solid blue; /* workaround */
border-bottom: 1px solid blue; /* workaround */
}
</style>
<table>
<thead><tr><th>Header</th><th>Header</th><th>Header</th></tr></thead>
<tfoot><tr><th>Foot</th><th>Foot</th><th>Foot</th></tr></tfoot>
<tbody>
<tr><td>Cell</td><td>Cell</td><td>Cell</td></tr>
<tr><td>Cell</td><td>Cell</td><td>Cell</td></tr>
<tr><td>Cell</td><td>Cell</td><td>Cell</td></tr>
<tr><td>Cell</td><td>Cell</td><td>Cell</td></tr>
<tr><td>Cell</td><td>Cell</td><td>Cell</td></tr>
<tr><td>Cell</td><td>Cell</td><td>Cell</td></tr>
<tr><td>Cell</td><td>Cell</td><td>Cell</td></tr>
<tr><td>Cell</td><td>Cell</td><td>Cell</td></tr>
<tr><td>Cell</td><td>Cell</td><td>Cell</td></tr>
<tr><td>Cell</td><td>Cell</td><td>Cell</td></tr>
<tr><td>Cell</td><td>Cell</td><td>Cell</td></tr>
<tr><td>Cell</td><td>Cell</td><td>Cell</td></tr>
<tr><td>Cell</td><td>Cell</td><td>Cell</td></tr>
</tbody>
</table>
Actually there is a problem with Firefox styling engine (see bug 135236), which seems not to correctly render tables with collapse border model and TBODY with overflow: auto style. I will ask DEVMO admins...