Bootstrap 5: menu, cards, forms and dialogs

Lesson 12 of 17 · HTML, CSS and JavaScript

Bootstrap has ready-made components – parts of a page that repeat on almost every website: a menu that collapses into a "hamburger" button on a phone, cards, forms, alerts and dialogs. This lesson shows the most important ones and how to adapt them to your design.

Before you start: required prior knowledge – the Bootstrap grid.
How to learn: do the tasks at the end on your own in your editor first; open the hint and the solution only after that. Every example has the code and a live result below it; type the code into your own file and change it.

Cards

<div class="container py-3">
  <div class="row g-3">
    <div class="col-12 col-sm-6">
      <div class="card h-100 shadow-sm">
        <img src="https://picsum.photos/id/180/400/200" class="card-img-top" alt="A laptop on a desk">
        <div class="card-body">
          <h5 class="card-title">HTML course</h5>
          <p class="card-text">From the first page to forms.</p>
          <a href="#" class="btn btn-primary">Start</a>
        </div>
      </div>
    </div>
    <div class="col-12 col-sm-6">
      <div class="card h-100 border-success">
        <div class="card-header bg-success text-white">Recommended</div>
        <div class="card-body">
          <h5 class="card-title">Bootstrap</h5>
          <p class="card-text">A card without an image, with a header and a green border.</p>
        </div>
        <div class="card-footer text-body-secondary">6 lessons</div>
      </div>
    </div>
  </div>
</div>

Result:

A card (card) has the parts card-img-top, card-header, card-body, card-title, card-text and card-footer. The class h-100 makes the cards in the same row equally tall.

Forms

<form class="container py-3" style="max-width: 420px">
  <div class="mb-3">
    <label for="name" class="form-label">Logo name</label>
    <input type="text" class="form-control" id="name" placeholder="e.g. Pupin Club" required>
    <div class="form-text">Up to 20 characters.</div>
  </div>
  <div class="mb-3">
    <label for="kind" class="form-label">Kind</label>
    <select class="form-select" id="kind">
      <option>Business</option><option>Education</option><option>Sport</option>
    </select>
  </div>
  <div class="form-check mb-3">
    <input class="form-check-input" type="checkbox" id="slogan">
    <label class="form-check-label" for="slogan">Add a slogan</label>
  </div>
  <div class="input-group mb-3">
    <span class="input-group-text">@</span>
    <input type="email" class="form-control" placeholder="email">
    <button class="btn btn-danger" type="submit">Create</button>
  </div>
</form>

Result:

ClassFor
form-label, form-controla label and an input field (full width, rounded)
form-selecta drop-down list
form-check, form-check-inputcheckbox and radio
input-groupa field with an add-on or a button in the same row
mb-3spacing between fields

Alerts, badges and a dialog (modal)

<div class="container py-3">
  <div class="alert alert-success" role="alert">The logo has been saved! <span class="badge bg-success-subtle text-success-emphasis">3 logos</span></div>
  <div class="alert alert-warning alert-dismissible fade show" role="alert">
    This alert can be closed.
    <button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
  </div>
  <button class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#help">Open help</button>

  <div class="modal fade" id="help" tabindex="-1" aria-hidden="true">
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title">How do you make a logo?</h5>
          <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
        </div>
        <div class="modal-body">Type a name, choose a colour and a shape, then download the image.</div>
        <div class="modal-footer"><button class="btn btn-secondary" data-bs-dismiss="modal">OK</button></div>
      </div>
    </div>
  </div>
</div>

Result:

Components are "switched on" with the attributes data-bs-toggle, data-bs-target and data-bs-dismiss – you do not have to write the JavaScript yourself. The other components (image carousel, accordion, tabs, tooltip…) have ready-made examples in the documentation.

Customizing: your colours and your style

Bootstrap 5.3 uses CSS variables, so a lot can be changed in your main.css without touching the Bootstrap files:

<style>
  /* my CSS – after Bootstrap */
  .btn-brand {
    --bs-btn-bg: #dd0022;              /* background */
    --bs-btn-color: #fff;
    --bs-btn-hover-bg: #b0001b;        /* when the mouse is over it */
    --bs-btn-hover-color: #fff;
    --bs-btn-border-color: #dd0022;
    --bs-btn-hover-border-color: #b0001b;
    border-radius: 999px;
  }
  .title-brand { font-family: Georgia, serif; color: #dd0022; }
</style>
<div class="container py-3">
  <h2 class="title-brand">Logo Creator</h2>
  <button class="btn btn-brand px-4">My button</button>
  <button class="btn btn-primary">Ordinary button</button>
</div>

Result:

The whole site gets a dark theme with one attribute: <html lang="en" data-bs-theme="dark">.

Bootstrap 4 → Bootstrap 5: what has changed

On the internet (and in older lessons) you often find examples for Bootstrap 4. The most important differences:

Bootstrap 4Bootstrap 5
jQuery and Popper needed (3 scripts)one script bootstrap.bundle.min.js, without jQuery
data-toggle, data-target, data-dismissdata-bs-toggle, data-bs-target, data-bs-dismiss
ml-*, mr-*, pl-*, pr-*ms-*, me-*, ps-*, pe-* (start/end)
float-left, text-leftfloat-start, text-start
form-group, custom-selectmb-3, form-select
no-guttersg-0 (and g-1 … g-5)
maxcdn.bootstrapcdn.com/bootstrap/4.xcdn.jsdelivr.net/npm/bootstrap@5.3.x

Practice tasks

Each task has an example, a hint and a solution. The solutions have been checked in the browser.

TASK 01Difficulty: medium

A sports club page

Make a club page with: a navbar (the club name on the left, three items on the right, a "hamburger" on a phone), a row of three cards (teams: under-12, under-14, juniors) and a sign-up form (name, age, team from a drop-down list, button).

Common mistakes and tips
  • The menu does not open on a phone: check that bootstrap.bundle.min.js is included and that data-bs-target="#id" matches the id of the part that collapses.
  • Examples for Bootstrap 4 (data-toggle, jQuery) do not work with Bootstrap 5 without changes.
  • Do not change Bootstrap's files – write your changes in your own CSS.

Check your knowledge

Open the quiz

Bootstrap components

1. Which attribute in Bootstrap 5 opens a drop-down list or a menu?




2. What is needed for the "hamburger" menu to work?




3. Which class makes the cards in a row equally tall?




4. How do you write spacing on the left side in Bootstrap 5 (formerly ml-2)?