Multi-level headers are useful when several columns share a parent label. Colspan and scope="colgroup" can express that relationship, while lower rows identify individual columns. Before writing the code, picture the information as a small grid. Each label should help a reader understand the value next to it.
What You Need to Know
Make sure the physical grid matches the labels. Assistive-technology interpretation can vary, so test complicated tables with a screen reader.
Read a table like a story about data. The caption says what you are looking at; row and column headers explain what the numbers or words mean.
Example: See It in Action
Try this markup in a fresh practice file. The next section explains the parts worth paying attention to.
This is a short HTML fragment. To try it as a webpage, put it inside the body element of a complete HTML document.
<table>
<caption>Homework progress</caption>
<thead>
<tr>
<th rowspan="2" scope="col">Student</th>
<th colspan="2" scope="colgroup">Tasks</th>
</tr>
<tr>
<th scope="col">Done</th>
<th scope="col">Left</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">Alex</th>
<td>7</td>
<td>2</td>
</tr>
</tbody>
</table>
Let’s Understand the Example
- Table represents information whose row and column relationships matter.
- Caption gives the whole data table a clear name.
- Scope explains which table row, column or group a th cell labels.
- Rowspan makes a table cell stretch across several rows.
- Colspan makes a table cell stretch across several columns.
Look at a data cell and ask: “What row and column does this value belong to?” The header elements help answer that question, including for people who navigate the table with a screen reader.
What Should You See?
You should see a grid for “Homework progress”, with headers such as “Student”, “Tasks”, “Done”. The browser displays cells, but meaningful table markup also helps readers connect each cell to its labels.
Read the first data row aloud together with its column labels. If you cannot explain a value without pointing to its position on screen, strengthen the headers.
Try It Yourself
- Add another group of two related columns and check the header associations.
- Save your changes, open or refresh the page, and compare the result with the original example.
- Tell a friend what your change did. If you are not sure, undo it, refresh the page, and compare.
Common Beginner Mistakes
- Merging header cells without checking which data columns each one spans.
- Do not replace actual header cells with bold ordinary cells. Header markup is part of the meaning, not just the appearance.
Key Takeaways
- Multi-level headers are useful when several columns share a parent label.
- Table represents information whose row and column relationships matter.
- Make sure the physical grid matches the labels. Assistive-technology interpretation can vary, so test complicated tables with a screen reader.