Styling Tables
General Considerations with Tables
- Padding:
- The
paddingproperty replaces thecellpaddingattribute for<table>. - If neither the
paddingproperty nor thecellpaddingattribute is specified, a browser default greater than0will be used forcellpadding. - Comparing the two approaches, the
paddingproperty is more flexible because different sides can be given unique values and cells can be styled differently; thecellpaddingattribute applies to all four sides of the cell and to all cells equally. - Be sure to style the
<th>and<td>, not<table>or<tr>.
- The
- Cellspacing:
border-spacingproperty replaces thecellspacingattribute.
- Margins:
- Internal table elements (such as
<tr>,<th>, and<td>) do not have margins, so avoid setting themarginproperty for them. - Some browsers may try to render that margin, while others will not.
- Internal table elements (such as
- Border, Widths, and Heights:
- These can be specified for
<table>,<th>, and<td>; avoid these for<tr>.
- These can be specified for
table-layout Property
- The primary benefit of
table-layoutis changing the rendering algorithm to speed up the rendering of large data tables. - The secondary benefit of changing the rendering algorithm is consistency across tables. Two (or more) separate tables can be styled to have the same column widths and changing the rendering algorithm ensures that they match one another.
- Using the
fixedvalue (algorithm) means that the table can start rendering after the first row is read by the browser, rather than waiting until the entire table is read.- The
fixedalgorithm uses the cell widths, borders, (cell)padding, and (cell/border)spacing for the first row's cells as the basis for the entire table. - Cell content is not factored into the rendering, which means that content wider than its cell will either overlap cells to the right or will be clipped (not displayed). The exact result differs somewhat by browser.
- Because of this overlap / overflow involving cell content, extensive testing is recommended.
- The
- The default
autovalue (algorithm) allows content to impact cell width, in order to keep content in its cell.
| Property | Usage and Effect | Values | Recommendations |
|---|---|---|---|
| table-layout | Determines how a table renders its column widths.
Apply to |
|
For consistent rendering cross-browser make sure that the <table> element has a defined width too.
Always check to make sure that content is not being truncated. Narrow widths will pose difficulties. |
table-layout Example
See the Pen Table Layout by Jason Withrow (@jwithrow) on CodePen.
empty-cells Property
- Browsers have traditionally collapsed (rendered without defined edges) any cells that are empty.
- To get around this web developers have inserted a single non-breaking space (
or ) into the cell to prevent it from collapsing (any character, other than a regular whitespace, prevents collapse). - The
empty-cellsproperty instructs cells to either show borders for empty cells or to hide borders for empty cells.
| Property | Usage and Effect | Values | Recommendations |
|---|---|---|---|
| empty-cells | Determines whether an empty table cell displays or hides borders. |
|
Can be beneficial if there are a number of empty cells in a table. |
empty-cells Example
See the Pen Empty Cells by Jason Withrow (@jwithrow) on CodePen.
border-spacing Property
- This property replaces the
cellspacingattribute. - As with other CSS properties, if
cellspacingis specified as well asborder-spacingthe CSS takes priority and the HTML attribute is ignored. - If borders are instructed to collapse (via the
border-collapseproperty) then theborder-spacingis ignored.
| Property | Usage and Effect | Values |
|---|---|---|
| border-spacing | Determines the space between table cells.
Apply to <table> and elements displayed as tables (display: table). |
|
border-spacing Example
See the Pen Border Spacing by Jason Withrow (@jwithrow) on CodePen.
border-collapse Property
- There are two ways to render cell borders (borders for
<th>and<td>, not the traditional border associated with<table>). - The default approach is to have them be
separate, which means that borders of adjacent cells add together (so that if all cells have 2 pixel borders, where cell walls touch there would be 4 pixels of border). - An alternate approach is to
collapsethe borders, which means that adjacent borders do not add together.- The border value specified for the cells is truly what is shown in the rendered output.
- Top and left borders of cells are removed to make things fit, so that in the previous example the total border would be 2 pixels and not 4 pixels.
- Collapsed borders will negate
border-spacing, because the border space is eliminated.
| Property | Usage and Effect | Values | Recommendations |
|---|---|---|---|
| border-collapse | Determines whether table cells share borders or have distinct borders.
Apply to |
|
Essential if table cells have borders shown. |
border-collapse Example
See the Pen Border Collapse by Jason Withrow (@jwithrow) on CodePen.
caption-side Property
- By default
<caption></caption>is centered above the table, but via CSS the side it is on can be switched to the bottom. - Horizontal alignment is adjusted via the
text-alignproperty.
| Property | Usage and Effect | Values | Recommendations |
|---|---|---|---|
| caption-side | Determines whether a caption renders above or below the table. |
|
Useful if captions are being added. |
caption-side Example
See the Pen Caption Side by Jason Withrow (@jwithrow) on CodePen.