📖 Scenario: You are creating a simple data table for a website that shows a list of fruits and their colors. The table must be easy to understand for everyone, including people who use screen readers.
🎯 Goal: Build an accessible HTML table with proper headers and ARIA attributes so that screen readers can read the table correctly.
📋 What You'll Learn
Create a table with 3 rows of fruit data and 2 columns: Fruit and Color
Use <table>, <thead>, <tbody>, <tr>, <th>, and <td> elements correctly
Add scope="col" to header cells to define column headers
Add a caption describing the table content
Ensure the table is keyboard accessible and screen reader friendly
💡 Why This Matters
🌍 Real World
Accessible tables are important on websites that show data, like product lists, schedules, or reports. They help everyone, including people using screen readers, understand the information clearly.
💼 Career
Web developers must create accessible content to meet legal standards and provide a good user experience for all visitors.
Progress0 / 4 steps
1
Create the basic table structure with fruit data
Create a table element with a thead containing one row with two header cells: Fruit and Color. Then add a tbody with three rows containing these exact fruit-color pairs: Apple - Red, Banana - Yellow, and Grape - Purple.
HTML
Hint
Use <thead> for the header row and <tbody> for the data rows. Each row uses <tr>. Headers use <th> and data cells use <td>.
2
Add scope="col" to header cells
Add the attribute scope="col" to both <th> elements inside the thead to specify that these headers apply to columns.
HTML
Hint
The scope="col" attribute tells screen readers that these headers describe columns.
3
Add a caption describing the table
Add a caption element inside the table as the first child. The caption text should be exactly Fruit colors list.
HTML
Hint
The caption element gives a short description of the table for screen readers and users.
4
Add keyboard accessibility and finalize
Ensure the table uses semantic HTML as before. Add the attribute tabindex="0" to the table element so keyboard users can focus on the table easily.
HTML
Hint
Adding tabindex="0" makes the table focusable by keyboard users, improving accessibility.
Practice
(1/5)
1. What is the main purpose of the <caption> element in an HTML table?
easy
A. To add a footer row at the bottom of the table.
B. To provide a title or description for the table, improving accessibility.
C. To create a header cell for each column.
D. To style the table with colors and fonts.
Solution
Step 1: Understand the role of <caption>
The <caption> element is used to give a title or description to a table, which helps screen readers and users understand the table's content.
Step 2: Compare with other options
Options B, C, and D describe other table parts or styling, but only <caption> provides a descriptive title.
Final Answer:
To provide a title or description for the table, improving accessibility. -> Option B
Quick Check:
<caption> = Table title [OK]
Hint: Caption tags give tables a clear title for screen readers [OK]
Common Mistakes:
Confusing <caption> with <tfoot> or <thead>
Thinking <caption> styles the table
Ignoring accessibility benefits of <caption>
2. Which of the following is the correct way to mark a header cell for a column in an HTML table?
easy
A. <td scope="col">Header</td>
B. <td>Header</td>
C. <th scope="row">Header</th>
D. <th scope="col">Header</th>
Solution
Step 1: Identify the correct element for header cells
The <th> element is used for header cells, not <td>.
Step 2: Understand the scope attribute
For column headers, scope="col" is correct. <th scope="col">Header</th> uses <th scope="col">, which is the proper syntax.
Final Answer:
<th scope="col">Header</th> -> Option D
Quick Check:
<th scope="col"> = Column header [OK]
Hint: Use <th> with scope="col" for column headers [OK]
Common Mistakes:
Using <td> instead of <th> for headers
Mixing scope="row" with column headers
Omitting the scope attribute
3. Given the following table snippet, what will screen readers associate the header "Name" with?