Skip to content
Tutorials Center
My learning

Topics

Lesson 31 of 58 ยท Components

Cards

Learn Bootstrap 5 cards: card-body, header, footer, images, list groups inside cards, horizontal cards, row-cols card grids and the stretched-link helper.

A card is a bordered box that holds a small, self-contained piece of content: a product, an article teaser, a user profile, a price plan. Cards are everywhere in modern interfaces because they group related text, images and actions into one scannable unit. Bootstrap's card is a flexible container with a handful of parts you can mix and match.

You can build cards with almost everything you have learned so far. The layout of many cards comes from Responsive Columns, and the details inside are spacing and text utilities. This lesson is about the card-specific classes.

The parts of a card

A card is a div with card. Inside it you can place any of these parts, in any order:

  • card-body is the padded area for content. Most cards have at least one.
  • card-title, card-subtitle and card-text style the headings and paragraphs inside the body.
  • card-header and card-footer are bars at the top and bottom, with their own subtle background and a border.
  • card-img-top and card-img-bottom place an image flush with the edges, with matching rounded corners. The related class card-img fills the whole card, for overlay designs.
  • card-link styles links inside the body so they sit side by side.

By default a card is as wide as its parent. Constrain it with a width style (style="width: 18rem"), a grid column, or the w-* utilities from Sizing Utilities.

A basic card with every part

The image's width and height attributes reserve space so the page does not jump while it loads, a habit that helps layout stability. Notice the image inherits the card's rounded corners only where it touches an outer edge, which is why you use card-img-top rather than a plain img.

flowchart TD
  C["div.card"] --> H["div.card-header"]
  C --> I["img.card-img-top"]
  C --> B["div.card-body"]
  B --> T["h5.card-title"]
  B --> P["p.card-text"]
  B --> A["a.btn or a.card-link"]
  C --> F["div.card-footer"]

Every part is optional; the card only requires the outer .card.

Lists, buttons and colors

A card does not have to contain paragraphs. You can put a list-group with the list-group-flush modifier straight inside a card, between the body and the footer, and its borders blend into the card's. You will meet list groups in depth in List Groups, Breadcrumbs and Pagination.

For colors, apply background and text utilities to the card: text-bg-primary (a shortcut that sets a background and a readable text color in one class), border-success, or bg-body-tertiary. The helper text-bg-* is more reliable than combining bg-* and text-* by hand, because it picks a contrasting text color for you. See Colors and Backgrounds.

For action buttons, put them at the bottom of card-body or in the footer. A good habit is to align the button row consistently across a group of cards, which the next sections help with.

Horizontal cards

To place the image beside the text, nest a row inside the card. Use g-0 to remove the gutters, put the image in one column and the body in another, and give the image img-fluid rounded-start:

A horizontal card

Below the md breakpoint the two columns stack, with the image on top. The object-fit-cover and h-100 utilities keep the picture filling its column without distortion.

Grids of cards

Placing many cards in a row is a job for the grid. The neatest recipe uses row-cols-* to say how many cards per row at each breakpoint, and g-4 for the gap. Add h-100 to every card so the cards in a row have the same height, regardless of how long their text is.

A responsive card grid

Resize the preview: three cards per row on large screens, two on small, one on a phone. The middle card has much more text, yet all three share the same height because of h-100.

Bootstrap 5 still has card-group (joined cards with no gaps and equal height) but it is far less flexible than the grid, and card-deck was removed. Most projects use row-cols-* instead.

Cards in depth: overlays, groups and navigation

Three more card patterns cover most of what the basic parts do not.

Image overlay. Replace card-img-top with card-img and put the text in a card-img-overlay. The overlay is positioned on top of the image, so the card height follows the image (or the text, whichever is taller). Add a text colour utility that contrasts with the picture, and if the picture is busy, give the overlay a translucent background with bg-dark bg-opacity-50 so the text stays readable.

Card groups. card-group joins cards edge to edge with equal heights, removing the gap and the inner rounded corners. It stays side by side from sm upwards and stacks on phones. Use it for a pricing row where the cards should look like one block. For separated cards, the row-cols-* grid above is the better tool.

Navigation in the header. Put a nav with nav-tabs card-header-tabs or nav-pills card-header-pills inside the card-header. The negative margins pull the tabs onto the header border. Combine it with the tab plugin so each tab shows a different card-body.

Overlay, card group and header tabs

Look at the three joined cards: the borders between them are shared and the middle card sets the height for all three. A kitchen-sink card combines everything: a header, an image, a list group, a body, links and a footer. Build it one part at a time, in the order the parts should appear, because Bootstrap styles them by class and not by position.

Sometimes the whole card should be clickable, not just a small link. Add the class stretched-link to the main link inside the card. It adds an invisible overlay (a pseudo-element) that covers the nearest positioned ancestor. A card is already position: relative, so the entire card becomes the click target.

HTML
<div class="card">
  <div class="card-body">
    <h5 class="card-title">Read the guide</h5>
    <a href="/bootstrap/grid-basics" class="stretched-link">Go to the grid lesson</a>
  </div>
</div>

Use just one stretched link per card. If a card has several links, others will be hidden behind the overlay unless you give them position-relative and a higher z-index. Also keep the link text meaningful: screen reader users still hear only the link text, not the whole card.

Cards are layout containers, not semantic elements. If a list of cards is really a list of articles, wrap each in <article> or put the cards inside a <ul>/<li> structure so assistive technology understands the grouping.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. Which class places an image flush with the top edge of a card, with matching rounded corners?

    card-img-top rounds the top corners to match the card and makes the image fill the card's width.
  2. 2. How do you make all cards in a row the same height?

    The columns in a row are equal height by flexbox; h-100 makes each card fill its column.
  3. 3. What does stretched-link do?

    It adds an invisible pseudo-element that covers the nearest relatively positioned parent, so the whole card is a click target.
  4. 4. Which setup produces three cards per row on large screens and one on phones?

    row-cols-1 sets one column by default and row-cols-lg-3 changes it to three from lg upward.
  5. 5. Why prefer text-bg-primary over bg-primary text-white on a colored card?

    text-bg-* sets both background and a matching foreground in one class and keeps them in step with the theme.

Summary

  • A card is .card with optional card-header, card-body, card-footer and card-img-top/card-img-bottom.
  • Use card-title, card-subtitle, card-text and card-link for the content styles.
  • Horizontal cards nest a row g-0 with an image column and a body column.
  • Build card grids with row row-cols-* g-* and h-100 on each card; skip the removed card-deck.
  • Add stretched-link to one link to make the whole card clickable.

Next lesson: Alerts, Badges, Progress and Spinners.