Simple data tables to show tabular information and table navigation #
Description:
Simple data tables are used to display tabular information with basic headers and rows. For accessibility, it’s important to use semantic HTML, provide clear headers, and ensure the table is navigable via keyboard and screen readers.
Key Features of Accessible Simple Data Tables #
- Semantic Structure: Use proper
<table>,<thead>,<tbody>,<th>, and<td>tags for clear organization. - Descriptive Headers: Use
<th>for headers and associate them with rows or columns using thescopeattribute. - Keyboard Accessibility: Ensure users can navigate the table using keyboard-only controls.
- Screen Reader Context: Use
caption,aria-label, oraria-describedbyto provide additional context for screen readers. - Responsive Design: Ensure tables remain usable on smaller screens by enabling horizontal scrolling or stacking rows.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Accessible Data Table</title>
<style>
/* Basic table styling */
table {
width: 100%;
border-collapse: collapse;
margin: 20px 0;
}
th, td {
border: 1px solid #ddd;
padding: 10px;
text-align: left;
}
th {
background-color: #f4f4f4;
}
caption {
font-size: 1.2em;
margin-bottom: 10px;
text-align: left;
font-weight: bold;
}
/* Responsive design */
@media (max-width: 600px) {
table {
display: block;
overflow-x: auto;
white-space: nowrap;
}
}
</style>
</head>
<body>
<!-- Simple Data Table -->
<table aria-label="Monthly Sales Data">
<caption>Monthly Sales Data</caption>
<thead>
<tr>
<th scope="col">Month</th>
<th scope="col">Product A</th>
<th scope="col">Product B</th>
<th scope="col">Product C</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">January</th>
<td>150</td>
<td>200</td>
<td>250</td>
</tr>
<tr>
<th scope="row">February</th>
<td>180</td>
<td>220</td>
<td>300</td>
</tr>
<tr>
<th scope="row">March</th>
<td>200</td>
<td>240</td>
<td>350</td>
</tr>
</tbody>
</table>
</body>
</html>
Explanation #
- Semantic Structure:
- The table uses
<thead>for the header row and<tbody>for the data rows, ensuring logical structure and easy navigation for screen readers. <th>elements define both column headers (scope="col") and row headers (scope="row").
- The table uses
- Descriptive Caption:
- The
<caption>provides context for the table, making it clear to all users what the data represents. Screen readers announce this first.
- The
- Responsive Design:
- A media query ensures the table is scrollable on smaller screens, maintaining usability without breaking the layout.
- Keyboard Navigation:
- Users can navigate the table cell by cell using the
Tabkey, and screen readers will announce headers and corresponding data.
- Users can navigate the table cell by cell using the
- ARIA for Additional Context:
- The
aria-label="Monthly Sales Data"attribute provides a brief description of the table’s purpose for screen readers.
- The
Key Takeaways #
- Structure Matters: Use semantic HTML to create a logical and accessible table structure.
- Provide Context: Use captions and ARIA attributes to clarify the table’s purpose and content.
- Ensure Keyboard Usability: Make sure users can navigate the table and any interactive elements using only the keyboard.
- Responsive Design: Make the table usable on all devices, including mobile, by enabling scrolling or stacking.
Screen reader: NVDA
Video: