Skip to main content

HTML Tags

HTML <table> Tag

Written by Updated HTML <table> Tag exercises

The table tag holds a table of data: rows of cells, with headings, organized in columns. Inside it go tr rows, th header cells and td data cells, and optionally a caption, thead, tbody and tfoot.

Use tables for data that really has rows and columns, like prices or schedules, never to lay out a page. The full picture with examples is in HTML Tables.

Syntax

html

<table>
  <tr>
    <th>Heading 1</th>
    <th>Heading 2</th>
  </tr>
  <tr>
    <td>Data 1</td>
    <td>Data 2</td>
  </tr>
</table>

<table> has no attributes you should still use. Old ones like border, cellpadding, cellspacing, width and bgcolor are obsolete; set borders, spacing and colors with CSS instead. See Table Styling & Borders.

Example

html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Table Tag Example</title>
  <style>
    table {
      width: 60%;
      border-collapse: collapse;
      margin: 20px auto;
    }
    th, td {
      border: 1px solid #555;
      padding: 10px;
      text-align: center;
    }
    th {
      background-color: #29AB87;
      color: white;
    }
  </style>
</head>
<body>

  <h2>HTML Table Example</h2>

  <table>
    <tr>
      <th>Language</th>
      <th>Type</th>
      <th>Year Introduced</th>
    </tr>
    <tr>
      <td>HTML</td>
      <td>Markup</td>
      <td>1993</td>
    </tr>
    <tr>
      <td>CSS</td>
      <td>Style Sheet</td>
      <td>1996</td>
    </tr>
    <tr>
      <td>JavaScript</td>
      <td>Programming</td>
      <td>1995</td>
    </tr>
  </table>

</body>
</html>

A table lists three languages with their type and the year they were introduced, with a header row on top and borders from CSS.

A well-built table

Caption, header scope and row groups

html

<table>
  <caption>Web languages</caption>
  <thead>
    <tr><th scope="col">Language</th><th scope="col">Year</th></tr>
  </thead>
  <tbody>
    <tr><td>HTML</td><td>1993</td></tr>
    <tr><td>CSS</td><td>1996</td></tr>
  </tbody>
</table>

The caption names the table, scope tells screen readers which cells each header labels, and the row groups make styling easier.

Browser Support

Chrome
Edge
Firefox
Safari
Opera
IE9+
YesYesYesYesYesYes

Good to know

  • border-collapse: collapse on the table gives single, clean cell borders instead of double lines.
  • Wrap wide tables in a box with overflow-x: auto, so they scroll on phones instead of breaking the page.

HTML <table> Tag exercises

Build a table

Make a table with one row of two cells: Ada and Engineer.

Result

Check your answer to see it here.