Occasionally you may want to merge some cells in a table heading row.
This is not ideal for accessibility - make sure you read the note below.
To merge table cells, you do it via the source code.
Use the colspan code
If you're merging cells in a column (such as a heading), you insert the code and remove extra cells that relate to the spanning.
This table has the first row set up as the heading row.
In this example, the table has 5 columns. We have merged the heading cells in columns 1,2 and 3 and deleted the heading code snippet for columns 2 and 3. The rest of the table has 5 cells per row.
<table>
<thead>
<tr>
<th colspan="3">Head 1,2,3</th>
<th>Head 4</th>
<th>Head 5</th>
</tr>
</thead>
<tbody>
<tr>
<td>blah blah</td>
<td>blah blah</td>
<td>blah blah</td>
<td>blah blah</td>
<td>blah blah</td>
</tr>
<tr>
<td>blah blah</td>
<td>blah blah</td>
<td>blah blah</td>
<td>blah blah</td>
<td>blah blah</td>
</tr>
</tbody>
</table>
Here's what the above code looks like:
Use the rowspan code
If you've set up a table and chosen the first column as the heading row, you'll notice the code looks different to that above. There are no <thead> tags, instead the first cell in each row has <th scope="row"> which indicates it's a heading. Here's an example:
<table>
<tbody>
<tr>
<th scope="row">Head 1</th>
<td>data</td>
<td>data</td>
</tr>
<tr>
<th scope="row">Head 2</th>
<td>data</td>
<td>data</td>
</tr>
<tr>
<th scope="row">Head 3</th>
<td>data</td>
<td>data</td>
</tr>
<tr>
<th scope="row">Head 4</th>
<td>data</td>
<td>data</td>
</tr>
</tbody>
</table>
This is what the above code looks like:
To merge row cells, you insert the code and remove extra cells that relate to the spanning.
In this example, the table has 3 columns. We have merged the heading cells in column 1, rows 1 and 2. The cell with <th> tags in the second row has been deleted.
<table>
<tbody>
<tr>
<th scope="row" rowspan="2">Head 1 & 2</th>
<td>data</td>
<td>data</td>
</tr>
<tr>
<td>data</td>
<td>data</td>
</tr>
<tr>
<th scope="row">Head 3</th>
<td>data</td>
<td>data</td>
</tr>
</tbody>
</table>
Here's how this table displays:
Accessibility and merged table cells
Don't overuse merged cells in tables, they can be a tricky for screen readers, especially in complex data tables.
For example, one common, free screen reader app - NVDA - does not announce when a cell spans multiple rows or columns. (Source: accessibility-developer-guide.com).
Some users may find it easier to work with several simple tables than one more complex table. Authors should consider whether they can convert complex tables to one or more simple tables. (Source: w3.org)