How to center a table in html
Centering a table can help in organizing content neatly and improving readability. Our tutorial shows you multiple techniques to center a table, including using CSS and Bootstrap for easy implementation.
Centering a table with Bootstrap
Bootstrap makes centering elements easy with its utility classes. Here’s how you can center a table using Bootstrap’s flex utilities.
Using Bootstrap's d-flex
and justify-content-center
classes, you can center a table
within a parent container. The d-flex
class makes the container a flexbox, and
justify-content-center
centers its content horizontally.
# | First | Last | Handle |
---|---|---|---|
1 | Mark | Otto | @mdo |
2 | Jacob | Thornton | @fat |
3 | Larry the Bird |
<div class="d-flex justify-content-center">
<table class="table" style="width: 400px;">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td colspan="2">Larry the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
</div>
Centering a table with CSS flexbox
Using pure CSS, you can achieve similar results with the Flexbox layout.
y applying CSS Flexbox properties to the container of the table, you can center the table horizontally. The
display: flex;
property sets up a flex context, and justify-content: center;
centers the
flex items horizontally.
# | First | Last | Handle |
---|---|---|---|
1 | Mark | Otto | @mdo |
2 | Jacob | Thornton | @fat |
3 | Larry the Bird |
<div class="flex-container">
<table class="table" style="width: 400px;">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td colspan="2">Larry the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
</div>
.flex-container {
display: flex;
justify-content: center;
}
Centering a table with CSS margin
A simple method involves using the CSS margin
property.
By setting the left and right margins of the table to auto
, the table is centered horizontally within
its parent container.
# | First | Last | Handle |
---|---|---|---|
1 | Mark | Otto | @mdo |
2 | Jacob | Thornton | @fat |
3 | Larry the Bird |
<div class="table-container">
<table class="table" style="width: 400px;">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td colspan="2">Larry the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
</div>
.table-container table {
margin-left: auto;
margin-right: auto;
}
Centering a table ysing HTML center tag
The deprecated center
tag can still be used for simplicity in non-strict HTML documents.
The center
tag is an older HTML element that centers its content horizontally. It’s not recommended
for modern web design but can be used in legacy projects.
# | First | Last | Handle |
---|---|---|---|
1 | Mark | Otto | @mdo |
2 | Jacob | Thornton | @fat |
3 | Larry the Bird |
<center>
<table class="table" style="width: 400px;">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">First</th>
<th scope="col">Last</th>
<th scope="col">Handle</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>Mark</td>
<td>Otto</td>
<td>@mdo</td>
</tr>
<tr>
<th scope="row">2</th>
<td>Jacob</td>
<td>Thornton</td>
<td>@fat</td>
</tr>
<tr>
<th scope="row">3</th>
<td colspan="2">Larry the Bird</td>
<td>@twitter</td>
</tr>
</tbody>
</table>
</center>