How to center align a table in bootstrap?

Hi Friends 👋,

Welcome To aGuideHub! ❤️

In this tutorial, we will learn how to center align a table in bootstrap.

Tables are widely used while creating web pages. Tables are used for constructing calendars, comparisons of any data, date pickers, etc. Bootstrap provides an easy way to create and modify Tables.

To create a table, add .table class to <table> tag. Add header elements using <th> tag. The <thead> tag denotes the head of the table. For adding contents use <td> tag. <tr> tag denotes the row of table.

Table of contents

  • Includes bootstrap view
  • Includes bootstrap library
  • Define its class name

Step 1: Includes bootstrap view

To ensure proper rendering and touch zooming for all devices, add the responsive viewport meta tag to your <head>.

    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

Step 1: Includes bootstrap library

First of all, load the Bootstrap framework CSS into the head tag of your webpage.

<!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous">

Step 2: Define its class name

After that, create align table is center with a class name .mx-auto .w-auto.

    <div class="table-responsive">
        <table class="table table-striped table-hover mx-auto w-auto">
            <thead>
                <tr>
                    <th scope="col">column1</th>
                    <th scope="col">column2</th>
                    <th scope="col">column3</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>item1</td>
                    <td>item2</td>
                    <td>item3</td>
                </tr>
                <tr>
                    <td>item1</td>
                    <td>item2</td>
                    <td>item3</td>
                </tr>
            </tbody>
        </table>
    </div>

Example.

Let’s look at the following example to understand how it basically works:

<!doctype html>
<html lang="en">
  <head>
    <!-- Required meta tags -->
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

    <!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" integrity="sha384-zCbKRCUGaJDkqS1kPbPd7TveP5iyJE0EjAuZQTgFLD2ylzuqKfdKlfG/eSrtxUkn" crossorigin="anonymous">

    <title>Bootstrap</title>
  </head>
  <body>
    <div class="table-responsive">
        <table class="table table-striped table-hover mx-auto w-auto">
            <thead>
                <tr>
                    <th scope="col">column1</th>
                    <th scope="col">column2</th>
                    <th scope="col">column3</th>
                </tr>
            </thead>
            <tbody>
                <tr>
                    <td>item1</td>
                    <td>item2</td>
                    <td>item3</td>
                </tr>
                <tr>
                    <td>item1</td>
                    <td>item2</td>
                    <td>item3</td>
                </tr>
            </tbody>
        </table>
    </div>

    <!-- Option 1: jQuery and Bootstrap Bundle (includes Popper) -->
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js" integrity="sha384-DfXdz2htPH0lsSSs5nCTpuj/zy4C+OGpamoFVy38MVBnE+IbbVYUew+OrCXaRkfj" crossorigin="anonymous"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js" integrity="sha384-fQybjgWLrvvRgtW6bFlB7jaZrFsaBXjsOMm/tB9LTS58ONXgqbR9W8oWht/amnpF" crossorigin="anonymous"></script>

    
  </body>
</html>

Check the output of the above code example.

Bootstrap, Center, Table

All the best 👍

Follow me on Twitter