← Back to HTML & CSS, page 6

html-css-0111 · Easy fresher

Why are tables a poor tool for page layout, and what do you use instead?

A table tells assistive tools that content is data in rows and columns, so using it for layout adds noise, ties the order to the visuals and makes responsive changes hard. Use Flexbox or Grid for layout, and keep tables for real data.

Full answer

Years ago, pages were laid out with tables, because it was the only way to make columns. Today that is considered a mistake, for clear reasons.

  • Meaning: a table says that the content is data in rows and columns. Screen readers announce the number of rows and columns and let users move cell by cell. For a layout, that is noise, and the reading order may not match the visual order.
  • Flexibility: a table layout is rigid. Cells grow with their content in both directions. It is hard to change the layout for a phone without changing the HTML.
  • Weight and maintenance: layout tables need many nested elements, and the markup mixes structure with looks.
  • Search and tools: the content is harder to reuse, to print and to test.

The right tools are CSS Flexbox for one-dimensional rows or columns and CSS Grid for two-dimensional layouts. Both keep the HTML meaningful and let a media query change the layout.

When is a table right? When the content is truly tabular: a timetable, a price list, a score board. Use caption, th with scope and a clear structure. If an old email template forces a layout table, add role="presentation" so assistive tools ignore the table structure. HTML emails are one of the few places where this is still needed, because many email clients have weak CSS support.

Both versions show a menu on the left and content on the right. The table needs a role to hide its table meaning, while the grid uses real nav and main elements that carry the right meaning without extra work. At a narrow width, the grid can change to one column with a media query, and the table cannot.

Keep tables for data and use CSS for layout.

html
<style>
  .grid { display: grid; grid-template-columns: 100px 1fr; gap: 8px; }
  .grid > * { background: #eee; padding: 4px; }
</style>
<table id="lt" role="presentation"><tr><td id="ta">Menu</td><td id="tb">Content</td></tr></table>
<div class="grid" id="g"><nav id="ga">Menu</nav><main id="gb">Content</main></div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • Tables carry data meaning that layouts do not need.
  • Table layouts are rigid and hard to make responsive.
  • Use Flexbox for one dimension and Grid for two.
  • Real data should still use a table with caption and th.
  • Email templates may still need layout tables.

Common mistakes

  • Using a table for layout and adding spacer cells to move things around.
  • Using display: table styles on divs without thinking about the reason. Flexbox or Grid is usually simpler.
  • Removing table semantics from a real data table.

Follow-ups

  • What does role="presentation" do on a table?
  • How would you make a two-column layout stack on a phone?

Tips

  • Say that tables mean data, and layout needs different tools.
  • Mention HTML email as the honest exception.