Say you’ve got an HTML table, with a <th>
cell that spans several columns, e.g.
<table>
<tr>
<th colspan="3" scope="?">Scores</th>
<!-- ...more headings here... -->
</tr>
<tr>
<th scope="col">English</th>
<th scope="col">Maths</th>
<th scope="col">Science</th>
<!-- ...more sub-headings here... -->
</tr>
<tr>
<td>12</td>
<td>16</td>
<td>20</td>
<!-- ...more cells here... -->
</tr>
</table>
What’s the correct value for the scope attribute for the spanning header cell? col
seems incorrect as it’s heading several columns, but colgroup
does’t seem right if I don’t actually have any colgroup
tags.
The scope attribute specifies whether a header cell is a header for a column, row, or group of columns or rows. Note: The scope attribute has no visual effect in ordinary web browsers, but can be used by screen readers.
To merge cells in HTML, use the colspan and rowspan attribute. The rowspan attribute is for the number of rows a cell should span, whereas the colspan attribute is for a number of columns a cell should span. Both the attribute will be inside the <td> tag.
The rowspan attribute in HTML specifies the number of rows a cell should span. That is if a row spans two rows, it means it will take up the space of two rows in that table. It allows the single table cell to span the height of more than one cell or row.
The rowspan and colspan are the attributes of <td> tag. These are used to specify the number of rows or columns a cell should merge. The rowspan attribute is for merging rows and the colspan attribute is for merging columns of the table in HTML.
The WebAIM (Web Accessibility in Mind) group has a great article on creating accessible data tables. Overall, they recommend avoiding spanned rows or columns, as screen readers cannot interpret the markup reliably.
Short of avoiding spanned columns altogether, I've had really good luck using the id/headers attributes in combination with the scope attribute. Although the markup is more verbose, this seems to simplify things for JAWS, and as a result it has less trouble interpreting the data.
This is what your example would look like with id/headers:
<table>
<tr>
<th id="scores" colspan="3">Scores</th>
</tr>
<tr>
<th id="english" scope="col">English</th>
<th id="maths" scope="col">Maths</th>
<th id="science" scope="col">Science</th>
</tr>
<tr>
<td headers="scores english">12</td>
<td headers="scores maths">16</td>
<td headers="scores science">20</td>
</tr>
</table>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With