Tables

Centering

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; to the table itself:

#foo { 
    margin-left: auto; 
    margin-right: auto; 
}

#foo td {
    padding: 1em;
    border: solid gray thin;
}
<table id="foo">
  <tr>
   <td>Alligator</td>
   <td>Pumpkin</td>
  </tr>
  <tr>
   <td>Turtle</td>
   <td>Pomegranate</td>
  </tr>
</table>

Border-collapse

To collapse the space between cells and prevent adjacent cell borders from doubling up, set the border-collapse CSS property of the table element to "collapse", eg border-collapse: collapse;. Note: this overrides any cellspacing attribute of the table element.

#bar {
    border-collapse: collapse;
}

#bar td {
    border: solid gray thin;
    padding: 1em;
}
<table id="bar">
  <tr>
   <td>Alligator</td>
   <td>Pumpkin</td>
  </tr>
  <tr>
   <td>Turtle</td>
   <td>Pomegranate</td>
  </tr>
</table>

Columns

You may use adjacent sybling selector CSS to style individual columns, eg td + td + td sellects all columns from column 3 right. If more precise control is needed, consider using HTML colgroup and col elements.

#baz {
    border-collapse: collapse;
}

#baz td {
    border: solid gray thin;
    padding: 1em;
    text-align: left;
}

#baz td + td + td {
    background-color: khaki; 
    text-align: right;
}
<table id="baz">
  <tr>
   <td>Alligator</td><td>win</td><td>3</td>
  </tr>
  <tr>
   <td>Turtle</td><td>win</td><td>2000</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.

Document Tags and Contributors

Contributors to this page: Francis, jswisher, Nickolay, Callek, rogerhc, Waldo, Lombardp, Kyodev, gregtyler
Last updated by: rogerhc,