Lesson 3 of 8
Accessible & Styled Tables
Make tables accessible to all users and style them professionally with CSS. Learn ARIA attributes for complex tables, responsive table patterns for mobile, zebra striping, sortable columns, and sticky headers.
Table Accessibility Requirements
- Always include <caption> - provides a title that screen readers announce before the table data
- Use <th> with scope for all header cells - associates headers with data for assistive technology
- For complex tables, use headers attribute on <td> - explicitly links data cells to multiple headers by ID
- Add aria-describedby for supplementary descriptions - explains the data format or units
- Avoid using color alone to convey meaning - pair with text indicators (checkmarks, icons)
- Test with a screen reader (NVDA, VoiceOver) - navigate with Ctrl+Alt+Arrow keys to verify associations
Tip
Always add <caption> as the first child of <table>. It acts as a title that screen readers announce before any data, giving users context about what the table contains. Think of it like a heading for your table. Example: <caption>Q3 2025 Sales by Region</caption>.
Use scope attributes and captions so screen readers navigate tabular data clearly
Responsive Table CSS
<style>
/* Responsive table wrapper */
.table-wrapper { overflow-x: auto; -webkit-overflow-scrolling: touch; }
/* Zebra striping */
tbody tr:nth-child(even) { background: #f8f9fa; }
/* Hover effect */
tbody tr:hover { background: #e8f0fe; }
/* Sticky header */
thead th { position: sticky; top: 0; background: #333; color: white; z-index: 1; }
/* Mobile: stack columns vertically */
@media (max-width: 600px) {
table, thead, tbody, th, td, tr { display: block; }
thead { display: none; }
td {
padding-left: 50%;
position: relative;
}
td::before {
content: attr(data-label);
position: absolute;
left: 12px;
font-weight: bold;
}
}
</style>
<!-- Usage: add data-label for mobile -->
<div class="table-wrapper">
<table>
<thead>
<tr><th>Name</th><th>Role</th><th>Salary</th></tr>
</thead>
<tbody>
<tr>
<td data-label="Name">Alice Johnson</td>
<td data-label="Role">Engineer</td>
<td data-label="Salary">$95,000</td>
</tr>
</tbody>
</table>
</div>Quick Quiz: Accessible Tables
Key Takeaways
- Make tables accessible to all users and style them professionally with CSS.
- Always include <caption> - provides a title that screen readers announce before the table data
- Use <th> with scope for all header cells - associates headers with data for assistive technology
- For complex tables, use headers attribute on <td> - explicitly links data cells to multiple headers by ID
Finished this lesson?