Bootstrap 5 Cards: The Replacement for Bootstrap 3 Panels

Panels were removed in Bootstrap 4. Their replacement is the card: a bordered box with an optional header, body and footer. .panel becomes .card, .panel-heading becomes .card-header, .panel-body becomes .card-body and .panel-footer becomes .card-footer. The contextual classes such as .panel-success have no direct equivalent; you build the same look with color utilities, as shown below.

Cards are pure CSS. They do not need Bootstrap’s JavaScript.

Page setup

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Bootstrap cards</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
  <main class="container py-4">
    <!-- cards go here -->
  </main>
</body>
</html>

Plain cards and cards with a header

<div class="card mb-3">
  <div class="card-body">Simple card without a header</div>
</div>

<div class="card mb-3">
  <div class="card-header">Sample 1</div>
  <div class="card-body">Card content 1</div>
</div>

Cards have no margin of their own, so the .mb-3 spacing utility keeps them apart when you stack several. Bootstrap 3 panels had a bottom margin built in, which is why converted pages often end up with cards touching each other.

Colored cards

The Bootstrap 3 contextual panels had a colored header on a white body. Two combinations come close to that in Bootstrap 5.3.

For a strong color, put .text-bg-primary (added in 5.2) on the header. It sets the background and picks a text color with enough contrast. A matching .border-primary on the card colors the outline:

<div class="card border-primary mb-3">
  <div class="card-header text-bg-primary">Sample 2</div>
  <div class="card-body">Card content 2</div>
</div>

For the softer look of the old success, info, warning and danger panels, use the subtle background and emphasis text colors introduced in 5.3:

<div class="card border-success-subtle mb-3">
  <div class="card-header bg-success-subtle text-success-emphasis">Sample 3</div>
  <div class="card-body">Card content 3</div>
</div>

<div class="card border-info-subtle mb-3">
  <div class="card-header bg-info-subtle text-info-emphasis">Sample 4</div>
  <div class="card-body">Card content 4</div>
</div>

<div class="card border-warning-subtle mb-3">
  <div class="card-header bg-warning-subtle text-warning-emphasis">Sample 5</div>
  <div class="card-body">Card content 5</div>
</div>

<div class="card border-danger-subtle mb-3">
  <div class="card-header bg-danger-subtle text-danger-emphasis">Sample 6</div>
  <div class="card-body">Card content 6</div>
</div>

The subtle and emphasis classes follow Bootstrap’s color modes. Add data-bs-theme="dark" to <html> and these cards switch to dark variants on their own, which a hard-coded style="background: #dff0d8" copied from an old theme will not do.

If you want the whole card filled, move the color class to the card itself: <div class="card text-bg-danger">.

Keep in mind that color alone tells a screen reader user nothing. If a red card means “error”, say so in the header text.

Titles, footers and list groups

A header can be a heading element, which is better for document structure than a div with bold text. Inside the body, .card-title and .card-text take care of spacing:

<div class="card mb-3">
  <h2 class="card-header h5">Order #1024</h2>
  <div class="card-body">
    <h3 class="card-title h6">Shipped</h3>
    <p class="card-text">Your parcel left the warehouse this morning.</p>
  </div>
  <ul class="list-group list-group-flush">
    <li class="list-group-item">2 items</li>
    <li class="list-group-item">Total: $48.00</li>
  </ul>
  <div class="card-footer text-body-secondary">Updated 2 hours ago</div>
</div>

The .h5 and .h6 classes let you choose the heading level that fits the page outline while keeping the visual size small. .list-group-flush removes the list group’s own borders and rounded corners so it sits flush inside the card, the same job .list-group did inside a Bootstrap 3 panel.

Cards in a grid

To show cards side by side, put each one in a grid column. .h-100 makes every card as tall as the tallest one in its row:

<div class="row row-cols-1 row-cols-md-3 g-4">
  <div class="col">
    <div class="card h-100">
      <div class="card-body">Short text</div>
    </div>
  </div>
  <div class="col">
    <div class="card h-100">
      <div class="card-body">A longer piece of text that wraps onto several lines.</div>
    </div>
  </div>
  <div class="col">
    <div class="card h-100">
      <div class="card-body">Medium text</div>
    </div>
  </div>
</div>

.row-cols-1 gives one card per row on phones and .row-cols-md-3 three per row from the medium breakpoint up. .g-4 sets the gutter in both directions, so stacked cards on small screens get vertical spacing too.

Related examples

Leave a Comment