Spanning Cells & Accessible Tables
Merge cells across rows and columns, add a caption, and make tables friendly to screen readers.
What you will learn
- Merge cells with colspan and rowspan
- Add a table caption
- Use scope for accessibility
Merging cells
Make a cell stretch across columns with colspan, or down across rows with rowspan.
<table border="1">
<tr>
<th colspan="2">Full Name</th>
</tr>
<tr>
<td>Asha</td>
<td>Rao</td>
</tr>
</table>The first row has a single <th colspan="2"> — colspan="2" makes that one cell stretch across two columns, so “Full Name” spans the full width. The row below it still has two separate cells (“Asha”, “Rao”) sitting under that wide header.
Note: Output: | Full Name | (one cell spanning both columns) | Asha | Rao |
Captions and accessibility
This next table adds two finishing touches. <caption> gives the table a visible title, and the scope attribute on each header cell tells screen readers (software that reads the page aloud) which cells it labels. Read the code, then the walk-through below it.
<table border="1">
<caption>Batch Schedule</caption>
<thead>
<tr><th scope="col">Day</th><th scope="col">Topic</th></tr>
</thead>
<tbody>
<tr><th scope="row">Mon</th><td>HTML</td></tr>
<tr><th scope="row">Tue</th><td>CSS</td></tr>
</tbody>
</table><caption> gives the whole table a title (“Batch Schedule”) shown above it. The scope attribute tells screen readers what each header labels: scope="col" means “this heads a column” and scope="row" means “this heads a row” (so “Mon” and “Tue” label their rows). Sighted users see a normal table; screen-reader users get the extra context.
Note: Output: Batch Schedule (caption above the table) Day | Topic Mon | HTML Tue | CSS
Tip: Use tables only for real tabular data (rows and columns that belong together). Do not use tables to lay out a whole page — that job belongs to CSS.
Q. Which attribute makes a cell span two columns?
✍️ Practice
- Build a table with a heading cell that spans two columns using
colspan. - Add a
<caption>andscopeattributes to your timetable.
🏠 Homework
- Create a 2×2 “invoice” where the total row spans across using
colspan.