Bootstrap 5 Table Color Classes for Rows and Cells

Bootstrap’s table color classes, which version 3 called contextual or emphasis classes, highlight a row or a single cell: a failed job in red, a paid invoice in green, the selected record in gray. In Bootstrap 5.3 they all start with table-.

From Bootstrap 3 to 5

Bootstrap 4 renamed the classes, so the old .success, .info, .warning, .danger and .active on a <tr> no longer do anything. The new names are:

Bootstrap 3Bootstrap 5.3
.active.table-active
.success.table-success
.info.table-info
.warning.table-warning
.danger.table-danger
(none).table-primary, .table-secondary, .table-light, .table-dark

Example

The stylesheet is all you need: https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css.

<table class="table">
  <thead>
    <tr>
      <th scope="col">#</th>
      <th scope="col">First name</th>
      <th scope="col">Last name</th>
      <th scope="col" class="text-end">Points</th>
      <th scope="col">Status</th>
    </tr>
  </thead>
  <tbody>
    <tr class="table-active">
      <th scope="row">1</th>
      <td>George</td>
      <td>Hagi</td>
      <td class="text-end">100</td>
      <td>Selected</td>
    </tr>
    <tr class="table-success">
      <th scope="row">2</th>
      <td>Jill</td>
      <td>Smith</td>
      <td class="text-end">70</td>
      <td>Qualified</td>
    </tr>
    <tr class="table-info">
      <th scope="row">3</th>
      <td>Eve</td>
      <td>Jackson</td>
      <td class="text-end">20</td>
      <td>New player</td>
    </tr>
    <tr class="table-warning">
      <th scope="row">4</th>
      <td>Adam</td>
      <td>Johnson</td>
      <td class="text-end">15</td>
      <td>At risk</td>
    </tr>
    <tr>
      <th scope="row">5</th>
      <td>Maria</td>
      <td>Anders</td>
      <td class="table-danger text-end">0</td>
      <td>Eliminated</td>
    </tr>
  </tbody>
</table>

The classes work on three levels. Put one on the <table> to color the whole table, on a <tr> to color a row, or on a single <td> or <th> to color one cell, as in the last row above, where only the zero is red.

Do not rely on color alone

The status column in the example is not decoration. A screen reader does not announce background colors, and roughly one in twelve men has some form of color blindness, so red and green rows can look the same. If the color carries meaning, put that meaning in the text as well.

When there is no room for a visible status column, add text that only assistive technology reads, using .visually-hidden:

<tr class="table-danger">
  <th scope="row">5</th>
  <td>Maria</td>
  <td>Anders</td>
  <td class="text-end">
    0 <span class="visually-hidden">(eliminated)</span>
  </td>
</tr>

Why .bg-danger does not work on a row

It is tempting to reach for the background utilities, but <tr class="bg-danger"> shows no color. Bootstrap 5 paints each cell’s background through CSS variables (--bs-table-bg and a few state variables), so the cell covers whatever is set on the row. The .table-* variant classes set those variables, which is why they work and the utilities do not. On a single cell, .bg-* does work, but the .table-* classes also adjust the text and border colors to match, so prefer them.

Choosing the class in code

When the table comes from server-side data, map the state to a class in one place instead of scattering if statements through the view. In a Razor page or view, for example:

@functions {
    static string? RowClass(int points) => points switch
    {
        0 => "table-danger",
        < 20 => "table-warning",
        >= 70 => "table-success",
        _ => null
    };
}
<tr class="@RowClass(player.Points)">

Returning null rather than an empty string matters: Razor drops an attribute whose value is null, so ordinary rows render as a plain <tr> instead of <tr class="">.

Related examples

Leave a Comment