TExcelTAD
Home › Bootstrap › BS5 Tables

Bootstrap 5 Tables

Bootstrap leaves plain <table> elements alone (tables are used for many things), so you opt in by adding the .table class. From there, modifier classes add stripes, borders, hover effects, colours and compact spacing.

A basic table

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <table class="table">
    <thead>
      <tr><th>#</th><th>Language</th><th>Released</th></tr>
    </thead>
    <tbody>
      <tr><td>1</td><td>JavaScript</td><td>1995</td></tr>
      <tr><td>2</td><td>Python</td><td>1991</td></tr>
      <tr><td>3</td><td>Rust</td><td>2015</td></tr>
    </tbody>
  </table>
</body>
</html>

Modifier classes

Combine any of these with .table:

Class Effect
.table-striped Zebra stripes on body rows
.table-striped-columns Stripes on alternate columns
.table-bordered Borders on every cell
.table-borderless No borders at all
.table-hover Highlights the row under the mouse
.table-sm Halves the cell padding
.table-dark Dark table (also .table-primary, .table-success, …)
.table-active Highlights a row or cell
.align-middle Vertically centres cell content
.caption-top Puts the <caption> above the table
.table-group-divider Thicker border between table groups (e.g. after <thead>)
html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <table class="table table-striped table-hover table-bordered table-sm caption-top">
    <caption>Team scores (hover a row)</caption>
    <thead class="table-dark">
      <tr><th>Team</th><th>Won</th><th>Lost</th></tr>
    </thead>
    <tbody>
      <tr><td>Falcons</td><td>8</td><td>2</td></tr>
      <tr class="table-active"><td>Otters</td><td>7</td><td>3</td></tr>
      <tr><td>Badgers</td><td>4</td><td>6</td></tr>
      <tr><td>Hares</td><td>1</td><td>9</td></tr>
    </tbody>
  </table>
</body>
</html>

Contextual row and cell colours

Use .table-{color} on a <tr> or <td> to colour just that row or cell. These are light tints designed to keep text readable.

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <table class="table align-middle">
    <thead><tr><th>Order</th><th>Status</th><th>Total</th></tr></thead>
    <tbody class="table-group-divider">
      <tr class="table-success"><td>#1001</td><td>Delivered</td><td>$42.00</td></tr>
      <tr class="table-warning"><td>#1002</td><td>Delayed</td><td>$18.50</td></tr>
      <tr class="table-danger"><td>#1003</td><td>Cancelled</td><td>$7.25</td></tr>
      <tr><td>#1004</td><td><span class="badge text-bg-info">Processing</span></td><td class="table-primary">$99.90</td></tr>
    </tbody>
  </table>
</body>
</html>

Responsive tables

Wide tables break small layouts. Wrap the table in .table-responsive and it scrolls horizontally inside its own box instead of stretching the page. Use .table-responsive-md (or sm, lg, xl, xxl) to scroll only below that breakpoint.

html · try it yourself
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body class="p-3">
  <div class="table-responsive">
    <table class="table table-bordered text-nowrap">
      <thead class="table-light">
        <tr><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th><th>Sat</th><th>Sun</th><th>Mon</th><th>Tue</th><th>Wed</th><th>Thu</th><th>Fri</th></tr>
      </thead>
      <tbody>
        <tr><td>Gym session</td><td>Rest day</td><td>Swimming</td><td>Rest day</td><td>Long run</td><td>Cycling</td><td>Rest day</td><td>Gym session</td><td>Yoga</td><td>Swimming</td><td>Rest day</td><td>Long run</td></tr>
      </tbody>
    </table>
  </div>
  <p class="small text-body-secondary">Scroll the table sideways — the page itself doesn't scroll.</p>
</body>
</html>

Tip: Keep <thead>, <tbody> and <th> in your markup. Bootstrap's styles target them (for example .table-hover only affects body rows), and they make tables accessible.

Note: .table-dark and the other .table-{color} classes set their own background, so in dark mode (data-bs-theme="dark") a plain .table already turns dark — you don't need .table-dark for that.

Test yourself

Which class adds zebra stripes to table rows?

  1. .table-zebra
  2. .table-striped
  3. .table-alt
  4. .table-rows

How do you make a wide table scroll horizontally on small screens?

  1. Add .table-scroll to the table
  2. Wrap it in a div with .table-responsive
  3. Add .overflow-table to the body
  4. Use .table-sm

What must every Bootstrap-styled table have?

  1. The .table class
  2. The .table-bordered class
  3. A caption
  4. A .container parent