Sub topic 9
Building the table with semantic HTML
This chapter assumes you know basic HTML and CSS and have already chosen the tiers and features to compare. Here you turn that plan into a table that browsers and screen readers can interpret correctly.
Why a table, not a grid of divs
A plan comparison is tabular data. Each cell has meaning only in relation to a plan (a column) and a feature (a row). A real <table> exposes those relationships to assistive technology. A screen reader user can move to a cell and hear "Business, Single sign-on, Included". A grid of div elements gives them none of that.
Build the table first with plain, correct markup. Add layout styling afterward.
The core elements
<caption>names the table. Screen readers announce it when a user enters the table.<thead>holds the header row. Here it lists the plans.<tbody>holds the feature rows.<th scope="col">marks a cell as a header for its column.<th scope="row">marks a cell as a header for its row.<td>holds a data value.
The scope attribute removes guesswork. Without it, a browser infers header relationships, and the inference can fail in complex tables.
A complete example
<div class="table-wrap">
<table class="plans">
<caption>Compare plans</caption>
<thead>
<tr>
<th scope="col"><span class="visually-hidden">Feature</span></th>
<th scope="col">Starter</th>
<th scope="col">Team</th>
<th scope="col">Business</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Projects</th>
<td>3</td>
<td>Unlimited</td>
<td>Unlimited</td>
</tr>
<tr>
<th scope="row">Single sign-on</th>
<td><span aria-hidden="true">✕</span><span class="visually-hidden">Not included</span></td>
<td><span aria-hidden="true">✕</span><span class="visually-hidden">Not included</span></td>
<td><span aria-hidden="true">✓</span><span class="visually-hidden">Included</span></td>
</tr>
</tbody>
</table>
</div>Three details matter here.
- The empty top-left cell still needs content. It is a header cell with no visible label. A visually hidden "Feature" gives it a name without changing the layout.
- Symbols need text alternatives. A check or cross glyph carries no reliable meaning for a screen reader. The
aria-hidden="true"span hides the symbol, and the hidden text states the value in words. - The wrapper div is for scrolling. It lets the table scroll sideways on narrow screens, which the responsive chapter covers.
The hiding class
The visually-hidden class removes text from view but keeps it in the accessibility tree.
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}Common errors
| Mistake | Effect | Fix |
|---|---|---|
| Feature names in `<td>` instead of `<th scope="row">` | Cells lose their row label | Use `<th scope="row">` |
| Check marks with no text alternative | Screen readers read nothing, or read the glyph name | Pair `aria-hidden` symbols with hidden text |
| Missing `<caption>` | Table has no accessible name | Add a caption |
| A `<table>` used only for page layout | Wrong semantics | Use CSS grid or flexbox |
Exercise
A teammate marks the feature names in the first column as <td> elements and uses a check mark character with no other text. Name two problems this causes and how to fix each.
Show answer
First, the feature names are not header cells, so a screen reader cannot announce the row label with each value. Change them to <th scope="row">. Second, the check mark has no reliable text alternative, so a screen reader user cannot tell whether a feature is included. Wrap the symbol in a span with aria-hidden="true" and add visually hidden text such as "Included" or "Not included".