TExcelTAD
Home › CSS › CSS Tables

CSS Tables

Unstyled HTML tables look cramped and hard to read. With a handful of CSS properties you can add borders, spacing, zebra stripes and hover highlights, and make wide tables scroll on small screens.

Borders and border-collapse

Give table, th and td a border and you'll see double lines, because each cell has its own border with gaps between them. border-collapse: collapse merges neighbouring borders into one.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; display: flex; gap: 24px; }
  table, th, td { border: 2px solid #5c6bc0; }
  .collapsed { border-collapse: collapse; }
  th, td { padding: 4px 8px; }
</style>
</head>
<body>
  <table>
    <tr><th>Default</th><th>Score</th></tr>
    <tr><td>Ana</td><td>91</td></tr>
    <tr><td>Ben</td><td>78</td></tr>
  </table>
  <table class="collapsed">
    <tr><th>Collapsed</th><th>Score</th></tr>
    <tr><td>Ana</td><td>91</td></tr>
    <tr><td>Ben</td><td>78</td></tr>
  </table>
</body>
</html>
Property Purpose
border-collapse separate (default) or collapse
border-spacing Gap between cells when separate, e.g. 6px
caption-side top or bottom for the <caption>
empty-cells show or hide borders of empty cells (separate mode)
table-layout auto (fit content) or fixed (use set widths — faster, predictable)

Size, alignment and padding

Set width on the table (often 100%). Text in th is centred and bold by default; text-align and vertical-align adjust alignment. Padding on cells is the single biggest readability improvement.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  table { width: 100%; border-collapse: collapse; }
  th, td { padding: 10px 12px; border-bottom: 1px solid #ddd; }
  th { text-align: left; background: #00897b; color: white; }
  td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
  tr.tall td { height: 50px; vertical-align: bottom; }
</style>
</head>
<body>
  <table>
    <tr><th>Item</th><th class="num">Qty</th><th class="num">Price</th></tr>
    <tr><td>Notebook</td><td class="num">3</td><td class="num">$4.50</td></tr>
    <tr><td>Pen</td><td class="num">12</td><td class="num">$0.80</td></tr>
    <tr class="tall"><td>Backpack (bottom-aligned)</td><td class="num">1</td><td class="num">$39.00</td></tr>
  </table>
</body>
</html>

Tip: Right-align numeric columns and use font-variant-numeric: tabular-nums so digits line up.

Zebra stripes and hover

:nth-child(even) stripes alternate rows; tr:hover highlights the row under the mouse. Both help the eye track across wide rows.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  table { border-collapse: collapse; width: 100%; }
  caption { caption-side: bottom; font-size: 13px; color: #777; padding-top: 6px; }
  th { background: #37474f; color: white; padding: 8px; text-align: left; }
  td { padding: 8px; }
  tbody tr:nth-child(even) { background: #eceff1; }
  tbody tr:hover { background: #ffe082; }
</style>
</head>
<body>
  <table>
    <caption>Hover a row to highlight it</caption>
    <thead><tr><th>City</th><th>Country</th><th>Population</th></tr></thead>
    <tbody>
      <tr><td>Hanoi</td><td>Vietnam</td><td>8.4M</td></tr>
      <tr><td>Lisbon</td><td>Portugal</td><td>0.5M</td></tr>
      <tr><td>Nairobi</td><td>Kenya</td><td>4.4M</td></tr>
      <tr><td>Lima</td><td>Peru</td><td>10.0M</td></tr>
    </tbody>
  </table>
</body>
</html>

Responsive tables

A table with many columns can be wider than a phone screen. Wrap it in a container with overflow-x: auto so the table scrolls horizontally instead of breaking the page layout.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; }
  .scroll { overflow-x: auto; border: 2px solid #8d6e63; max-width: 320px; }
  table { border-collapse: collapse; }
  th, td { padding: 8px 14px; border: 1px solid #d7ccc8; white-space: nowrap; }
  th { background: #efebe9; position: sticky; left: 0; }
</style>
</head>
<body>
  <div class="scroll">
    <table>
      <tr><th>Month</th><td>Jan</td><td>Feb</td><td>Mar</td><td>Apr</td><td>May</td><td>Jun</td><td>Jul</td></tr>
      <tr><th>Sales</th><td>120</td><td>135</td><td>160</td><td>150</td><td>190</td><td>210</td><td>230</td></tr>
    </table>
  </div>
  <p>Scroll the box sideways — the first column stays put thanks to <code>position: sticky</code>.</p>
</body>
</html>

Note: Use tables for tabular data only. For page layout, use Flexbox or Grid.

Test yourself

Which property merges adjacent cell borders into a single line?

  1. border-spacing: 0
  2. border-collapse: collapse
  3. border-merge: true
  4. table-layout: fixed

How do you add zebra stripes to table rows?

  1. tr:odd-even
  2. tr:nth-child(even) { background: ... }
  3. table { stripes: on }
  4. tr + tr

How can a wide table be made usable on small screens?

  1. Set font-size to 1px
  2. Wrap it in a container with overflow-x: auto
  3. Use display: none
  4. Use border-collapse