Product Comparison Tables #
Description:
Product comparison tables are an effective way to present detailed information side-by-side for users to evaluate multiple products quickly. To ensure these tables are accessible, it’s important to use semantic HTML, ARIA attributes, and a user-friendly design that supports screen readers, keyboard navigation, and other assistive technology
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Accessible Product Comparison 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;
}
th[scope="row"] {
background-color: #e9e9e9;
text-align: left;
}
caption {
font-size: 1.2em;
margin-bottom: 10px;
text-align: left;
font-weight: bold;
}
/* Responsive table */
@media (max-width: 600px) {
table {
display: block;
overflow-x: auto;
white-space: nowrap;
}
}
</style>
</head>
<body>
<!-- Accessible Product Comparison Table -->
<table aria-label="Comparison of backpacks">
<caption>Comparison of Eco-Friendly Backpacks</caption>
<thead>
<tr>
<th scope="col">Feature</th>
<th scope="col">Backpack A</th>
<th scope="col">Backpack B</th>
<th scope="col">Backpack C</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Material</th>
<td>Sustainable Leather</td>
<td>Recycled Plastic</td>
<td>Organic Cotton</td>
</tr>
<tr>
<th scope="row">Weight</th>
<td>1.2 kg</td>
<td>1.1 kg</td>
<td>0.8 kg</td>
</tr>
<tr>
<th scope="row">Capacity</th>
<td>20 Liters</td>
<td>25 Liters</td>
<td>15 Liters</td>
</tr>
<tr>
<th scope="row">Price</th>
<td>$120</td>
<td>$80</td>
<td>$90</td>
</tr>
</tbody>
</table>
</body>
</html>
Explanation #
- Semantic HTML for Structure:
- The
<table>element creates a structured layout for presenting data. <thead>and<tbody>separate the header and body sections, making navigation clearer for screen readers.
- The
- Descriptive Headers:
<th scope="col">defines column headers, like “Feature” and “Backpack A,” making the column’s purpose clear to screen readers.<th scope="row">labels each row with features like “Material” and “Weight,” so screen readers can announce the row context.
- Responsive Design:
- The table is wrapped in a responsive design to ensure it’s scrollable on smaller screens, preventing content from breaking.
- Caption for Context:
- The
<caption>element provides an overview of the table’s purpose. Screen readers announce this first, giving users context about what the table contains.
- The
- Keyboard Navigation:
- By default, semantic tables are navigable via keyboard, allowing users to move through cells and headers with
Tabor arrow keys.
- By default, semantic tables are navigable via keyboard, allowing users to move through cells and headers with
Key Takeaways #
- Semantic HTML: Use correct table elements (
<table>,<thead>,<tbody>,<th>,<td>) to create a clear structure for screen readers and users. - Descriptive Labels: Provide clear, contextual labels for rows and columns using
scopeattributes. - Keyboard Accessibility: Ensure the table is fully keyboard navigable.
- Responsive Design: Make the table scrollable or stack rows for mobile users.
- Enhance Usability: Offer filters or summaries for complex tables to improve user experience.