Tables

  • Revision slug: Useful_CSS_tips/Tables
  • Revision title: Tables
  • Revision id: 345313
  • Created:
  • Creator: Francis
  • Is current revision? No
  • Comment Removed outdated example that doesn't work since Firefox 4. Added a warning about the changes made in the tbody scrolling behavior with proper bug reference at bugzilla.

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-left: auto;
margin-right: auto;

style to the table itself.

Table with margin-left: auto; margin-right: auto;
cell content cell content cell content
cell content cell content cell content
cell content cell content cell content

Borders

By default, tables are rendered with spacing between cells and borders around each of 1 pixel.

Standard table
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 its cellspacing attribute to 0.

You can remove the spacing by setting the table's cellspacing attribute to 0.

<table cellspacing="0">
<tr>...</tr>
</table>
Table with no cellspacing
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 so, when the gap between cells is 0, they stand next to each other to make thicker internal borders. We can counter this by setting the border-collapse CSS property to "collapse".

<table cellspacing="0" style="border-collapse:collapse;">
<tr>...</tr>
</table>
Table with no cellspacing and collapsed borders
cell content cell content cell content
cell content cell content cell content
cell content cell content cell content

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>

Fixed header

It is no longer possible to set the tbody's height and overflow properties to create a fixed header table with scrollable body. See {{ Bug(28800) }} for more information.

Through Firefox 3.6 (Gecko 1.9.2) it is possible to make a tbody scrollable by setting its overflow property to auto and having a fixed height value. However this behavior is incorrect (according to the CSS2 specification) and was removed later.

{{ languages( { "fr": "fr/Astuces_CSS/Tableaux" } ) }}

Revision Source

<p>&nbsp;</p>
<h3 id="Centering" name="Centering">Centering</h3>
<p>If you want to center a table, it is not correct to use</p>
<dl>
  <dd>
    <code>text-align: center</code>;</dd>
</dl>
<p>in its parent. The correct way is to apply</p>
<dl>
  <dd>
    <code>margin-left: auto;</code><br />
    <code>margin-right: auto;</code></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-left: auto; margin-right: 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 id="Borders" name="Borders">Borders</h3>
<p>By default, tables are rendered with spacing between cells and borders around each of 1 pixel.</p>
<table cellspacing="4">
  <tbody>
    <tr>
      <td colspan="3" style="border: 1px solid black;">Standard table</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 its cellspacing attribute to 0.</p>
<p>You can remove the spacing by setting the table's cellspacing attribute to 0.</p>
<dl>
  <dd>
    <code>&lt;table cellspacing="0"&gt;</code><br />
    <code>&lt;tr&gt;...&lt;/tr&gt;</code><br />
    <code>&lt;/table&gt;</code></dd>
</dl>
<table cellspacing="0">
  <tbody>
    <tr>
      <td colspan="3" style="border: 1px solid black;">Table with no cellspacing</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 <strong>separate</strong>. With this model each cell has its own border so, when the gap between cells is 0, they stand next to each other to make thicker internal borders. We can counter this by setting the <a href="/en/CSS/border-collapse" title="en/CSS/border-collapse">border-collapse</a> CSS property to "collapse".</p>
<dl>
  <dd>
    <code>&lt;table cellspacing="0" style="border-collapse:collapse;"&gt;</code><br />
    <code>&lt;tr&gt;...&lt;/tr&gt;</code><br />
    <code>&lt;/table&gt;</code></dd>
</dl>
<table cellspacing="0" style="border-collapse:collapse;">
  <tbody>
    <tr>
      <td colspan="3" style="border: 1px solid black;">Table with no cellspacing and collapsed borders</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 id="Columns_style" name="Columns_style">Columns style</h3>
<p>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.</p>
<pre>
&lt;style type="text/css"&gt;
td { background-color: #eeeeee; }
td.darkcol { background-color: #cccccc; }
&lt;/style&gt;

&lt;table&gt;
&lt;tr&gt;&lt;td class="darkcol"&gt;Cell&lt;/td&gt;
    &lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td class="darkcol"&gt;Cell&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="darkcol"&gt;Cell&lt;/td&gt;
    &lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td class="darkcol"&gt;Cell&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="darkcol"&gt;Cell&lt;/td&gt;
    &lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td class="darkcol"&gt;Cell&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="darkcol"&gt;Cell&lt;/td&gt;
    &lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td class="darkcol"&gt;Cell&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td class="darkcol"&gt;Cell&lt;/td&gt;
    &lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td class="darkcol"&gt;Cell&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
</pre>
<p>This solution has the advantage to be cross-browser, but it also needs changing the HTML code, to explictly select table cells to be <em>styled</em>.</p>
<p>For browsers supporting <em>adjacent selector</em> there is an alternative pure CSS solution.</p>
<pre>
&lt;style type="text/css"&gt;
/* 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; }
&lt;/style&gt;

&lt;table&gt;
&lt;tr&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;/tr&gt;
&lt;tr&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;td&gt;Cell&lt;/td&gt;&lt;/tr&gt;
&lt;/table&gt;
</pre>
<h3 id="Fixed_header" name="Fixed_header">Fixed header</h3>
<div class="warning">
  <p>It is no longer possible to set the tbody's height and overflow properties to create a fixed header table with scrollable body. See {{ Bug(28800) }} for more information.</p>
</div>
<p>Through Firefox 3.6 (Gecko 1.9.2) it is possible to make a tbody scrollable by setting its overflow property to auto and having a fixed height value. However this behavior is incorrect (according to the CSS2 specification) and was removed later.</p>
<p>{{ languages( { "fr": "fr/Astuces_CSS/Tableaux" } ) }}</p>
Revert to this revision