Support0

HTML tables playground

Build tabular data with table, caption, th, scope, thead, tbody, tfoot, colgroup, colspan, and rowspan while previewing and copying the result.

Ask AI

HTML tables playground

Response generated with openai/gpt-5.4-nano. AI can make mistakes. Always review the result.
Web project status
Phase Task Owner Status
Planning Scope Ana Complete
Research Luis Complete
Development Semantic HTML Mei In progress
Accessibility Sam Pending
Completed tasks 2 of 4
HTML
<div class="table-wrapper">
  <table class="data-table">
    <caption>Web project status</caption>

    <colgroup>
      <col>
      <col>
      <col>
      <col class="highlighted-column">
    </colgroup>

    <thead>
      <tr>
        <th scope="col">Phase</th>
        <th scope="col">Task</th>
        <th scope="col">Owner</th>
        <th scope="col">Status</th>
      </tr>
    </thead>

    <tbody>
      <tr>
        <th scope="row" rowspan="2">Planning</th>
        <td>Scope</td>
        <td>Ana</td>
        <td>Complete</td>
      </tr>
      <tr>
        <td>Research</td>
        <td>Luis</td>
        <td>Complete</td>
      </tr>
      <tr>
        <th scope="row" rowspan="2">Development</th>
        <td>Semantic HTML</td>
        <td>Mei</td>
        <td>In progress</td>
      </tr>
      <tr>
        <td>Accessibility</td>
        <td>Sam</td>
        <td>Pending</td>
      </tr>
    </tbody>

    <tfoot>
      <tr>
        <th colspan="3" scope="row">Completed tasks</th>
        <td>2 of 4</td>
      </tr>
    </tfoot>
  </table>
</div>
CSS
.table-wrapper {
  overflow-x: auto;
}

.data-table {
  inline-size: 100%;
  border-collapse: collapse;
  color: CanvasText;
}

.data-table caption {
  margin-block-end: 0.75rem;
  font-weight: 700;
  text-align: start;
}

.data-table :is(th, td) {
  padding: 0.75rem 1rem;
  border: 1px solid color-mix(in oklch, CanvasText 24%, transparent);
  text-align: start;
}

.data-table :is(thead, tfoot) {
  background: color-mix(in oklch, Highlight 16%, Canvas);
}

.data-table .highlighted-column {
  background: color-mix(in oklch, Highlight 10%, transparent);
}

.data-table tbody tr:nth-child(even) {
  background: color-mix(in oklch, CanvasText 6%, transparent);
}

Educational guide to table

Keep the context of every cell

A table relates data in two dimensions. Learn to identify it with caption, associate headers and preserve readability when scrolling on a small screen.

Mental model

The title identifies the dataset; headers give each value its meaning.

  1. caption · dataset

    Team hours: identifies what this table compares.

  2. th · coordinates

    Design and Planned hours: the row and column explaining the value.

  3. td · value

    2: two planned hours for Design, rather than an isolated number.

Dataset → headers → valueStatic diagram of the relationships in the first example. Position and color help visually; HTML communicates the associations.

Use table when rows and columns relate data, such as schedules, measurements or comparisons. Choose Grid or Flexbox to arrange cards or lay out a page. A data table keeps its native structure; it does not need role="grid" to look like a spreadsheet.

Simple table: caption and scope

A first row and a first column of headers are often enough.

caption goes at the start of table and identifies its subject. thead, tbody and tfoot group rows; tr contains cells, th names headers and td contains data. Grouping a row in thead does not turn its td cells into headers. tfoot holds a summary rather than a decorative footer.

Planned and logged hours

If the table does not fit, focus this area and use the arrow keys to scroll. Tab continues outside the area.

Team hours
Work Planned hours Logged hours
Design 2 1
Code 3 2
Total 5 3
Each number crosses a row header with a column header. The summary adds both rows: 5 planned hours and 3 logged hours. Every example keeps a named scroll container with keyboard focus.
Planned and logged hours
<p id="table-guide-simple-hint">If the table does not fit, focus this area and use the arrow keys to scroll. Tab continues outside the area.</p>
<div class="table-guide-simple" role="region" tabindex="0"
     aria-labelledby="table-guide-simple-caption" aria-describedby="table-guide-simple-hint">
  <table>
    <caption id="table-guide-simple-caption">Team hours</caption>
    <thead>
      <tr>
        <th scope="col">Work</th>
        <th scope="col">Planned hours</th>
        <th scope="col">Logged hours</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th scope="row">Design</th>
        <td>2</td>
        <td>1</td>
      </tr>
      <tr>
        <th scope="row">Code</th>
        <td>3</td>
        <td>2</td>
      </tr>
    </tbody>
    <tfoot>
      <tr>
        <th scope="row">Total</th>
        <td>5</td>
        <td>3</td>
      </tr>
    </tfoot>
  </table>
</div>
:root {
  color-scheme: light dark;
}

.table-guide-simple {
  max-inline-size: 100%;
  overflow: auto;

  &:focus-visible {
    outline: 3px solid light-dark(oklch(0.46 0.14 261), oklch(0.82 0.18 261));
    outline-offset: 3px;
  }

  table {
    inline-size: 100%;
    min-inline-size: 26rem;
    border-collapse: separate;
    border-spacing: 0;
    background: light-dark(color-mix(in oklch, oklch(0.95 0.008 95) 94%, white), color-mix(in oklch, oklch(0.22 0.024 260) 92%, black));
    color: light-dark(oklch(0.22 0.01 95), oklch(0.95 0.01 95));
    font-size: 1rem;
    line-height: 1.5;
  }

  caption {
    padding-block: 0.75rem;
    font-weight: 800;
    text-align: start;
    overflow-wrap: anywhere;
  }

  th, td {
    padding: 0.75rem;
    border-block-end: 1px solid light-dark(oklch(0.38 0.008 95), oklch(0.78 0.018 260));
    text-align: start;
    vertical-align: top;
    overflow-wrap: anywhere;
  }

  thead th, tfoot th, tfoot td, th[scope="rowgroup"], th[colspan] {
    background: light-dark(color-mix(in oklch, oklch(0.95 0.008 95) 98%, white), color-mix(in oklch, oklch(0.22 0.024 260) 86%, black));
  }

  @media (forced-colors: active) {
    table, th, td {
      background: Canvas;
      color: CanvasText;
    }

    th, td {
      border-color: CanvasText;
    }

    &:focus-visible {
      outline-color: Highlight;
    }
  }
}

scope="col" identifies column headers; scope="row" identifies row headers. Do not replace th with bold td cells. Describe units and abbreviations in the header or nearby text. The caption can remain visible even when another section of the page already has a heading.

Groups and merged cells

Occupying several positions and heading a group are separate responsibilities.

colspan and rowspan define geometry. Here, Seats spans two columns with scope="colgroup" and an actual colgroup span="2". HTML and CSS each head two rows with scope="rowgroup"; each course has its own tbody. Second-level headers keep scope="col" or scope="row".

Seats by course, session and time slot

If the table does not fit, focus this area and use the arrow keys to scroll. Tab continues outside the area.

Available seats by course
Course Session Seats
Morning Afternoon
HTML Basics 12 8
Practice 6 4
CSS Basics 10 6
Practice 4 2
Read the first 12 as HTML → Basics → Seats → Morning. Positions occupied by rowspan are not added again in the next row. Each effective row covers four columns.
Seats by course, session and time slot
<p id="table-guide-groups-hint">If the table does not fit, focus this area and use the arrow keys to scroll. Tab continues outside the area.</p>
<div class="table-guide-groups" role="region" tabindex="0"
     aria-labelledby="table-guide-groups-caption" aria-describedby="table-guide-groups-hint">
  <table>
    <caption id="table-guide-groups-caption">Available seats by course</caption>
    <colgroup span="1"></colgroup>
    <colgroup span="1"></colgroup>
    <colgroup span="2"></colgroup>
    <thead>
      <tr>
        <th rowspan="2" scope="col">Course</th>
        <th rowspan="2" scope="col">Session</th>
        <th colspan="2" scope="colgroup">Seats</th>
      </tr>
      <tr>
        <th scope="col">Morning</th>
        <th scope="col">Afternoon</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th rowspan="2" scope="rowgroup">HTML</th>
        <th scope="row">Basics</th>
        <td>12</td>
        <td>8</td>
      </tr>
      <tr>
        <th scope="row">Practice</th>
        <td>6</td>
        <td>4</td>
      </tr>
    </tbody>
    <tbody>
      <tr>
        <th rowspan="2" scope="rowgroup">CSS</th>
        <th scope="row">Basics</th>
        <td>10</td>
        <td>6</td>
      </tr>
      <tr>
        <th scope="row">Practice</th>
        <td>4</td>
        <td>2</td>
      </tr>
    </tbody>
  </table>
</div>
:root {
  color-scheme: light dark;
}

.table-guide-groups {
  max-inline-size: 100%;
  overflow: auto;

  &:focus-visible {
    outline: 3px solid light-dark(oklch(0.46 0.14 261), oklch(0.82 0.18 261));
    outline-offset: 3px;
  }

  table {
    inline-size: 100%;
    min-inline-size: 26rem;
    border-collapse: separate;
    border-spacing: 0;
    background: light-dark(color-mix(in oklch, oklch(0.95 0.008 95) 94%, white), color-mix(in oklch, oklch(0.22 0.024 260) 92%, black));
    color: light-dark(oklch(0.22 0.01 95), oklch(0.95 0.01 95));
    font-size: 1rem;
    line-height: 1.5;
  }

  caption {
    padding-block: 0.75rem;
    font-weight: 800;
    text-align: start;
    overflow-wrap: anywhere;
  }

  th, td {
    padding: 0.75rem;
    border-block-end: 1px solid light-dark(oklch(0.38 0.008 95), oklch(0.78 0.018 260));
    text-align: start;
    vertical-align: top;
    overflow-wrap: anywhere;
  }

  thead th, tfoot th, tfoot td, th[scope="rowgroup"], th[colspan] {
    background: light-dark(color-mix(in oklch, oklch(0.95 0.008 95) 98%, white), color-mix(in oklch, oklch(0.22 0.024 260) 86%, black));
  }

  @media (forced-colors: active) {
    table, th, td {
      background: Canvas;
      color: CanvasText;
    }

    th, td {
      border-color: CanvasText;
    }

    &:focus-visible {
      outline-color: Highlight;
    }
  }
}

Merging cells does not create accessible associations on its own. rowgroup must match the actual row group, and colgroup the column group. A colgroup allows certain CSS properties on columns but does not replace their th cells. Consider several simple tables if groups make reading harder.

Explicit relationships with id and headers

When relationships become more complex, declare the headers of each cell.

Here an internal row changes the day that the data refers to. Every th has a document-wide unique id, and each td lists the IDs of its three headers in headers, separated by spaces without #. All must belong to the same table. Session headers also declare their context.

Seats by day, session and attendance mode

If the table does not fit, focus this area and use the arrow keys to scroll. Tab continues outside the area.

Available seats by day and attendance mode
Session Online In person
Monday
Basics 12 8
Practice 11 7
Tuesday
Basics 10 6
Practice 9 5
The first 12 explicitly relates to Monday, Basics and Online. IDs are isolated from other examples. When reusing several copies of this recipe, change all IDs and their references.
Seats by day, session and attendance mode
<p id="table-guide-headers-hint">If the table does not fit, focus this area and use the arrow keys to scroll. Tab continues outside the area.</p>
<div class="table-guide-headers" role="region" tabindex="0"
     aria-labelledby="table-guide-headers-caption" aria-describedby="table-guide-headers-hint">
  <table>
    <caption id="table-guide-headers-caption">Available seats by day and attendance mode</caption>
    <thead>
      <tr>
        <th id="table-guide-h-session">Session</th>
        <th id="table-guide-h-online">Online</th>
        <th id="table-guide-h-room">In person</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th id="table-guide-h-monday" colspan="3">Monday</th>
      </tr>
      <tr>
        <th id="table-guide-h-monday-basics"
            headers="table-guide-h-monday table-guide-h-session">Basics</th>
        <td headers="table-guide-h-monday table-guide-h-monday-basics table-guide-h-online">12</td>
        <td headers="table-guide-h-monday table-guide-h-monday-basics table-guide-h-room">8</td>
      </tr>
      <tr>
        <th id="table-guide-h-monday-practice"
            headers="table-guide-h-monday table-guide-h-session">Practice</th>
        <td headers="table-guide-h-monday table-guide-h-monday-practice table-guide-h-online">11</td>
        <td headers="table-guide-h-monday table-guide-h-monday-practice table-guide-h-room">7</td>
      </tr>
    </tbody>
    <tbody>
      <tr>
        <th id="table-guide-h-tuesday" colspan="3">Tuesday</th>
      </tr>
      <tr>
        <th id="table-guide-h-tuesday-basics"
            headers="table-guide-h-tuesday table-guide-h-session">Basics</th>
        <td headers="table-guide-h-tuesday table-guide-h-tuesday-basics table-guide-h-online">10</td>
        <td headers="table-guide-h-tuesday table-guide-h-tuesday-basics table-guide-h-room">6</td>
      </tr>
      <tr>
        <th id="table-guide-h-tuesday-practice"
            headers="table-guide-h-tuesday table-guide-h-session">Practice</th>
        <td headers="table-guide-h-tuesday table-guide-h-tuesday-practice table-guide-h-online">9</td>
        <td headers="table-guide-h-tuesday table-guide-h-tuesday-practice table-guide-h-room">5</td>
      </tr>
    </tbody>
  </table>
</div>
:root {
  color-scheme: light dark;
}

.table-guide-headers {
  max-inline-size: 100%;
  overflow: auto;

  &:focus-visible {
    outline: 3px solid light-dark(oklch(0.46 0.14 261), oklch(0.82 0.18 261));
    outline-offset: 3px;
  }

  table {
    inline-size: 100%;
    min-inline-size: 26rem;
    border-collapse: separate;
    border-spacing: 0;
    background: light-dark(color-mix(in oklch, oklch(0.95 0.008 95) 94%, white), color-mix(in oklch, oklch(0.22 0.024 260) 92%, black));
    color: light-dark(oklch(0.22 0.01 95), oklch(0.95 0.01 95));
    font-size: 1rem;
    line-height: 1.5;
  }

  caption {
    padding-block: 0.75rem;
    font-weight: 800;
    text-align: start;
    overflow-wrap: anywhere;
  }

  th, td {
    padding: 0.75rem;
    border-block-end: 1px solid light-dark(oklch(0.38 0.008 95), oklch(0.78 0.018 260));
    text-align: start;
    vertical-align: top;
    overflow-wrap: anywhere;
  }

  thead th, tfoot th, tfoot td, th[scope="rowgroup"], th[colspan] {
    background: light-dark(color-mix(in oklch, oklch(0.95 0.008 95) 98%, white), color-mix(in oklch, oklch(0.22 0.024 260) 86%, black));
  }

  @media (forced-colors: active) {
    table, th, td {
      background: Canvas;
      color: CanvasText;
    }

    th, td {
      border-color: CanvasText;
    }

    &:focus-visible {
      outline-color: Highlight;
    }
  }
}

headers describes relationships, not a guaranteed announcement order. Test table navigation with your audience’s screen readers and browsers. Before maintaining a complex matrix, consider a separate table per day with clear repeated headers; this is often easier to read and maintain.

Responsive presentation and scrolling

A table may need two dimensions; the rest of the page should reflow.

The container uses overflow: auto, and the table keeps a readable minimum width. tabindex="0" allows focusing the area in browsers that do not make scroll containers focusable automatically. role="region", aria-labelledby and aria-describedby identify the area and its help. An extra keyboard stop is justified here to demonstrate scrolling.

Local scrolling with sticky headers

If the table does not fit, focus this area and use the arrow keys to scroll. Tab continues outside the area.

Extended team hours log
Work Planned hours Logged hours
Design 2 1
Code 3 2
Design 2 1 1
Code 3 1 1
Design 4 1 1
Code 5 1 1
Design 6 1 1
Code 7 1 1
Total 11 9
On a narrow screen, focus the area and use the arrow keys to scroll. It also scrolls vertically: column headers stay at the top edge. Tab leaves the area; scrolling does not depend on dragging.
Local scrolling with sticky headers
<p id="table-guide-responsive-hint">If the table does not fit, focus this area and use the arrow keys to scroll. Tab continues outside the area.</p>
<div class="table-guide-responsive" role="region" tabindex="0"
     aria-labelledby="table-guide-responsive-caption" aria-describedby="table-guide-responsive-hint">
  <table>
    <caption id="table-guide-responsive-caption">Extended team hours log</caption>
    <thead>
      <tr>
        <th scope="col">Work</th>
        <th scope="col">Planned hours</th>
        <th scope="col">Logged hours</th>
      </tr>
    </thead>
    <tbody>
      <tr>
        <th scope="row">Design</th>
        <td>2</td>
        <td>1</td>
      </tr>
      <tr>
        <th scope="row">Code</th>
        <td>3</td>
        <td>2</td>
      </tr>
      <tr>
        <th scope="row">Design 2</th>
        <td>1</td>
        <td>1</td>
      </tr>
      <tr>
        <th scope="row">Code 3</th>
        <td>1</td>
        <td>1</td>
      </tr>
      <tr>
        <th scope="row">Design 4</th>
        <td>1</td>
        <td>1</td>
      </tr>
      <tr>
        <th scope="row">Code 5</th>
        <td>1</td>
        <td>1</td>
      </tr>
      <tr>
        <th scope="row">Design 6</th>
        <td>1</td>
        <td>1</td>
      </tr>
      <tr>
        <th scope="row">Code 7</th>
        <td>1</td>
        <td>1</td>
      </tr>
    </tbody>
    <tfoot>
      <tr>
        <th scope="row">Total</th>
        <td>11</td>
        <td>9</td>
      </tr>
    </tfoot>
  </table>
</div>
:root {
  color-scheme: light dark;
}

.table-guide-responsive {
  max-inline-size: 100%;
  overflow: auto;
  max-block-size: 18rem;

  &:focus-visible {
    outline: 3px solid light-dark(oklch(0.46 0.14 261), oklch(0.82 0.18 261));
    outline-offset: 3px;
  }

  table {
    inline-size: 100%;
    min-inline-size: 38rem;
    border-collapse: separate;
    border-spacing: 0;
    background: light-dark(color-mix(in oklch, oklch(0.95 0.008 95) 94%, white), color-mix(in oklch, oklch(0.22 0.024 260) 92%, black));
    color: light-dark(oklch(0.22 0.01 95), oklch(0.95 0.01 95));
    font-size: 1rem;
    line-height: 1.5;
  }

  caption {
    padding-block: 0.75rem;
    font-weight: 800;
    text-align: start;
    overflow-wrap: anywhere;
  }

  th, td {
    padding: 0.75rem;
    border-block-end: 1px solid light-dark(oklch(0.38 0.008 95), oklch(0.78 0.018 260));
    text-align: start;
    vertical-align: top;
    overflow-wrap: anywhere;
  }

  thead th, tfoot th, tfoot td, th[scope="rowgroup"], th[colspan] {
    background: light-dark(color-mix(in oklch, oklch(0.95 0.008 95) 98%, white), color-mix(in oklch, oklch(0.22 0.024 260) 86%, black));
  }

  thead th {
    position: sticky;
    inset-block-start: 0;
    z-index: 1;
    background: light-dark(color-mix(in oklch, oklch(0.95 0.008 95) 98%, white), color-mix(in oklch, oklch(0.22 0.024 260) 86%, black));
  }

  @media (forced-colors: active) {
    table, th, td {
      background: Canvas;
      color: CanvasText;
    }

    th, td {
      border-color: CanvasText;
    }

    &:focus-visible {
      outline-color: Highlight;
    }
  }
}

sticky refers to the nearest scroll container. inset-block-start enables the constraint, an opaque background prevents text overlap, and z-index keeps headers above data. border-collapse: separate with border-spacing: 0 preserves per-cell borders. A maximum height limits only this long example.

Keep caption and headers on mobile too. Do not hide thead and replace it solely with CSS-generated labels, or shrink the font until everything fits. A card view may require another data presentation and accessibility testing. table-layout: fixed with a defined width changes column sizing; it does not fix associations or prevent content overflow on its own.

Common pitfalls

Check structure, relationships and access to content before polishing borders.

The header is only distinguished by appearance

Expected result
Each value keeps recognizable headers without depending on color or weight.
Cause
Bold td cells are used, or thead contains no th cells.
Solution
Use th and scope for simple relationships; caption identifies the dataset.

Merged cells shift the data

Expected result
All rows cover the same grid and groups have actual boundaries.
Cause
Positions occupied by rowspan are counted again, or span is confused with scope.
Solution
Count effective positions and align tbody/colgroup with rowgroup/colgroup; simplify when needed.

headers points to another table or cannot find its header

Expected result
Each reference resolves to a th in this table.
Cause
Duplicated IDs after copying, a # in headers or outdated references.
Solution
Assign unique IDs and check every headers token after changing rows or columns.

Columns disappear on mobile or scrolling is unavailable

Expected result
All content is reachable without widening the page or losing focus.
Cause
overflow: hidden clips data, keyboard access is missing or headers have been removed.
Solution
Keep the native table in a named local scroll area with visible focus and help; test keyboard and zoom.

Support and compatibility

HTML availability does not certify how a particular table is announced.

HTML tables are Baseline Widely Available. Caption, headers and sections provide the foundation; accessible results also depend on correct relationships and the browser/assistive technology combination. Examples work without JavaScript; sticky is a visual aid, and scrolling remains available if it does not apply.

Check cell navigation, announced headers, enlarged text, contrast, focus and scrolling in real environments. Avoid roles that replace native semantics and do not use obsolete summary, border, cellpadding or cellspacing attributes as a modern recipe: use visible text and CSS.