Altcademy - a Forbes magazine logo Best Coding Bootcamp 2023

How to make tables in HTML

Understanding Tables: The Foundation

If you are learning programming, chances are you have come across the term 'tables'. In the HTML context, tables are used to organize and present data in a structured manner, with rows and columns. Picture a typical Excel spreadsheet, that's your table. Just like we use different cells in Excel to store different pieces of data, HTML tables work similarly.

The Anatomy of an HTML Table

HTML tables are made up of several integral parts. Here is a basic structure of an HTML table:

<table>
  <tr>
    <td>Content</td>
  </tr>
</table>

This is the simplest form of an HTML table. It contains a single cell. Here, <table> is the HTML tag that starts a table. <tr> stands for table row and <td> stands for table data. The <td> tag creates a cell in which you can put content - text, images, links, other tables, etc.

Creating a Basic Table

Let's make a simple table with two rows and two columns:

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

This table has two rows, each containing two cells. The cells are filled with text to indicate their position. The first row contains cells with 'Row 1, Column 1' and 'Row 1, Column 2'. The second row contains 'Row 2, Column 1' and 'Row 2, Column 2'.

HTML Table Headers

Just like how a book has a title to tell us what it's about, tables often have headers to inform us what the data in the columns represents. For this, HTML provides the <th> tag for 'table header'. Here's how we can add headers to our table:

<table>
  <tr>
    <th>Header 1</th>
    <th>Header 2</th>
  </tr>
  <tr>
    <td>Row 1, Column 1</td>
    <td>Row 1, Column 2</td>
  </tr>
  <tr>
    <td>Row 2, Column 1</td>
    <td>Row 2, Column 2</td>
  </tr>
</table>

The <th> tags are included in the first row of the table, and give us the headers 'Header 1' and 'Header 2'.

Spanning Rows and Columns

Occasionally, you may want a cell to span multiple rows or columns. This can be achieved using the colspan and rowspan attributes. colspan is used when you want a cell to span across multiple columns and rowspan when you want it to span across multiple rows.

Here's an example:

<table>
  <tr>
    <th>Header 1</th>
    <th colspan="2">Header 2</th>
  </tr>
  <tr>
    <td>Row 1, Column 1</td>
    <td>Row 1, Column 2</td>
    <td>Row 1, Column 3</td>
  </tr>
</table>

In this example, 'Header 2' spans across two columns.

Styling your Table

While the function of a table is to present data, it also needs to be visually appealing and readable. HTML provides a number of ways to style your table. Here's a basic example:

<table style="width:100%">
  <tr>
    <th>Header 1</th>
    <th>Header 2</th>
  </tr>
  <tr>
    <td>Row 1, Column 1</td>
    <td>Row 1, Column 2</td>
  </tr>
  <tr>
    <td>Row 2, Column 1</td>
    <td>Row 2, Column 2</td>
  </tr>
</table>

In this example, the style attribute is used to set the width of the table to 100% of its parent element. This way, the table will always take up the entire available width.

Conclusion

Tables in HTML provide a way to organize and display data effectively. With the basic structure, headers, rowspan, colspan and styling features, you can create a table that fits your needs. As with any HTML element, the key to mastering tables is consistent practice. So go ahead, start coding and experiment with different table structures and styles. Happy coding!