Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to center a table in HTML

Understanding HTML Tables

Let's start by understanding what a table is in HTML. In simple terms, a table is a structured set of data arranged in rows and columns, just like a spreadsheet. In HTML, we use specific tags or codes to create these rows and columns.

For example, here's a basic HTML table:

<table>
  <tr>
    <td>Row 1, Cell 1</td>
    <td>Row 1, Cell 2</td>
  </tr>
  <tr>
    <td>Row 2, Cell 1</td>
    <td>Row 2, Cell 2</td>
  </tr>
</table>

In this example, <table> is the tag that starts and ends the table. <tr> is the tag that starts and ends a table row, and <td> is the tag that starts and ends a table cell or data cell.

The Need for Centering

Now, sometimes, you might want to center your table on the webpage. Just like when you're writing a document and you decide to center a paragraph to make it stand out, centering your HTML table can make it more visible and appealing on your webpage.

Centering Tables: The Old Way

Once upon a time, in the early days of the web, centering a table was as simple as using the <center> tag. You would wrap your table with this tag, and voila, your table would be centered.

<center>
  <table>
    <!-- Your table data here -->
  </table>
</center>

However, the <center> tag is now considered obsolete and is not recommended for use in modern web development. This is because it doesn't promote separation of concerns, a concept in programming that encourages separating the design and presentation (the look) from the content and structure (the data) for easier maintenance and better scalability.

Centering Tables: The Modern Way

Today, the recommended way to center a table is to use CSS (Cascading Style Sheets), a language used for describing the look and formatting of a document written in HTML.

There are two main methods to center a table using CSS: the margin property and the flexbox model.

Using the Margin Property

The CSS margin property is used to create space around elements. When you set the left and right margins to auto and specify a width, the browser will automatically distribute equal margins on both sides, effectively centering your table. Here's how you can do it:

<style>
  .center-table {
    margin-left: auto;
    margin-right: auto;
    width: 50%;
  }
</style>

<table class="center-table">
  <!-- Your table data here -->
</table>

In this example, we first define a style rule for a class named .center-table using the <style> tag. Then, we assign this class to our table using the class attribute. The result is a table centered within its containing element and occupying 50% of its width.

Using the Flexbox Model

The CSS flexbox model is a more recent addition to CSS, offering more flexibility and control over element layout and alignment. To center a table using flexbox, you can make the table's containing element a flex container and use the justify-content and align-items properties to center the table both horizontally and vertically:

<style>
  .flex-container {
    display: flex;
    justify-content: center;
    align-items: center;
    height: 100vh; /* Optional: this makes the container fill the entire viewport height */
  }
</style>

<div class="flex-container">
  <table>
    <!-- Your table data here -->
  </table>
</div>

In this example, the .flex-container class is applied to a <div> element, which is a generic container element often used for styling purposes. The table inside this flex container is then centered both horizontally and vertically.

Conclusion

Centering a table in HTML can be done in various ways. While the old <center> tag method is no longer recommended, using CSS to apply the margin property or the flexbox model can help you achieve the same result in a more modern and flexible way. Remember, practice makes perfect! So, try these methods out on your own to get the hang of it. Happy coding!