Skip to content
Tutorials Center
My learning

Topics

HTML Reference ยท Tables

The <table> element

Reference for the HTML <table> element: the order of its children, header cells, row groups, accessibility, why not to use tables for layout, and technical details.

The <table> element represents tabular data: information that makes sense in rows and columns, where each cell relates to both a row heading and a column heading. Timetables, price lists, sports results and comparison charts are typical examples.

A table is built from rows (<tr>) that contain header cells (<th>) and data cells (<td>). Rows can be grouped into a head, one or more bodies and a foot, and the whole table can have a <caption>.

HTML
<table>
  <caption>Title of the table</caption>
  <thead>
    <tr><th scope="col">Column A</th><th scope="col">Column B</th></tr>
  </thead>
  <tbody>
    <tr><td>a1</td><td>b1</td></tr>
  </tbody>
</table>

Try it

A complete table

The first column uses <th scope="row"> so each hut name labels its row, and the header row uses scope="col". Borders, colors and alignment all come from CSS.

Order of the children

The parts of a table must appear in this order:

  1. An optional <caption>.
  2. Zero or more <colgroup> elements.
  3. An optional <thead>.
  4. Either zero or more <tbody> elements, or one or more <tr> elements directly in the table (not both).
  5. An optional <tfoot>.

Script-supporting elements (<script> and <template>) may appear between these parts. If you write rows directly inside <table>, the HTML parser wraps them in an implied <tbody>, so a CSS selector like table > tr never matches in an HTML document.

Attributes

This element has no attributes of its own in modern HTML. It accepts the global attributes.

Older presentational attributes are obsolete. Use these CSS replacements instead:

Obsolete attribute CSS replacement
border border on table, th and td
cellpadding padding on th and td
cellspacing border-spacing (or border-collapse: collapse)
width width
align margin-inline: auto to center the table
bgcolor background-color
frame, rules border on the table and on cells or rows
summary Explain the table in the <caption> or in nearby text

Usage notes

  • Data, not layout. Don't use tables to place things side by side on a page. Screen readers announce layout tables as data, and they don't reflow on small screens. Use CSS grid or flexbox for layout.
  • Mark up headers. Use <th> for every heading cell and set scope (col or row) when it is not obvious. For complex tables with headers at several levels, use the headers attribute on cells.
  • Add a caption. A <caption> gives the table an accessible name and tells everyone what it contains.
  • Wide tables on small screens. Put the table in a wrapper with overflow-x: auto so it scrolls sideways instead of breaking the page layout. Give the wrapper tabindex="0" and a label so keyboard users can scroll it.
  • Group rows. <thead>, <tbody> and <tfoot> help styling, and browsers can repeat the head and foot on each printed page.
  • Changing display can remove semantics. Setting display: block or grid on a table or its parts can make some browsers drop the table role. If you restyle a table heavily, test it with a screen reader.

Technical summary

Content categories Flow content, palpable content
Permitted content In this order: an optional <caption>, zero or more <colgroup>, an optional <thead>, then either zero or more <tbody> or one or more <tr>, then an optional <tfoot>; optionally mixed with script-supporting elements
Tag omission None; both the start and end tags are required
Permitted parents Any element that accepts flow content
Implicit ARIA role table
DOM interface HTMLTableElement
Default display table

Browser support

Supported in every browser.