I am trying to center a table using bootstrap.
Here is the html:
<div class="container-fluid">
<table id="total votes" class="table table-hover text-centered">
<thead>
<tr>
<th>Total votes</th>
<th> = </th>
<th>Voter A</th>
<th> + </th>
<th>Voter B</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{ total_votes }}</td>
<td> = </td>
<td>{{ total_voter_a }}</td>
<td> + </td>
<td>{{ total_voter_b }}</td>
</tr>
</tbody>
</table>
</div>
However, no matter how I adjust the css
, the table is still aligned to the left. I'm sure I am missing something simple. I also thought container-fluid
would make the table span across the whole page.
here is the css:
.table th {
text-align: center;
}
.table {
border-radius: 5px;
width: 50%;
float: none;
}
By adding the ” text-center” class of Bootstrap 3 We can use the “text-center” class of bootstrap 3 for the center-alignment of an element. So in our td when we add “text-center” class, and then our table text goes to the center.
Use d-flex justify-content-center on your column div. This will center everything inside that column. If you have text and image inside the column, you need to use d-flex justify-content-center and text-center .
We set th { text-center: left; } , which is more specific than the class on the parent and thus prevents the header cell from inheriting the centering styles. Putting the . text-center class directly on the <th> should work though (confirmed in the docs).
You're missing the margin:auto
.table {
border-radius: 5px;
width: 50%;
margin: 0px auto;
float: none;
}
Example
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With