CSS Table Background Colors: Headers, Stripes, and Cells

Set background-color on table for a base color and on thead for a distinct header. To stripe rows or columns, use :nth-child(even) to target alternate rows or cells.

Set the table and header background colors

Use background-color to fill the part of the table you select. The samples below show the base, header, and status colors used in the example. Pair a dark header with light text.

Target Example selector Color role
Whole table .stock Base background
Header .stock thead Dark background with white text
Status cell .stock td.notice Highlighted status background
.stock {
  background-color: #f8fafc;
  color: #172532;
}
.stock thead {
  background-color: #17314a;
  color: #fff;
}
.stock td.notice { background-color: #d3efe2; }

Table backgrounds paint in layers. From bottom to top, the order is table, column groups and columns, row groups and rows, then cells. An opaque cell background covers the row background below it. A transparent cell reveals the layers underneath without inheriting their background color. See the CSS table background layer order.

Stripe alternate rows in the table body

tbody > tr:nth-child(even) colors alternate body rows while leaving the header and footer out. Use odd to start with the first row. If you want to swap both colors, update the table’s base color as well.

.stock.by-row tbody > tr:nth-child(even) {
  background-color: #e4eef8;
}
.stock.by-column tbody > tr > :nth-child(even):not(.notice) {
  background-color: #e4eef8;
}

Positions are counted within each parent. With multiple tbody elements, counting restarts in each group. For continuous striping, use one body group where appropriate or assign row classes when generating the table.

Stripe alternate columns

Use tr > :nth-child(even) to color every second cell in each row. A row header made with th also counts as a child. The CSS :nth-child() matching rules explain how positions are counted.

With colspan, the second child in the document tree may no longer occupy the second visual column. For such tables, add a class to the specific cells you want to highlight and check each row.

Highlight one cell and check text contrast

The notice cell combines a mint background with the text Low stock. In row mode, the cell background covers the underlying stripe. In column mode, :not(.notice) exempts that cell from the stripe selector.

Check the text color against each background you use. The Web Content Accessibility Guidelines (WCAG) minimum contrast guidance call for at least 4.5:1 for normal-sized text. Convey important information such as stock status with words as well as color.

Run the stock table and inspect its colors

Save the UTF-8 example as table-background.html and open it in a browser. It works without external libraries and starts in row mode.

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Stock levels</title>
  <style>
    body { font: 1rem/1.5 system-ui, sans-serif; margin: 1rem; }
    .jb-table-scroll { overflow-x: auto; }
    .stock { inline-size: min(100%, 36rem); border-collapse: collapse; }
    .stock caption { margin-block-end: .5rem; font-weight: bold; }
    .stock :is(th, td) {
      padding: .55rem .7rem;
      border-block-end: 1px solid #a5b7c7;
      text-align: start;
    }
    .stock {
      background-color: #f8fafc;
      color: #172532;
    }
    .stock thead {
      background-color: #17314a;
      color: #fff;
    }
    .stock td.notice { background-color: #d3efe2; }

    .stock.by-row tbody > tr:nth-child(even) {
      background-color: #e4eef8;
    }
    .stock.by-column tbody > tr > :nth-child(even):not(.notice) {
      background-color: #e4eef8;
    }
  </style>
</head>
<body>
  <div class="jb-table-scroll">
    <table class="jb-table-01 stock by-row">
      <caption>Stock levels</caption>
      <thead>
        <tr><th scope="col">Item</th><th scope="col">Status</th><th scope="col">Quantity</th></tr>
      </thead>
      <tbody>
        <tr><th scope="row">Pencils</th><td>In stock</td><td>12</td></tr>
        <tr><th scope="row">Notebooks</th><td class="notice">Low stock</td><td>2</td></tr>
        <tr><th scope="row">Erasers</th><td>In stock</td><td>51</td></tr>
        <tr><th scope="row">Rulers</th><td>In stock</td><td>36</td></tr>
      </tbody>
    </table>
  </div>
</body>
</html>

Alternate rows appear below the dark header. Replace by-row with by-column on <table> and reload to color alternate columns. The low stock cell keeps its highlight in both modes.

If a stripe is missing, inspect background-color on the row and its cells. When a theme gives cells an opaque color, make the relevant cells transparent or apply the stripe directly to the cells. For separation with lines, see CSS table borders and row dividers.