Align table contents to the center using bootstrap

Tables are a fundamental part of web design, often used for displaying data in a structured format. Aligning table contents can sometimes be tricky, but Bootstrap, with its powerful grid system and utilities, makes this task straightforward. In this blog post, we will guide you through the process of aligning table contents using Bootstrap.

Why Bootstrap?

Bootstrap is a popular front-end framework that simplifies the design and development of responsive and mobile-first websites. It includes a comprehensive set of CSS and JavaScript components, including a robust grid system, pre-designed UI components, and utility classes, making it easier to create consistent and visually appealing web pages.

Getting Started

Before we begin, ensure you have Bootstrap integrated into your WordPress site. You can do this by either downloading Bootstrap and including it in your theme or by using a CDN.

To include Bootstrap via CDN, add the following lines to your html file:

<html>
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link href="https://stackpath.bootstrapcdn.com/bootstrap/4.5.2/css/bootstrap.min.css" rel="stylesheet">
    <title>Document</title>
</head>
<body>

Creating and Aligning Table Contents

Let’s create a simple table and see how we can align its contents using Bootstrap.

Basic Table Structure

First, create a table structure using HTML:

<table>
  <thead>
    <tr>
      <th>Column 1</th>
      <th>Column 2</th>
      <th>Column 3</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Row 1, Col 1</td>
      <td>Row 1, Col 2</td>
      <td>Row 1, Col 3</td>
    </tr>
    <tr>
      <td>Row 2, Col 1</td>
      <td>Row 2, Col 2</td>
      <td>Row 2, Col 3</td>
    </tr>
  </tbody>
</table>

It will look like this :


Aligning Table Contents

Bootstrap provides several classes to align table contents. You can align text within table cells using classes such as .text-left, .text-center,

<div class="container mt-5">
  <table class="table table-bordered table-custom">
    <thead>
      <tr>
        <th class="text-center">Column 1</th>
        <th class="text-center">Column 2</th>
        <th class="text-center">Column 3</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <td class="text-center">Row 1, Col 1</td>
        <td class="text-center">Row 1, Col 2</td>
        <td class="text-center">Row 1, Col 3</td>
      </tr>
      <tr>
        <td class="text-center">Row 2, Col 1</td>
        <td class="text-center">Row 2, Col 2</td>
        <td class="text-center">Row 2, Col 3</td>
      </tr>
    </tbody>
  </table>
</div>

The final table will look like this :

Conclusion

Aligning table contents is essential for improving the readability and visual appeal of your data. With Bootstrap, you can easily align table contents using utility classes like .text-left, .text-center, and .text-right ,  Additionally, Bootstrap offers various styling options to enhance the overall appearance of your tables.

Happy Coding!

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top