Skip to content
Tutorials Center
My learning

Topics

Lesson 56 of 58 ยท Practice and Review

Exercises with Solutions

Practice Bootstrap 5.3 with six hands-on exercises covering the grid, utilities, forms, cards, navbars and modals. Every exercise has a runnable solution.

Reading about the grid and typing a working layout are different skills. The only way to make class names like col-md-6, g-3 and d-flex feel natural is to build things from a written brief, get stuck, and then compare your result with a known-good one. This page gives you six exercises, ordered from layout to utilities to forms to components, and each one ends with a solution you can run and edit.

How to use it: read the brief, write your own answer in a scratch file or in the editor of the solution, and only then look at the solution. The checklist after each brief tells you what a correct answer must do, so you can mark yourself honestly. All solutions use Bootstrap 5.3.3 from the jsDelivr CDN, exactly as in the earlier lessons.

Do not copy the solution. Retype it from memory, then compare. The small differences (a missing mb-3, col-12 instead of col) are where the learning is.

Exercise 1: A responsive three-column layout

Brief. Build a page section with a title and three equal feature boxes. On a phone the boxes stack vertically. From the md breakpoint they sit side by side. Between the boxes there must be a gap of one spacing step (g-3), and the whole section is centered inside a container with vertical padding.

Checklist.

  • A container with py-4 (or similar).
  • A row with g-3 and three columns that switch at md.
  • Boxes that look like boxes: a background, padding and rounded corners from utilities, not custom CSS.

Revisit The Grid System and Responsive Columns if you get stuck.

Solution 1

Exercise 1: three responsive feature boxes

h-100 makes every box as tall as the tallest one in its row. Without it, the boxes would have different heights when their text lengths differ. Narrow the preview below 768 pixels to see the stack.

Exercise 2: Center something both ways

Brief. Show a single message card in the exact center of a box that is 240 pixels tall, both horizontally and vertically. The card has a border and padding. Use flex utilities only.

Checklist.

  • A parent with d-flex, justify-content-center and align-items-center.
  • A fixed height set with a utility or a small inline style (Bootstrap has no h-240 utility).
  • A child that does not stretch to the full width.

Solution 2

Exercise 2: centering with flex utilities

The parent is a flex container, so justify-content-center works on the main (horizontal) axis and align-items-center on the cross axis. See Flex Utilities for the full list.

Exercise 3: A spacing and typography fix-up

Brief. A teammate wrote a pricing teaser with no classes at all. Add utilities so that the price is large and bold, the label above it is small and muted, the button sits under the text with a one-step gap, and everything is center aligned.

Checklist.

  • Text alignment with text-center.
  • Typography with display-5 or fs-*, fw-bold, small, text-body-secondary.
  • Spacing with mb-* or mt-*, never with <br>.

Solution 3

Exercise 3: typography and spacing utilities

The mb-* scale runs from mb-0 (no margin) to mb-5 (3rem). Using mb-0 on the price removes the default paragraph margin so the two lines sit close together. Read Spacing Utilities for the scale and Typography for the type helpers.

Exercise 4: A validated sign-up form

Brief. Build a sign-up form with a name field, an email field and a checkbox "I accept the terms". All three are required. Add the novalidate attribute and a small script: on submit, stop the real submission, add was-validated to the form, and if the form is valid print the entered name in a paragraph below the form. Nothing may be posted anywhere.

Checklist.

  • needs-validation and novalidate on the form.
  • A label for every control and an invalid-feedback message for each.
  • event.preventDefault() and form.checkValidity() in the handler.

Look back at Form Layout and Validation if you need the pattern.

Solution 4

Exercise 4: client-side validation

Press the button with an empty form to see all three messages, then fill it in. The novalidate attribute turns off the browser's own bubbles so Bootstrap's styles take over, and aria-live="polite" makes screen readers announce the result.

Brief. Show three course cards. Each card has a title, a short description and a footer with a link. On phones there is one card per row, on md two, on lg three. All cards in a row must have equal height, and the whole card must be clickable through one link.

Checklist.

  • row row-cols-1 row-cols-md-2 row-cols-lg-3 g-3.
  • h-100 on each .card.
  • stretched-link on a single anchor per card, with position-relative handled by the card itself.

Solution 5

Exercise 5: responsive cards

.card already has position: relative, which is what stretched-link needs: the link's invisible ::after layer covers the nearest positioned ancestor, so the whole card becomes the click target. Hover anywhere on a card to see the link react. More in Cards.

Exercise 6: A navbar with a modal

Brief. Build a dark navbar that collapses below lg, with a brand, two links and a "Help" button on the right. Pressing "Help" opens a modal with a title, a short text and a Close button. Use only data-bs-* attributes, no custom JavaScript.

Checklist.

  • navbar navbar-expand-lg with a toggler (data-bs-toggle="collapse").
  • A modal with aria-labelledby pointing at its title.
  • The bundle script, because collapse and modal both need JavaScript.

Solution 6

Exercise 6: navbar and help modal

Make the preview narrow to see the toggler, then press Help. data-bs-theme="dark" on the navbar switches only that component to the dark palette, a topic of Color Modes and Dark Mode later. The two plugins used here are covered in The Navbar and Modals.

Where to go from here

Once all six feel easy, make them harder on your own: add a fourth column that only appears from xl, replace the centered box with a full-viewport hero, add a password-match rule to the sign-up form, or load the modal text from a button's data-bs-* attribute. The projects in this track combine these pieces into complete pages.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Three cards in a row should have equal height even when their text lengths differ. Which class do you put on each card?

    h-100 sets height to 100% of the column, and the columns in a row are already equal height, so the cards match.
  2. 2. What does the novalidate attribute on a form do when you use Bootstrap validation?

    novalidate disables the browser's default messages. Your script still calls checkValidity() and adds was-validated to display Bootstrap's feedback.
  3. 3. Which pair of utilities centers a child on both axes inside a flex container?

    justify-content-center works on the main axis and align-items-center on the cross axis of the d-flex parent.
  4. 4. Why does a stretched-link on a card make the entire card clickable?

    stretched-link uses an absolutely positioned ::after layer that fills the closest ancestor with position: relative; .card has that by default.

Summary

  • Practice from a brief, then compare with a solution; retype instead of copying.
  • Responsive grids combine row, g-* and col-* classes; row-cols-* and h-100 give equal cards.
  • Flex utilities center content; spacing and type utilities replace custom CSS.
  • Bootstrap validation needs novalidate, checkValidity() and was-validated, with no real submission in the examples.
  • Navbars and modals depend on the JavaScript bundle and data-bs-* attributes.

Next lesson: Bootstrap Interview Questions.