← Back to HTML & CSS, page 5

html-css-0088 · Easy fresher

How do you style a data table so it is neat and easy to read, including zebra rows and a sticky header?

Use border-collapse: collapse, cell padding, right-aligned numbers, tbody tr:nth-child(even) for stripes and position: sticky with a background on thead th for the header. Wrap wide tables in an overflow container. Do not change table display values.

Full answer

A table already has the right structure in HTML (thead, tbody, th, td). CSS then controls the look.

Main properties:

  • border-collapse: collapse merges the borders of neighbouring cells into one line. The default, separate, draws a double border with gaps. With border-spacing you can control the gap in separate mode.
  • padding on th and td gives the text room.
  • text-align: left for text, right for numbers so digits line up. font-variant-numeric: tabular-nums makes digits the same width.
  • Zebra stripes: tbody tr:nth-child(even) { background: #f5f5f5; }. They help the eye follow a row.
  • A sticky header: thead th { position: sticky; top: 0; background: #fff; }. The header stays visible while the body scrolls. The cells need a background, or the rows show through. Put the table in a box with a fixed height and overflow: auto, or let the page scroll.
  • A caption gives the table a title. Style it like any other block.

For small screens, a wide table should be inside a wrapper with overflow-x: auto, so the page itself does not scroll sideways.

Do not use tables for page layout, and do not change display on table elements without thinking: display: block on a tr removes table semantics in some browsers and for some assistive technology.

The table has single lines between rows, right-aligned prices and grey stripes on every second row. When the box is scrolled, the header row stays at the top.

Check keyboard and screen reader behaviour in a real browser if the table is important.

html
<style>
  .wrap { height: 100px; overflow: auto; width: 260px; }
  table { border-collapse: collapse; width: 100%; font: 14px sans-serif; }
  caption { text-align: left; font-weight: bold; padding: 4px 0; }
  th, td { padding: 6px 8px; border-bottom: 1px solid #ccc; text-align: left; }
  td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
  thead th { position: sticky; top: 0; background: #fff; }
  tbody tr:nth-child(even) { background: #f5f5f5; }
</style>
<div class="wrap" id="wrap">
  <table id="t">
    <caption>Fees</caption>
    <thead><tr><th scope="col">Course</th><th scope="col" class="num">Price</th></tr></thead>
    <tbody id="tb">
      <tr><td>HTML</td><td class="num">10</td></tr>
      <tr><td>CSS</td><td class="num">20</td></tr>
      <tr><td>JavaScript</td><td class="num">30</td></tr>
      <tr><td>React</td><td class="num">40</td></tr>
      <tr><td>Node</td><td class="num">50</td></tr>
      <tr><td>SQL</td><td class="num">60</td></tr>
    </tbody>
  </table>
</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

  • border-collapse: collapse merges neighbouring borders.
  • Padding and alignment make cells readable.
  • nth-child(even) on rows creates zebra stripes.
  • position: sticky on thead th keeps the header visible.
  • A scroll wrapper keeps wide tables from breaking the page.

Common mistakes

  • Leaving the default border-spacing so borders look double and gappy.
  • Making a sticky header without a background, so rows show through while scrolling.
  • Setting display: block or flex on table rows, which can remove table semantics.

Follow-ups

  • Why does a sticky th need a background?
  • How do you handle a wide table on a phone?

Tips

  • Name border-collapse first, because every interviewer expects it.
  • Mention right-aligned numbers and tabular-nums as a small detail that shows care.