Sobes.tech
Intern

What is the structure of a table in HTML?

sobes.tech AI

Answer from AI

The structure of an HTML table is defined by several elements:

  • <table>: The table container.
  • <caption>: An optional table caption.
  • <colgroup> and <col>: Optional elements for grouping columns and applying styles.
  • <thead>: The header section of the table.
  • <tbody>: The main content section of the table.
  • <tfoot>: The footer section of the table.
  • <tr>: A table row.
  • <th>: A header cell in a row.
  • <td>: A data cell in a row.

The <thead>, <tbody>, and <tfoot> elements help group rows by meaning and can be used by browsers to optimize the display of large tables. Inside these sections are <tr> elements. Inside <tr>, there are either <th> (in <thead>, sometimes in <tbody> or <tfoot>), or <td> (in <tbody> and <tfoot>, sometimes in <thead>).

Example of a basic structure:

<table>
  <caption>
    List of products
  </caption>
  <thead>
    <tr>
      <th>Name</th>
      <th>Price</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Product 1</td>
      <td>100</td>
    </tr>
    <tr>
      <td>Product 2</td>
      <td>150</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td colspan="2">Total: 250</td>
    </tr>
  </tfoot>
</table>