HTML tagTables

<table>

A table with data arranged in rows and columns.

Display
table
Closing tag
Required
Permitted content
Optionally a <caption>, then <colgroup>, <thead>, <tbody> or <tr> rows, and finally <tfoot>
On this page

Example

#
HTML
<style>
  table { border-collapse: collapse; }
  th, td { border: 1px solid #8886; padding: 6px 12px; text-align: left; }
</style>
<table>
  <caption>Course progress</caption>
  <thead>
    <tr>
      <th scope="col">Course</th>
      <th scope="col">Completed lessons</th>
      <th scope="col">Test score</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th scope="row">HTML and CSS</th>
      <td>24 of 24</td>
      <td>92%</td>
    </tr>
    <tr>
      <th scope="row">JavaScript</th>
      <td>11 of 32</td>
      <td>78%</td>
    </tr>
  </tbody>
</table>
Preview

Definition and usage

#

The <table> element is used to present tabular data: a class schedule, a price list, results, a feature comparison. A table consists of <tr> rows, which in turn consist of cells: header cells <th> and regular cells <td>. You can group rows into <thead>, <tbody>, and <tfoot>, and describe the whole thing with a <caption>.

Tables used to be used to lay out entire pages. Today that’s a bad idea: CSS Grid and Flexbox are for layout, and a table should contain only data that really has rows and columns. Screen readers let users move through a table cell by cell and read out the headers along the way, so a table used for layout makes their job much harder.

By default a table has no borders at all. You add lines, spacing, and colors in CSS, most often starting with border-collapse: collapse, which merges the double edges of adjacent cells into a single line.

Syntax

#
Syntax
<table>
  <tr>
    <th>Header</th>
  </tr>
  <tr>
    <td>Data</td>
  </tr>
</table>

Attributes

#
  • border

    1 (obsolete)

    An obsolete attribute that used to draw a border around the table and its cells. Browsers still understand it, which is why it still shows up in old tutorials, but set borders in CSS. cellpadding, cellspacing, width, align, and bgcolor are obsolete in the same way. The example shows how to get the same effect in CSS.

The element also supports all global attributes, such as class, id, style and hidden.

Attributes in practice

#

border

1 (obsolete)

An obsolete attribute that used to draw a border around the table and its cells. Browsers still understand it, which is why it still shows up in old tutorials, but set borders in CSS. cellpadding, cellspacing, width, align, and bgcolor are obsolete in the same way. The example shows how to get the same effect in CSS.

HTML
<style>
  .scores { border-collapse: collapse; }
  .scores th, .scores td { border: 1px solid #8886; padding: 4px 10px; }
</style>
<table class="scores">
  <tr><th>Student</th><th>Points</th></tr>
  <tr><td>Ola</td><td>980</td></tr>
  <tr><td>Kuba</td><td>870</td></tr>
</table>
Preview

Default CSS

#

Most browsers display this element with these default styles:

CSS
table {
  display: table;
  box-sizing: border-box;
  border-collapse: separate;
  border-spacing: 2px;
  text-indent: initial;
}

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Wrap a wide table in a <div> with overflow-x: auto. On a phone it will scroll horizontally instead of stretching the whole page.
  • Write column and row headers in <th> with the scope attribute, not in bold <td> cells. That way a screen reader says what each value refers to.
  • Don’t use tables for page layout. Build columns and grids with CSS Grid or Flexbox.

Related entries

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.