# Tables and Data Specialist reference: Simple Data Tables Part of the `tables-data-specialist` skill. Read this only when the task reaches these sections. ## Simple Data Tables ### The Basics Every data table needs these elements: ```html
Quarterly sales by region, 2025
Region Q1 Q2 Q3 Q4
North America $2.1M $2.4M $2.8M $3.1M
Europe $1.8M $1.9M $2.2M $2.5M
``` Requirements: - `` describes what the table contains -- this is the table's accessible name. It MUST be the first child element after `` - `` wraps the header row(s), `` wraps data rows - `` for summary/total rows if they exist ### Structural Clarifications (per WebAIM) **``, ``, ``** provide no accessibility semantics -- screen readers do not use them. They exist for CSS styling, print rendering, and fixed-header scrolling. Still use them for code organization, but do not rely on them for accessibility. **The `summary` attribute** is obsolete in HTML. Use ` ``` ## Complex Tables ### Multi-Level Headers When a table has headers that span multiple columns or rows: ```html
` for all header cells, never `` styled to look like a header - `scope="col"` on column headers, `scope="row"` on row headers -- always explicit, even when there is only one header row - `
` for the table's accessible name. If a longer description is needed, use `aria-describedby` pointing to a paragraph outside the table. **`headers`/`id` associations** are a last resort. Prefer `scope` on every `
`. Only use `headers`/`id` when a table has irregular header spans that `scope` cannot express. Over-complex `headers`/`id` markup is fragile and error-prone. **Proportional sizing:** Use percentage or relative widths (`width: 30%`, `min-width: 8em`) rather than fixed pixel widths. This prevents horizontal scrolling at increased text sizes (WCAG 1.4.10 Reflow). **Flatten when possible:** If a table requires deeply nested `colspan`/`rowspan` spanning three or more levels, consider whether the data can be restructured into simpler tables. Complex spanning creates substantial screen reader navigation difficulty. ### Why `scope` Matters Without `scope`, screen readers have to guess which headers apply to which cells. In simple tables they often guess correctly, but in complex tables they will guess wrong. Always be explicit. ```html Region Region
Employee schedule, week of January 20
Morning Afternoon
Task Location Task Location
Monday Code review Remote Sprint planning Room 4A
``` ### The `headers` Attribute For truly complex tables where `scope` is insufficient (cells relate to headers in non-obvious ways), use the `headers` attribute: ```html
Test results by browser and operating system
Chrome Firefox Safari
Windows Pass Pass N/A
macOS Pass Pass Fail
``` ## Sortable Tables ```html
User accounts
``` Requirements: - Sort buttons inside `` elements - `aria-sort` on the ``: `"ascending"`, `"descending"`, or `"none"` - Only one column can have `aria-sort="ascending"` or `"descending"` at a time - Update `aria-sort` when the user clicks to sort - Visual sort indicator (arrow/chevron) with `aria-hidden="true"` - the `aria-sort` attribute is the accessible indicator - Announce the sort change via a live region or by the `aria-sort` update ```javascript function sortColumn(th, direction) { // Reset all columns document.querySelectorAll('th[aria-sort]').forEach(h => { h.setAttribute('aria-sort', 'none'); }); // Set the active column th.setAttribute('aria-sort', direction); // Sort the data... } ``` ## Interactive Data Grids When a table has interactive content (editable cells, inline actions, checkboxes), use the ARIA grid pattern: ```html
Name Role Actions
Jane Smith
``` Requirements for `role="grid"`: - Arrow keys navigate between cells - Only one focusable element in the grid is in the page tab sequence (roving `tabindex`), so the grid is a single Tab stop and arrow keys move between cells, per the APG grid pattern () - Enter/Space activates the focused cell's interactive element - Every interactive element inside cells needs a descriptive `aria-label` that includes context (not just "Edit" - "Edit Jane Smith") - `role="grid"` goes on the ``, not individual cells - Only use `role="grid"` when cells are interactive - plain data tables should NOT have `role="grid"`