Tables show data in rows and columns, such as a timetable, a price list or a set of results. HTML builds a table from a few elements that are easy to remember.
Building a Basic Table
The <table> element wraps the whole table. Each <tr> element is one row. Inside a row, <th> makes a heading cell, which is bold and centred by default, and <td> makes a data cell. Every row must have the same number of cells unless cells are merged. The element <caption> gives the table a title.
For larger tables, <thead>, <tbody> and <tfoot> group the header rows, the main rows and the closing rows. These groups help screen readers and let CSS style each part.
Merging Cells
Sometimes one cell must cover several columns or rows, for example a day name above two class periods. The attribute colspan makes a cell cover several columns, and rowspan makes it cover several rows. The cells that are covered must be removed from the code, or the grid will break.
Definition: Rowspan and colspan. Attributes that let one table cell cover several rows or several columns.
Table 1.5: Attributes that merge table cells
| Attribute | Example | Effect |
|---|---|---|
| colspan | <td colspan="2"> |
The cell covers two columns |
| rowspan | <td rowspan="3"> |
The cell covers three rows |
Planning a Merged Table
Draw the grid on paper first and count the columns. A cell with colspan="2" uses two of the columns in its row, so that row needs two fewer separate cells. A cell with rowspan="2" also takes a position in the next row, so the next row needs one cell fewer.
Tables and Page Layout
Tables are for tabular data only. In the past, designers used tables to place elements on a page, but that approach makes pages hard to read on phones and hard for screen readers. Modern layouts use CSS, which the next unit explains.
Example 1.5: A timetable with a merged heading
<table border="1" cellpadding="6">
<caption>Monday</caption>
<tr><th colspan="2">Morning</th></tr>
<tr><td>9:00</td><td>HTML</td></tr>
<tr><td>10:00</td><td>CSS</td></tr>
</table>
Output in the browser:
The heading cell Morning spans both columns. The next two rows each have two cells.
Accessible Tables
A table should have a caption that says what it shows, and a header row of <th> cells. A screen reader announces the heading together with each data cell, so the user always knows which column or row a value belongs to. The scope attribute, written as scope="col" or scope="row", states whether a heading belongs to a column or to a row. Keep tables simple, avoid deep merging, and let wide tables scroll on small screens.
Common mistakes
- Using a table to lay out the whole page instead of CSS.
- Leaving covered cells in the code after using rowspan or colspan, which pushes cells out of place.
- Giving rows unequal numbers of cells without merging.
- Using td for heading cells instead of th.
Key takeaways
- A table is built from table, tr, th and td elements.
- The colspan and rowspan attributes merge cells across columns and rows.
- Count the cells in every row after merging, to keep the grid correct.
- Use tables for data, and CSS for page layout.
Practice questions
- Which element makes a heading cell?
- A cell with colspan 3 covers how many columns?
- Why should tables not be used for page layout?
Answers to the practice questions
- The th element.
- Three columns.
- They make pages hard to adapt to phones and hard for screen readers to follow.
Sources
- Official BCA syllabus (NEP 2025 scheme), Web Application Development, course code 25BCA405DS04, Unit I
