Skip to main content

What does the <table> tag do?

The <table> tag in HTML is used to create a table - a structure made up of rows and columns that hold data.

It is a container for all the table's elements: rows (<tr>), cells (<td>), headers (<th>), and logical sections (<thead>, <tbody>, <tfoot>).


Main function

The <table> tag groups and displays data in tabular form - like a grid where rows and columns intersect in cells.


Example:

html
<table> <tr> <th>Name</th> <th>Age</th> </tr> <tr> <td>Maria</td> <td>25</td> </tr> <tr> <td>Oleh</td> <td>30</td> </tr> </table>

What happens here:

  • <table> - creates the table.
  • <tr> - adds a row.
  • <th> - makes a cell a header (bold, centered).
  • <td> - adds a regular data cell.

<table> tag attributes (outdated, but still seen)

  • border - adds a border around the table.

    html
    <table border="1">...</table>
  • cellpadding - sets the inner padding inside cells.

  • cellspacing - the spacing between cells.

Nowadays CSS is used to style tables instead of these attributes.


Example with CSS:

html
<table> <tr> <th>Item</th> <th>Price</th> </tr> <tr> <td>Bread</td> <td>$40</td> </tr> </table> <style> table { border-collapse: collapse; width: 300px; } th, td { border: 1px solid #333; padding: 8px; text-align: left; } </style>

Conclusion:

The <table> tag creates a table and serves as the main container for all its elements. It does not display data by itself, but it organizes it into rows and columns, providing a structured and visually clear representation of information.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.

What does the <table> tag do?: HTML Interview Question