Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Centering table on a page on bootstrap [duplicate]

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;
}
like image 223
H C Avatar asked Dec 14 '15 15:12

H C


People also ask

How do I center a table on bootstrap on page?

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.

How do I center data in bootstrap?

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 .

How do I center a table header in bootstrap?

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).


1 Answers

You're missing the margin:auto

.table {
    border-radius: 5px;
    width: 50%;
    margin: 0px auto;
    float: none;
}

Example

like image 137
Antonio Smoljan Avatar answered Oct 12 '22 05:10

Antonio Smoljan