Tables

  • Revision slug: Useful_CSS_tips/Tables
  • Revision title: Tables
  • Revision id: 105942
  • Created:
  • Creator: Lombardp
  • Is current revision? No
  • Comment /* Centering */

Revision Content

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

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

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.

(to be written)

Revision Source

<p>
</p>
<h3 name="Centering"> Centering </h3>
<p>If you want to center a table, it is not correct to use<br>
</p>
<dl><dd> <code>text-align: center</code><br>
</dd></dl>
<p>in its parent. The correct way is to apply<br>
</p>
<dl><dd> <code>margin: 0px auto 0px auto</code><br>
</dd></dl>
<p>style to the table itself.
</p>
<table cellspacing="0" style="border-collapse:collapse; margin: 0px auto 0px auto;">
<tbody><tr><td colspan="3" style="border: 1px solid black;">Table with margin: 0px auto 0px auto;</td></tr>
<tr><td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td></tr>
<tr><td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td></tr>
<tr><td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td></tr>
</tbody></table>
<h3 name="Borders"> Borders </h3>
<p>This is a standard table with non-zero cellspacing and 1px cell borders.
</p>
<table cellspacing="4">
<tbody><tr><td colspan="3" style="border: 1px solid black;">Table with cellspacing="4"</td></tr>
<tr><td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td></tr>
<tr><td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td></tr>
<tr><td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td></tr>
</tbody></table>
<p>If you want to transform the table in a grid, you set cellspacing to 0 and the rendered tables is this.
</p>
<table cellspacing="0">
<tbody><tr><td colspan="3" style="border: 1px solid black;">Table with cellspacing="0"</td></tr>
<tr><td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td></tr>
<tr><td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td></tr>
<tr><td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td></tr>
</tbody></table>
<p>The result is not as expected, and the reason is that the default border model is set to <b>separate</b>.  With this model each cell has its own border, also if cells spacing is zero. In order to have a grid-like rendering, the <b>collapse</b> border model has to be set.
</p>
<table cellspacing="0" style="border-collapse:collapse;">
<tbody><tr><td colspan="3" style="border: 1px solid black;">Table with cellspacing="0" <br>and border-collapse:collapse;</td></tr>
<tr><td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td></tr>
<tr><td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td></tr>
<tr><td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td>
    <td style="border: 1px solid black;">cell content</td></tr>
</tbody></table>
<h3 name="Fixed_header"> Fixed header </h3>
<p>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.
</p><p>(to be written)
</p>
Revert to this revision