How to center a table

Learn how to center a table using CSS.

Centered table:

Firstname Lastname Age
Jill Smith 50
Eve Jackson 94
John Doe 80

How to center a table

Step 1 - Add HTML:

<table class="center">
  <tr>
    <th>Firstname</th>
    <th>Lastname</th>
    <th>Age</th>
  </tr>
  <tr>
    <td>Jill</td>
    <td>Smith</td>
    <td>50</td>
  </tr>
  <tr>
    <td>Eve</td>
    <td>Jackson</td>
    <td>94</td>
  </tr>
  <tr>
    <td>John</td>
    <td>Doe</td>
    <td>80</td>
  </tr>
</table>

Step 2 - Add CSS:

To center the table, set the left and right margins to auto:

.center {
  margin-left: auto;
  margin-right: auto;
}

Try it yourself

Note that if the width is set to 100% (full width), the table cannot be centered.

Related Pages

Tutorial:CSS Table