Sub topic 12
Common mistakes and legacy attributes to avoid
This chapter assumes you can write the core table elements (table, tr, td, th, caption, thead, tbody, tfoot) and use colspan, rowspan and scope. It also assumes you know basic CSS selectors. Here you review the errors that appear most often in real tables, the way browsers react to them, and the old presentational attributes that CSS replaced.
Structural mistakes the parser quietly "fixes"
Browsers do not reject invalid table markup. The HTML parser repairs it, and the repair is often not what you meant. Your source and the DOM (the tree the browser builds) can then differ, which makes bugs hard to find.
Cells and rows in the wrong place
A tr placed directly inside table is legal. The parser wraps it in an implied tbody. This surprises people who write a selector such as table > tr, because that selector never matches in the DOM.
Content that is not allowed inside a table is worse. A div or a stray text node between rows is moved out in front of the table. The spec calls this foster parenting.
<table>
<caption>Team</caption>
<div>Active members</div>
<tr>
<th scope="col">Name</th>
</tr>
<tr>
<td>Ana</td>
</tr>
</table>In the DOM, the div ends up before the table. If you want a label inside the table, use caption. If you want a note, put it outside the table or inside a cell.
Other repairs and errors of this kind:
- A
tddirectly insidetablegets an impliedtbodyandtraround it. - A
trinsidetrcloses the first row early. - Wrapping a
trin aformorasplits the table's structure. Put the form inside a cell, or use theformattribute on the controls. - A second
theadortfootis invalid. Use extratbodyelements to make more groups.
You can check the repaired structure in your browser's developer tools. The Elements panel shows the DOM, not your source.
Rows that do not add up
A table is a grid, and every row must cover the same number of columns once spans are counted. When a row is short or long, browsers extend the grid with empty cells, and your layout shifts.
<table>
<caption>Scores</caption>
<tr>
<th rowspan="2">Name</th>
<th colspan="2">Scores</th>
</tr>
<tr>
<th>Math</th>
<th>Art</th>
</tr>
<tr>
<td>Ana</td>
<td>90</td>
</tr>
</table>The last row covers only two columns, but the grid is three wide. The third column has no cell in that row. The fix is to add the missing td for Art. When you debug this, count each row as the sum of its cells' colspan values, plus the columns still occupied by rowspan cells from above.
Two related errors break the table model itself:
- Overlapping cells. A
rowspanthat reaches into a row that also has a cell in the same column slot is a table model error. Browsers draw the cells on top of each other. - A span that crosses a row group. A cell's
rowspanis cut off at the end of itsthead,tbodyortfoot. A header cell spanning fromtheadintotbodywill not do what you expect.
Also remember that rowspan="0" is valid in current HTML and means "to the end of the row group". Old browsers treated it differently, so write an explicit number if you need consistent results.
Semantic mistakes
Using a table for layout
Tables were once used to position page columns, navigation and images. Do not do this now. Screen readers announce layout tables as data tables, which makes the page confusing. Layout tables also resist responsive design. Use CSS Grid or Flexbox, and keep tables for data that has meaning along both rows and columns.
If you inherit a layout table that you cannot rewrite, add role="presentation" to the table. This removes the table semantics from the accessibility tree. Treat it as a stopgap and not as a design choice.
Faking headers with styled `td`
A bold or centered td looks like a header but is only a data cell. Assistive technology cannot connect it to the cells it describes. Use th, and give it scope so the direction is clear.
<table>
<caption>Monthly expenses</caption>
<tr>
<td><strong>Item</strong></td>
<td><strong>Cost</strong></td>
</tr>
<tr>
<td>Rent</td>
<td>900</td>
</tr>
</table>Replace both cells in the first row with <th scope="col">. Do the same for row labels such as "Rent", using <th scope="row">.
Empty or missing labels
- A table with no
captionhas no accessible name. Add one, or label it witharia-labelledbypointing to a nearby heading. - A visually empty top-left
this a problem. In a grid with both row and column headers, that corner cell has nothing to label. Use an emptytdfor the corner. If it must exist as a header, put visually hidden text in it. - A
scopeorheadersvalue that points to nothing does not warn you. Each id in aheaderslist must exist in the same table, and ids must be unique on the page.
Using `caption` as a heading, or a heading as a caption
caption must be the first child of table. It names that one table. A page heading such as h2 structures the document and is not a replacement. If a heading and a caption say the same thing, keep the caption and shorten the heading, or hide the caption visually and leave it for assistive technology.
Legacy attributes to avoid
Early HTML styled tables with attributes. Most are now obsolete, which means validators flag them and CSS does the same job better. Browsers still honor many of them, so you will meet them in old code.
| Obsolete attribute | What it did | Use instead |
|---|---|---|
| `border` | Drew borders on the table and cells | `border` on `th, td` with `border-collapse` |
| `cellpadding` | Padded inside cells | `padding` on `th, td` |
| `cellspacing` | Set gaps between cells | `border-spacing`, or `border-collapse: collapse` |
| `align` on `table` or `caption` | Aligned or floated the table | `margin-inline: auto`, `float`, or `text-align` |
| `align`, `valign`, `char` on cells and rows | Aligned cell content | `text-align`, `vertical-align` |
| `bgcolor` | Set the background color | `background-color` |
| `width`, `height` on `table` or cells | Set sizes | `width` and `height` in CSS |
| `frame`, `rules` | Chose which borders to draw | `border-*` properties on specific elements |
| `summary` | Described the table to screen readers | `caption`, or text near the table |
| `nowrap` | Stopped text wrapping | `white-space: nowrap` |
Not every attribute on th and td is obsolete. colspan, rowspan, headers, scope and abbr are current HTML.
A typical legacy table and its modern equivalent:
<table border="1" cellpadding="8" cellspacing="0" width="100%" bgcolor="#f0f0f0">
<tr>
<td align="right" valign="top">900</td>
</tr>
</table><style>
.expenses {
border-collapse: collapse;
width: 100%;
background-color: #f0f0f0;
}
.expenses td {
border: 1px solid #000;
padding: 8px;
text-align: right;
vertical-align: top;
}
</style>
<table class="expenses">
<caption>Rent</caption>
<tr>
<td>900</td>
</tr>
</table>The cellspacing="0" setting is not the same as border-collapse: collapse. Collapse merges adjacent borders into one line, while a zero spacing value leaves two borders touching and looks thicker. Choose the CSS that matches the look you want.
Styling and responsive mistakes
Borders on the wrong element
Putting border: 1px solid on table draws only an outer frame. The cell borders must be set on th and td. Without border-collapse: collapse, each cell keeps its own border, and the spacing between them stays visible.
Expecting `colgroup` to do everything
Styles on col and colgroup affect only a few properties: border, background, width and visibility. Setting text-align or color there has no effect on the cells. This is a frequent surprise after you have written the column rule. Put those properties on the cells themselves, for example with td:nth-child(2) or a class on each cell.
Also, column rules do not apply when the cell has its own background. A td background paints over the col background.
Changing `display` and losing the table
Responsive "stacked" tables are often built with display: block on tr, td and th. In some browser and screen reader combinations, this removes the table semantics, and the content is read as plain text. If you must restack cells, check the result with a screen reader. Add the matching ARIA roles (role="table", role="row", role="cell") only when you have verified that you need them. A safer default is the scroll wrapper from the previous chapter.
Hiding columns on small screens
Using display: none on a column's cells removes the data for every user, including those on assistive technology. If that data matters, keep it reachable, for example through a scroll wrapper. If it does not matter, ask whether it belongs in the table at all.
A quick review path
When a table looks wrong, work from structure to style, and stop at the first step that finds a fault.
Run the HTML through a validator, such as the W3C Nu checker, before you check anything else. It reports foster-parented content, overlapping cells, and obsolete attributes in one pass.
Summary of rules to keep
- Keep everything inside a table within a row and cell, apart from
caption,colgroup, and the row groups. - Count columns, including spans, in every row.
- Use
thwithscopefor headers, andcaptionfor the name. - Use CSS for borders, spacing, alignment, colors and sizes. Keep
colspan,rowspan,headers,scopeandabbr. - Do not use tables for page layout.
- Check the DOM and the accessibility behavior after any responsive restyling.
Exercise
A table has <div>Note</div> placed between <caption> and the first <tr>. Where does the browser put the div, and what is the correct way to keep a note with the table?
Show answer
The parser moves the div out of the table, to just before it (foster parenting). Put the note outside the table, in a paragraph after it, or inside a cell, or include it in the caption.
Exercise
Rewrite <table border="1" cellpadding="6" cellspacing="0"> as a CSS rule for a table with the class data, so cells have 1px borders and 6px padding with no gaps between them.
Show answer
.data { border-collapse: collapse; } .data th, .data td { border: 1px solid #000; padding: 6px; }
Exercise
A table has a 3-column grid. Row 1 has <th rowspan="2">, one <th colspan="2">. Row 2 has two <th> cells. Row 3 has <td>, <td>. What is wrong with row 3, and how do you fix it?
Show answer
Row 3 covers only two of the three columns, because the rowspan from row 1 ends after row 2. Add a third td so that row 3 has three cells.
Exercise
You set col { text-align: right; } on a numeric column and nothing changes. Why, and what should you do?
Show answer
col and colgroup only support border, background, width and visibility. Apply text-align to the cells, for example with a class on each td or with a td:nth-child() selector.