Nothing is more annoying than seeing a web page laid-out in a table. (Trust me, it causes more problems that it solves.) The question is: how do you make a table that is accessible. Let’s learn how. And I’ll give you code to copy for later.
The sample table we are going to work on is a Weekly Menu. The table will have two headers.
The first thing you will need to do is create code for the user to know what the table is about at first glance. This isn’t required by Section 508, but is generally a best practice. You’ll be adding a summary attribute to the <table> tag as well as a separate <caption> tag after the opening <table> tag.
<table style=”width: 500px;” summary=”This table is a weekly menu of breakfast, lunch and dinner items at Michelle’s apartment”><caption style=”text-align: left; font-weight: bold;”>Michelle’s Weekly Menu</caption>
Section 508 §1194.22 (g) requires that row and column headers shall be identified for data tables. This means using the <th> tag to identify the the table headers. If we only had one row of headers, using <th> alone would be okay. However, we’ll be having two (days of the week and meals). So, it will also be necessary to use scope=”col” and scope=”row” where appropriate.
Here’s the table so far:
Michelle’s Weekly Menu
|
Monday |
Tuesday |
Wednesday |
Thursday |
Here’s the code. Notice that we also added a scope=”col” attribute to all the th cells. We want the screen reader to know the cell is a head of a column. We also added an id attribute to give the header a name.
<table style=”width: 600px;” cellspacing=”0″ summary=”This table is a weekly menu of breakfast, lunch and dinner items at Michelle’s apartment”><caption style=”text-align: left; font-weight: bold;”>Michelle’s Weekly Menu</caption>
<tr style=”color: #fff; background-color: #bfa1e7;”><td style=”width:100px;”> </td><th style=”width: 100px; Padding: 10px;” scope=”col” id=”Monday”>Monday</th><th style=”width: 100px; Padding: 10px;” scope=”col” id=”Tuesday”>Tuesday</th><th style=”width: 100px; Padding: 10px;” scope=”col” id=”Wednesday”>Wednesday</th><th style=”width: 100px; Padding: 10px;” scope=”col” id=”Thursday”>Thursday</th><th style=”width: 100px; Padding: 10px;” scope=”col” id=”Friday”>Friday</th></tr>
Next we’ll add our first regular row of data.
Michelle’s Weekly Menu
|
Monday |
Tuesday |
Wednesday |
Thursday |
| Breakfast |
Oatmeal |
Eggs |
Smoothie |
Peanutbutter Toast |
Notice in the code the first cell is a header with a scope=”row”. The data cells have a header attribute to identify what row and cell it belongs to.
<table style=”width: 500px;” summary=”This table is a weekly menu of breakfast, lunch and dinner items at Michelle’s apartment” cellspacing=”0″><caption style=”text-align: left; font-weight: bold;”>Michelle’s Weekly Menu</caption>
<tbody>
<tr style=”color: #fff; background-color: #bfa1e7;”>
<td style=”width: 100px;”></td>
<th id=”Monday” style=”width: 100px; padding: 10px;” scope=”col”>Monday</th>
<th id=”Tuesday” style=”width: 100px; padding: 10px;” scope=”col”>Tuesday</th>
<th id=”Wednesday” style=”width: 100px; padding: 10px;” scope=”col”>Wednesday</th>
<th id=”Thursday” style=”width: 100px; padding: 10px;” scope=”col”>Thursday</th>
</tr>
<tr>
<th style=”color: #fff; background-color: #bfa1e7;” scope=”row” id=”Breakfast”>Breakfast</th>
<td style=”background-color: #d4c6e9; padding: 10px;” headers=”Breakfast Monday”>Oatmeal</td>
<td style=”background-color: #d4c6e9; padding: 10px;” headers=”Breakfast Tuesday”>Eggs</td>
<td style=”background-color: #d4c6e9; padding: 10px;” headers=”Breakfast Wednesday”>Smoothie</td>
<td style=”background-color: #d4c6e9; padding: 10px;” headers=”Breakfast Thursday”>Peanutbutter Toast</td>
Here’s the good news. You can officially become lazy.
Copy the data row and simple add in new data by hand. I choose to take out the light purple and have the data cells be white. (Personal opinion as I think it makes it easier to read.)
Michelle’s Weekly Menu
|
Monday |
Tuesday |
Wednesday |
Thursday |
| Breakfast |
Oatmeal |
Eggs |
Smoothie |
Peanut butter Toast |
| Lunch |
Veggie Pizza |
Sushi |
Salad |
Indian Food! |
Here’s the new code:
<table style=”width: 500px;” summary=”This table is a weekly menu of breakfast, lunch and dinner items at Michelle’s apartment” cellspacing=”0″><caption style=”text-align: left; font-weight: bold;”>Michelle’s Weekly Menu</caption>
<tbody>
<tr style=”color: #fff; background-color: #bfa1e7;”>
<td style=”width: 100px;”></td>
<th id=”Monday” style=”width: 100px; padding: 10px;” scope=”col”>Monday</th>
<th id=”Tuesday” style=”width: 100px; padding: 10px;” scope=”col”>Tuesday</th>
<th id=”Wednesday” style=”width: 100px; padding: 10px;” scope=”col”>Wednesday</th>
<th id=”Thursday” style=”width: 100px; padding: 10px;” scope=”col”>Thursday</th>
</tr>
<tr>
<th id=”Breakfast” style=”color: #fff; background-color: #bfa1e7;” scope=”row”>Breakfast</th>
<td style=”background-color: #d4c6e9; padding: 10px;” headers=”Breakfast Monday”>Oatmeal</td>
<td style=”background-color: #d4c6e9; padding: 10px;” headers=”Breakfast Tuesday”>Eggs</td>
<td style=”background-color: #d4c6e9; padding: 10px;” headers=”Breakfast Wednesday”>Smoothie</td>
<td style=”background-color: #d4c6e9; padding: 10px;” headers=”Breakfast Thursday”>Peanut butter Toast</td>
</tr>
<tr>
<th id=”Lunch” style=”color: #fff; background-color: #bfa1e7;” scope=”row”>Lunch</th>
<td style=”padding: 10px;” headers=”Lunch Monday”>Veggie Pizza</td>
<td style=”padding: 10px;” headers=”Lunch Tuesday”>Sushi</td>
<td style=”padding: 10px;” headers=”Lunch Wednesday”>Salad</td>
<td style=”padding: 10px;” headers=”Lunch Thursday”>Indian Food!</td>
</tr>
I’m finally going to add the last row in. Yes, I’m copying-and-pasting. Why do the extra work?
Michelle’s Weekly Menu
|
Monday |
Tuesday |
Wednesday |
Thursday |
| Breakfast |
Oatmeal |
Eggs |
Smoothie |
Peanut butter Toast |
| Lunch |
Veggie Pizza |
Sushi |
Salad |
Indian Food! |
| Dinner |
Pasta |
Fish & Potatoes |
Lemon Chicken |
Stirfry with chicken |
Here’s the final code:
<table style=”width: 500px;” summary=”This table is a weekly menu of breakfast, lunch and dinner items at Michelle’s apartment” cellspacing=”0″><caption style=”text-align: left; font-weight: bold;”>Michelle’s Weekly Menu</caption>
<tbody>
<tr style=”color: #fff; background-color: #bfa1e7;”>
<td style=”width: 100px;”></td>
<th id=”Monday” style=”width: 100px; padding: 10px;” scope=”col”>Monday</th>
<th id=”Tuesday” style=”width: 100px; padding: 10px;” scope=”col”>Tuesday</th>
<th id=”Wednesday” style=”width: 100px; padding: 10px;” scope=”col”>Wednesday</th>
<th id=”Thursday” style=”width: 100px; padding: 10px;” scope=”col”>Thursday</th>
</tr>
<tr>
<th id=”Breakfast” style=”color: #fff; background-color: #bfa1e7;” scope=”row”>Breakfast</th>
<td style=”background-color: #d4c6e9; padding: 10px;” headers=”Breakfast Monday”>Oatmeal</td>
<td style=”background-color: #d4c6e9; padding: 10px;” headers=”Breakfast Tuesday”>Eggs</td>
<td style=”background-color: #d4c6e9; padding: 10px;” headers=”Breakfast Wednesday”>Smoothie</td>
<td style=”background-color: #d4c6e9; padding: 10px;” headers=”Breakfast Thursday”>Peanut butter Toast</td>
</tr>
<tr>
<th id=”Lunch” style=”color: #fff; background-color: #bfa1e7;” scope=”row”>Lunch</th>
<td style=”padding: 10px;” headers=”Lunch Monday”>Veggie Pizza</td>
<td style=”padding: 10px;” headers=”Lunch Tuesday”>Sushi</td>
<td style=”padding: 10px;” headers=”Lunch Wednesday”>Salad</td>
<td style=”padding: 10px;” headers=”Lunch Thursday”>Indian Food!</td>
</tr>
<tr>
<th id=”Dinner” style=”color: #fff; background-color: #bfa1e7;” scope=”row”>Dinner</th>
<td style=”background-color: #d4c6e9; padding: 10px;” headers=”Dinner Monday”>Pasta</td>
<td style=”background-color: #d4c6e9; padding: 10px;” headers=”Dinner Tuesday”>Fish & Potatoes</td>
<td style=”background-color: #d4c6e9; padding: 10px;” headers=”Dinner Wednesday”>Lemon Chicken</td>
<td style=”background-color: #d4c6e9; padding: 10px;” headers=”Dinner Thursday”>Stirfry with chicken</td>
</tr></tbody></table>