Skip to content
Tutorials Center
My learning

Topics

Lesson 30 of 122 Β· Tables

Styling Tables with CSS

Turn plain HTML tables into clear, readable data with CSS. Borders and border-collapse, padding, alignment, zebra rows, hover, sticky headers and scrolling on small screens.

An unstyled table is just text floating in a grid. A few well-chosen CSS rules make it easy to scan: lines that guide the eye, room around each value, numbers that line up and a header that stays put while you scroll.

This lesson walks through those rules one at a time, using a single league table as the running example. Every technique here is plain CSS, so the HTML from the previous lessons doesn't change at all.

Start with clean HTML

Good styling begins with good structure. The table below has a caption, a <thead> and a <tbody>, which gives CSS clear hooks to target. (Review Table Headers, Sections and Captions if any of that is new.)

HTML
<table class="league">
  <caption>Riverside Five-a-Side League, week 8</caption>
  <thead>
    <tr>
      <th scope="col">Pos</th>
      <th scope="col">Team</th>
      <th scope="col">Played</th>
      <th scope="col">Points</th>
    </tr>
  </thead>
  <tbody>
    <tr><td>1</td><th scope="row">Canal Rovers</th><td>8</td><td>19</td></tr>
    …
  </tbody>
</table>

Borders and border-collapse

If you put border: 1px solid on every cell, you get double lines: each cell draws its own box, and the browser leaves a small gap between neighboring cells. That gap is controlled by border-spacing.

Usually you want single, shared lines instead. One property does it:

CSS
table { border-collapse: collapse; }

With collapsed borders, neighboring cells share one border, border-spacing is ignored and borders on rows and columns start working too. Try toggling it in the example below.

Separate vs collapsed borders

Many modern designs skip vertical lines entirely and draw only a thin line under each row. That looks lighter and still guides the eye across:

CSS
th, td { border-bottom: 1px solid #e2e8f0; }

Spacing and alignment

Cramped cells are hard to read. Give every cell some padding, and then align content by type:

  • Text reads best left-aligned (text-align: start, which also works in right-to-left languages).
  • Numbers should be right-aligned so units, tens and hundreds line up. Add font-variant-numeric: tabular-nums so every digit has the same width.
  • Headers should match the alignment of the column they label. A centered header over right-aligned numbers looks off.
  • Vertical alignment: cells are vertically centered by default. When some cells wrap onto several lines, vertical-align: top often looks tidier.

Zebra rows and hover

Alternating row colors, often called zebra striping, help readers follow a long row across the table. The :nth-child() selector does it without any classes:

CSS
tbody tr:nth-child(even) { background: #f8fafc; }

A hover highlight helps in a similar way when someone is reading with a mouse:

CSS
tbody tr:hover { background: #fef9c3; }

Here is everything so far, combined into one league table:

A styled league table

Experiment: change even to odd, swap the header color, or add border-left: 4px solid #16a34a to the first row to mark the leader.

Keep enough contrast. Stripes and hover colors should be subtle, but text must stay easy to read on every background. Dark text on very light tints, as above, is a safe choice.

Sticky headers

In a long table, readers scroll past the header row and forget which column is which. position: sticky keeps the header cells pinned while the rest of the table scrolls:

CSS
thead th {
  position: sticky;
  top: 0;
  background: #0f172a; /* needed so rows don't show through */
}

The header sticks to the nearest scrolling container. That can be the page itself, or a wrapper you give a fixed height and overflow: auto, as in the next example. Two details matter:

  • Give sticky cells a background, or the scrolling rows will be visible behind them.
  • Stick the th cells rather than the <thead> element itself; it's the most reliable approach across browsers.

Collapsed borders don't stick. With border-collapse: collapse, the border under a sticky header stays behind as the rows scroll. If you need a visible line under a sticky header, use a box-shadow such as box-shadow: 0 1px 0 #cbd5e1; on the header cells instead.

Wide tables on small screens

A table with many columns is wider than a phone screen. Shrinking the text until it fits makes it unreadable, and letting the whole page scroll sideways breaks the rest of the layout. The common solution is a scrolling wrapper: only the table scrolls, and the page stays put.

Scrolling wrapper with sticky header

Notice the three attributes on the wrapper <div>:

  • tabindex="0" lets keyboard users focus the box and scroll it with the arrow keys.
  • role="region" and aria-labelledby give it a name, so a screen reader announces "Island ferry departures, region" when it receives focus. You'll learn more about these in ARIA Roles, States and Properties.

Narrow the preview (or your browser window) and the table scrolls sideways inside its box while the header stays pinned on vertical scroll.

Don't change the display of table elements. Setting display: block or display: grid on <table>, <tr> or <td> to make a "responsive" table can cause some browsers to stop exposing it as a table to screen readers. If you must restyle a table into cards, test it with assistive technology, as covered in Accessible Tables.

A few more useful properties

  • width: 100% on <table> makes it fill its container; table-layout: fixed plus column widths keeps columns steady (see Column Groups).
  • caption-side: bottom moves the caption under the table.
  • empty-cells: hide hides borders and backgrounds of empty cells (only with separate borders).
  • white-space: nowrap keeps short values like times and dates on one line.

Check your understanding

Quick check Β· 4 questions

of 4 answered You scored / 4 Β· perfect!

  1. 1. Which CSS rule turns double cell borders into single shared lines?

    border-collapse: collapse merges neighboring cell borders into one line. border-spacing: 0 removes the gap but still draws two borders side by side, and single isn't a border style.
  2. 2. Which selector stripes every other row in the table body?

    :nth-child(even) matches the 2nd, 4th, 6th… rows of each <tbody>.
  3. 3. Why should a sticky header cell have a background color?

    Header cells are transparent by default, so the rows scrolling underneath would be visible through them.
  4. 4. Why does the scrolling wrapper get tabindex="0"?

    A scrolling box that can't receive focus can't be scrolled with a keyboard. tabindex="0" adds it to the normal tab order.

Summary

  • Use border-collapse: collapse for single, shared borders; border-spacing only applies to separate borders.
  • Add padding to every cell, left-align text and right-align numbers with tabular-nums.
  • tbody tr:nth-child(even) creates zebra rows; tr:hover adds a hover highlight.
  • position: sticky; top: 0 on thead th pins the header; give it a background and use box-shadow for its bottom line.
  • Wrap wide tables in a focusable, labeled scrolling container instead of letting the page scroll sideways.
  • Avoid changing display on table elements, which can hide their table semantics.

Next lesson: Accessible Tables.