Sub topic 9
Building the table with semantic HTML
This chapter assumes you know basic HTML and CSS, and that you have already chosen your tiers and features. The goal here is a comparison table that browsers, search engines, and screen readers can all read as a table. It builds on the table anatomy from the previous chapters. Responsive behavior and the full accessibility review come in the next two chapters, so this one touches them only where the markup requires it.
Why use a real table
A plan comparison is tabular data. Each cell has meaning only in relation to a plan (a column) and a feature (a row). A <table> states those relationships in the markup itself.
Many pricing pages are built from nested <div> elements with CSS grid instead. That works visually, but it loses the structure. A screen reader user hears a flat list of text and cannot tell which plan a "3" or an "Unlimited" belongs to. A real table lets assistive technology announce the plan and the feature for every cell.
Use a table when you compare three or more plans across many features. Use cards, with no table, for the top of the page where each plan has a short pitch and a button. Many pricing pages use both: cards for the summary, a table for the detail.
The building blocks
These elements carry the structure:
| Element | Purpose |
|---|---|
| `<table>` | The container for the whole comparison |
| `<caption>` | A visible or hidden title that names the table |
| `<thead>` | The header row group, holding the plan names |
| `<tbody>` | The body row group, holding the features |
| `<tr>` | One row |
| `<th scope="col">` | A header cell that labels a column (a plan) |
| `<th scope="row">` | A header cell that labels a row (a feature) |
| `<td>` | A data cell for one feature in one plan |
The scope attribute is the key. It tells the browser whether a header applies to the cells below it (col) or to the cells beside it (row). With both set, every <td> has two labels: its plan and its feature.
A complete example
This table has four columns: an empty corner cell, then three plans. The top-left cell is a header too, so it needs content. A visually hidden label gives it one without adding visible clutter.
<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>A screen reader reading the Business cell in the "Single sign-on" row announces something like "Business, Single sign-on, Included". The visible check mark is hidden from assistive technology with aria-hidden="true", and the hidden words take its place.
Symbols need text
A check mark or a cross is a picture of a meaning. Sighted users read it at a glance. A screen reader may skip it, read it as "multiplication x", or say nothing. Color alone is worse, because it fails for color-blind users and is invisible to screen readers.
The pattern in the example has two parts:
- A visible symbol marked
aria-hidden="true", so it is not announced. - A visually hidden text node that says "Included" or "Not included".
A plain dash for "not included" is also ambiguous, since it may look like missing data. Spell out the state in the hidden text.
The visually hidden class
You cannot use display: none or visibility: hidden for the hidden text. Both remove the content from the accessibility tree, so screen readers never see it. You need a class that hides the text visually but keeps it available:
.visually-hidden {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
overflow: hidden;
clip-path: inset(50%);
white-space: nowrap;
border: 0;
}Each property has a job. Absolute positioning takes the element out of the layout. The 1px size and overflow: hidden shrink it to nothing visible. The negative margin stops it from adding space. white-space: nowrap prevents the text from wrapping into a tall, narrow column that a screen reader might read oddly.
Group rows with feature categories
A long table is hard to scan. Group related features under a heading, such as "Collaboration", "Security", and "Support". In HTML you have two sound options.
The first is one <tbody> per group, with a header row that spans all columns:
<tbody>
<tr>
<th scope="colgroup" colspan="4">Security</th>
</tr>
<tr>
<th scope="row">Single sign-on</th>
<td>Not included</td>
<td>Not included</td>
<td>Included</td>
</tr>
<tr>
<th scope="row">Audit log</th>
<td>Not included</td>
<td>90 days</td>
<td>Unlimited</td>
</tr>
</tbody>Here scope="colgroup" tells assistive technology that the heading covers the group of columns it spans. Using several <tbody> elements, one per category, also gives you a hook for styling and keeps each group together in the source.
The second option is to split the comparison into several small tables, each with its own caption. This is simpler for screen reader users, who can jump between tables. It costs you column alignment unless you fix the column widths in CSS.
Calls to action inside the table
Plans usually need a button in the table, often in a footer row or in the header. Put the button where the user finishes reading a column. A common choice is a final row:
<tfoot>
<tr>
<th scope="row"><span class="visually-hidden">Choose a plan</span></th>
<td><a class="button" href="/signup?plan=starter">Start free<span class="visually-hidden"> with Starter</span></a></td>
<td><a class="button" href="/signup?plan=team">Start trial<span class="visually-hidden"> with Team</span></a></td>
<td><a class="button" href="/contact-sales">Contact sales<span class="visually-hidden"> about Business</span></a></td>
</tr>
</tfoot>Three identical links that all say "Start trial" are indistinguishable when a screen reader lists the links on a page. The hidden suffix gives each link a unique, meaningful name. Use real links for navigation to a sign-up page and real buttons for actions that change the page.
Prices in the header
Prices belong near the plan names, and the data should come from one source. The next code block keeps plan data in a typed structure and computes both the displayed price and the annual discount from it. The toggle and the stated saving then cannot disagree.
type BillingPeriod = "monthly" | "annual";
interface Plan {
id: string;
name: string;
monthlyCents: number;
annualPerMonthCents: number;
}
const formatter = new Intl.NumberFormat("en-US", {
style: "currency",
currency: "USD",
});
function displayPrice(plan: Plan, period: BillingPeriod): string {
const cents =
period === "annual" ? plan.annualPerMonthCents : plan.monthlyCents;
return formatter.format(cents / 100);
}
function annualSavingsPercent(plan: Plan): number {
return Math.round((1 - plan.annualPerMonthCents / plan.monthlyCents) * 100);
}
const team: Plan = {
id: "team",
name: "Team",
monthlyCents: 1500,
annualPerMonthCents: 1200,
};
console.log(displayPrice(team, "annual")); // "$12.00"
console.log(annualSavingsPercent(team)); // 20Storing money as integer cents avoids floating-point rounding errors. Formatting with Intl.NumberFormat handles currency symbols and separators for the visitor's locale if you pass a different locale and currency.
To switch the prices on the page, a button with aria-pressed works well. It tells assistive technology whether the annual option is on:
const toggle = document.querySelector("#billing-toggle");
toggle.addEventListener("click", () => {
const annual = toggle.getAttribute("aria-pressed") !== "true";
toggle.setAttribute("aria-pressed", String(annual));
document.querySelectorAll("[data-monthly][data-annual]").forEach((el) => {
el.textContent = annual ? el.dataset.annual : el.dataset.monthly;
});
});Each price element carries both strings, for example <span data-monthly="$15.00" data-annual="$12.00">$15.00</span>. The page renders the monthly text first, so it still works if the script fails to load.
Common errors
- Using
<td>for header cells. Plan names and feature names styled as bold<td>cells are not headers to assistive technology. Use<th>withscope. - An empty corner cell. A
<th>with no content is announced as blank. Add hidden text such as "Feature". - Layout tables. Do not use a table to arrange cards or columns of marketing copy. Reserve it for data.
- Merged cells everywhere.
colspanandrowspanmake the structure harder for screen readers. Keep them to group headings. - Hiding text with
display: none. The text disappears for everyone. Use the visually hidden class. - Symbols without text. Check marks, crosses, and dashes need a text equivalent.
- Missing caption. Without a
<caption>, the table has no name in the list of tables a screen reader offers.
Structure at a glance
Checking your work
You can test the structure without a screen reader:
- Remove the stylesheet. The table should still read in a sensible order.
- Open the browser's accessibility inspector and check that each
<th>has a role of column header or row header. - Tab through the page. Every link in the table should be reachable and have a distinct name.
- If you can, try a screen reader and move cell by cell. Each cell should announce its plan and its feature.
Exercise
A teammate builds the plan table with <td> cells for the plan names and feature names, and uses a green check image with no alt text for "included". Name two changes that make this table readable by a screen reader, and say which HTML elements or attributes you would use.
Show answer
First, replace the <td> cells that act as labels with <th> elements: <th scope="col"> for plan names in the header row and <th scope="row"> for feature names. Second, replace the unlabeled image with a visible symbol marked aria-hidden="true" plus a visually hidden text such as "Included" (and "Not included" for the cross). Also add a <caption> that names the table.
Exercise
Why is display: none the wrong way to hide the "Included" text, and what do you use instead?
Show answer
display: none removes the element from the accessibility tree, so screen readers do not announce it. Use a visually hidden class that shrinks and clips the element (absolute position, 1px size, overflow: hidden, clip-path: inset(50%), white-space: nowrap) so it is invisible on screen but still read aloud.