XML technologies / CSS / CSS Cookbook / Tables / Setting cell borders and padding

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

Adjust cell borders and padding for a stronger visual effect than default tables (see the figure).

Cells with adjusted borders and padding

Figure: Cells with adjusted borders and padding.

Solution

Use the property padding to set space between cell content and edges. Define cell and table borders with the property border (Note: the identical rules for td and th+th are not grouped because Internet Explorer 5.5 and 6 ignore any rule containing the adjacent sibling selector +, even as part of a selector list):

table {
 border-collapse: collapse;
 border: 5px solid #444;
}
td {
 padding: 4px;
}
th {
 color: white;
 background-color: black;
}
td {
 border: 5px solid #666;
}
th+th {
 border: 5px solid #666;
}
td+td {
 border: 5px solid #ccc;
 text-align: center;
}
td#winner {
 border: 7px dotted #999;
}

Discussion

HTML tables have two border models: collapsed (collapse) and separate (separate). Collapsed borders share edges between adjacent cells; separate borders give each cell its own border. At the time of writing, collapsed borders are most widespread and therefore more commonly used by designers.

All current browsers default to collapsed borders except Firefox, which uses separate cells. CSS prescribes no default behavior, so explicitly specify the model. For collapsed borders, set the property border-collapse to collapse:

table {
 border-collapse: collapse;
}

In HTML, the attribute border for the element table sets border width for the table and its cells. For separate widths, use the CSS property border.

Conflicting cell border rules are resolved as follows:

  • If border-style is hidden, all other border sides are hidden too.

  • If border-style is none, other values for border-style take precedence.

  • Except when a cell’s border-style is hidden or none, wider borders outrank narrower ones. With equal widths, styles follow this descending priority: double, solid, dashed, dotted, ridge, outset, groove, inset.

  • For equal widths and styles but different colors, priority follows: cell, row, row group, column, column group, table.

The second model, separate, gives each cell an independently styled border. Horizontal and vertical gaps use the property border-spacing:

table#runoffdata {
 border-collapse: separate;
 border-spacing: 4px 4px;
}

If the property border-collapse is separate, styles for rows, columns and cell groups are ignored. The property empty-cells controls whether empty cells appear.

Separate borders offer more control, but at the time of writing only Firefox, Mozilla, Safari and Netscape 6 support them, not Internet Explorer for Windows. Most designers therefore use collapsed borders.

See also

The CSS 2.1 specification on border models and a detailed discussion of CSS tables in chapter 11 of CSS – Das umfassende Handbuch by Eric A. Meyer (O’Reilly).

  

 

 

 

back to top