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.
- caption · dataset
Team hours: identifies what this table compares.
- th · coordinates
Design and Planned hours: the row and column explaining the value.
- td · value
2: two planned hours for Design, rather than an isolated number.
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.
| Work | Planned hours | Logged hours |
|---|---|---|
| Design | 2 | 1 |
| Code | 3 | 2 |
| Total | 5 | 3 |
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.
| Course | Session | Seats | |
|---|---|---|---|
| Morning | Afternoon | ||
| HTML | Basics | 12 | 8 |
| Practice | 6 | 4 | |
| CSS | Basics | 10 | 6 |
| Practice | 4 | 2 | |
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.
| Session | Online | In person |
|---|---|---|
| Monday | ||
| Basics | 12 | 8 |
| Practice | 11 | 7 |
| Tuesday | ||
| Basics | 10 | 6 |
| Practice | 9 | 5 |
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.
| 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 |
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.