HTML Tables

Learn how to create and use tables in HTML

HTML Tables

HTML tables allow you to arrange data into rows and columns. They are created using the <table> tag, along with other related tags.

Basic Table Structure

Here's an example of a basic HTML table:

<table>
  <tr>
    <th>Header 1</th>
    <th>Header 2</th>
  </tr>
  <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>

This will create the following table:

Header 1 Header 2
Row 1, Cell 1 Row 1, Cell 2
Row 2, Cell 1 Row 2, Cell 2

Table Tags

Here are the main tags used in HTML tables:

  • <table>: Defines a table
  • <tr>: Defines a table row
  • <th>: Defines a table header
  • <td>: Defines a table cell
  • <caption>: Defines a table caption
  • <thead>: Groups the header content in a table
  • <tbody>: Groups the body content in a table
  • <tfoot>: Groups the footer content in a table

Table Attributes

Tables can have various attributes to modify their appearance or behavior:

  • border: Specifies the width of the border around the table.
  • cellpadding: Sets the space between the cell content and cell borders.
  • cellspacing: Defines the space between cells.
  • width: Specifies the width of the table.