Sub topic 12
Common mistakes and legacy attributes to avoid
This chapter assumes you have read the earlier chapters on table, th, caption, row groups, spanning, scope, and CSS styling. It collects the errors that show up most often in real pages and the old attributes you will meet in older code.
Using a table for page layout
Tables once positioned sidebars and headers. That is now wrong. A screen reader announces a layout table as data, and the markup does not adapt to small screens. Use CSS Grid or Flexbox for layout. Use a table only when the content has meaning along both rows and columns.
A quick test: if you can't write a sensible header for each column, you probably don't have a table.
Structural mistakes
Rows with different column counts
Every row should cover the same number of columns. The cause is usually a colspan or rowspan that doesn't match the grid.
<table>
<tr>
<th scope="col">Name</th>
<th scope="col">Math</th>
<th scope="col">Art</th>
</tr>
<tr>
<td>Ana</td>
<td>90</td>
</tr>
</table>The second row covers two columns, and the header row covers three. Browsers render it, but the grid is ragged and assistive technology may pair cells with the wrong headers. Add the missing cell, or use colspan="2" on the last cell if the merge is intended.
Spans that overlap
A rowspan reserves cells in the rows below it. If you also write a cell in that spot, the two cells collide. When you use rowspan="2" in row one, row two needs one fewer cell. Count the columns for each row after every span you add.
Headers made from `td`
Bolding a td with CSS looks like a header, but it isn't one. Use th with scope so that screen readers can announce the header with each data cell.
Captions faked with other elements
A paragraph above the table has no programmatic link to it. Use caption as the first child of table.
Empty cells without meaning
An empty td can confuse a reader who relies on audio. Write a value such as "n/a" or a dash when the data is missing. The one exception is the empty corner cell above row headers, as in the multi-level example in the earlier chapter.
Legacy presentation attributes
Older HTML put styling in attributes. They are obsolete in HTML5, and CSS replaces each one.
| Legacy attribute | Use instead |
|---|---|
| `border="1"` | `border: 1px solid #ccc` on `th, td` |
| `cellpadding` | `padding` on `th, td` |
| `cellspacing` | `border-collapse: collapse` or `border-spacing` |
| `align` | `text-align` on cells, `margin-inline: auto` on the table |
| `valign` | `vertical-align` |
| `bgcolor` | `background-color` |
| `width`, `height` | `width`, `height` in CSS |
| `summary` | A `caption`, or a nearby paragraph |
Here is the same table written both ways.
<table border="1" cellpadding="8" cellspacing="0" width="100%" bgcolor="#f0f0f0">
<tr>
<td align="right">900</td>
</tr>
</table><style>
table {
border-collapse: collapse;
width: 100%;
background-color: #f0f0f0;
}
td {
border: 1px solid #ccc;
padding: 8px;
text-align: right;
}
</style>
<table>
<tr>
<td>900</td>
</tr>
</table>The second version keeps content and presentation apart, so one stylesheet can restyle every table on the site.
CSS mistakes
- Borders on
tableonly. Aborderon thetableelement draws an outline, not lines between cells. Put borders onthandtd. - Doubled borders. Without
border-collapse: collapse, each cell keeps its own border and a gap shows between them. - Spacing with
<br>or empty rows. Usepaddingon cells instead. - Hiding data to fit small screens. Removing columns with
display: nonealso removes them from some assistive technology. Use a scroll wrapper, as covered in the previous chapter. - Using
display: blockorflexon table elements without care. This can strip the table semantics that screen readers use. If you change the display type, test with a screen reader or add the roles back.
A short review checklist
- Is the data tabular, with a header for every column?
- Does the table have a
caption? - Do headers use
thwith the rightscope? - Does every row cover the same number of columns after spans?
- Is all styling in CSS, with no obsolete attributes?
Exercise
A page has <table cellspacing="0" border="1"> and the author bolds the first row's td cells with CSS. Name the problems and give the fix.
Show answer
cellspacing and border are obsolete presentation attributes. Replace them with border-collapse: collapse on the table and border: 1px solid #ccc on th, td. The first row uses td styled to look like headers, so assistive technology doesn't treat them as headers. Change them to th scope="col" and put them in a thead.
Exercise
Row one of a table has a cell with rowspan="2" followed by two other cells. Row two contains three td cells. What goes wrong and how do you fix it?
Show answer
The rowspan already occupies the first column of row two, so row two has one cell too many and the extra cell overflows the grid. Remove one td from row two so it holds two cells.