Bootstrap Exercises
Time to practise. Each exercise gives you a starter playground with plain HTML — add Bootstrap classes (and, where needed, attributes) until the result matches the goal, then compare with the solution. You shouldn't need to write any custom CSS.
| # | Topic |
|---|---|
| 1 | Container and typography |
| 2 | Grid columns |
| 3 | Buttons and button group |
| 4 | Alert with close button |
| 5 | Card |
| 6 | Responsive navbar |
| 7 | Form with input group |
| 8 | Modal |
| 9 | Utilities and flex |
| 10 | Responsive visibility |
Exercise 1 — Container and typography
Put the content in a centred container with top margin 3. Make the heading a display-6 heading, the first paragraph a lead, and the last paragraph centred and muted (text-body-secondary).
<!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>
<div>
<h1>Welcome</h1>
<p>This is the introduction to my page.</p>
<p>Normal paragraph text.</p>
<p>— footer note —</p>
</div>
</body>
</html>Show solution
<div class="container mt-3">
<h1 class="display-6">Welcome</h1>
<p class="lead">This is the introduction to my page.</p>
<p>Normal paragraph text.</p>
<p class="text-center text-body-secondary">— footer note —</p>
</div>Exercise 2 — Grid columns
Turn the three boxes into a grid row: stacked on phones, and from the md breakpoint the first box is 6 columns wide and the other two 3 each. Add a gutter of 2 between the boxes.
<!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="py-2">
<div class="container">
<div>
<div><div class="p-3 text-bg-primary">Main</div></div>
<div><div class="p-3 text-bg-success">Side A</div></div>
<div><div class="p-3 text-bg-danger">Side B</div></div>
</div>
</div>
</body>
</html>Show solution
<div class="container">
<div class="row g-2">
<div class="col-md-6"><div class="p-3 text-bg-primary">Main</div></div>
<div class="col-md-3"><div class="p-3 text-bg-success">Side A</div></div>
<div class="col-md-3"><div class="p-3 text-bg-danger">Side B</div></div>
</div>
</div>Exercise 3 — Buttons and a button group
Style "Save" as a green button, "Delete" as a red outline button, and join the three view buttons into one button group of small, secondary outline buttons. Mark "Grid" as active.
<!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">
<button type="button">Save</button>
<button type="button">Delete</button>
<hr>
<div>
<button type="button">List</button>
<button type="button">Grid</button>
<button type="button">Table</button>
</div>
</body>
</html>Show solution
<button type="button" class="btn btn-success">Save</button>
<button type="button" class="btn btn-outline-danger">Delete</button>
<hr>
<div class="btn-group btn-group-sm" role="group" aria-label="View">
<button type="button" class="btn btn-outline-secondary">List</button>
<button type="button" class="btn btn-outline-secondary active" aria-pressed="true">Grid</button>
<button type="button" class="btn btn-outline-secondary">Table</button>
</div>Exercise 4 — Dismissible alert
Make the message a warning alert that can be closed with a × button and fades out. The Bootstrap JavaScript is already loaded.
<!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>
<strong>Heads up!</strong> Your session expires in 5 minutes.
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>Show solution
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<strong>Heads up!</strong> Your session expires in 5 minutes.
<button type="button" class="btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>Exercise 5 — Card
Build a card (max width 18rem) with a header "New", a body containing a title, a short text and a primary button, and a muted footer.
<!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>
<div>New</div>
<div>
<h5>Bootstrap course</h5>
<p>Learn the grid, components and utilities.</p>
<a href="#">Start</a>
</div>
<div>46 lessons</div>
</div>
</body>
</html>Show solution
<div class="card" style="max-width: 18rem;">
<div class="card-header">New</div>
<div class="card-body">
<h5 class="card-title">Bootstrap course</h5>
<p class="card-text">Learn the grid, components and utilities.</p>
<a href="#" class="btn btn-primary">Start</a>
</div>
<div class="card-footer text-body-secondary">46 lessons</div>
</div>Exercise 6 — Responsive navbar
Turn this into a dark navbar that shows its links in a row from sm up and collapses behind a hamburger button below that. The JavaScript is loaded.
<!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>
<nav>
<a href="#">MySite</a>
<ul>
<li><a href="#">Home</a></li>
<li><a href="#">About</a></li>
<li><a href="#">Contact</a></li>
</ul>
</nav>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>Show solution
<nav class="navbar navbar-expand-sm bg-dark" data-bs-theme="dark">
<div class="container-fluid">
<a class="navbar-brand" href="#">MySite</a>
<button class="navbar-toggler" type="button" data-bs-toggle="collapse" data-bs-target="#nav"
aria-controls="nav" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="nav">
<ul class="navbar-nav">
<li class="nav-item"><a class="nav-link active" aria-current="page" href="#">Home</a></li>
<li class="nav-item"><a class="nav-link" href="#">About</a></li>
<li class="nav-item"><a class="nav-link" href="#">Contact</a></li>
</ul>
</div>
</div>
</nav>Exercise 7 — Form with an input group
Style the form: labels with form-label, inputs with form-control, a $ add-on in front of the price, the select as a Bootstrap select, a "Publish" switch, and a primary submit button. Space the fields with mb-3.
<!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">
<form>
<label for="name">Product</label>
<input id="name">
<label for="price">Price</label>
<input id="price" type="number">
<label for="cat">Category</label>
<select id="cat"><option>Books</option><option>Games</option></select>
<input type="checkbox" id="pub"> <label for="pub">Publish</label>
<button type="submit">Save</button>
</form>
</body>
</html>Show solution
<form>
<div class="mb-3">
<label for="name" class="form-label">Product</label>
<input id="name" class="form-control">
</div>
<div class="mb-3">
<label for="price" class="form-label">Price</label>
<div class="input-group">
<span class="input-group-text">lt;/span>
<input id="price" type="number" class="form-control">
</div>
</div>
<div class="mb-3">
<label for="cat" class="form-label">Category</label>
<select id="cat" class="form-select"><option>Books</option><option>Games</option></select>
</div>
<div class="form-check form-switch mb-3">
<input class="form-check-input" type="checkbox" role="switch" id="pub">
<label class="form-check-label" for="pub">Publish</label>
</div>
<button type="submit" class="btn btn-primary">Save</button>
</form>Exercise 8 — Modal
Make the button open the modal. The modal needs a title, a close button, a short body and a "Got it" button in the footer that closes it. Keep it small (modal-sm).
<!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">
<button type="button" class="btn btn-primary">Show info</button>
<div id="info">
<h5>Did you know?</h5>
<p>Bootstrap was first released in 2011.</p>
<button type="button">Got it</button>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>Show solution
<button type="button" class="btn btn-primary" data-bs-toggle="modal" data-bs-target="#info">Show info</button>
<div class="modal fade" id="info" tabindex="-1" aria-labelledby="infoTitle" aria-hidden="true">
<div class="modal-dialog modal-sm">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="infoTitle">Did you know?</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">Bootstrap was first released in 2011.</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-bs-dismiss="modal">Got it</button>
</div>
</div>
</div>
</div>Exercise 9 — Utilities and flex
Using only utility classes: make the bar a flex row with items vertically centred, padding 2, a light tertiary background, a border and rounded corners. Push the two buttons to the right with an auto margin on the title, and add a gap of 2 between items.
<!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>
<strong>Inbox (3)</strong>
<button class="btn btn-sm btn-outline-secondary">Refresh</button>
<button class="btn btn-sm btn-primary">Compose</button>
</div>
</body>
</html>Show solution
<div class="d-flex align-items-center gap-2 p-2 bg-body-tertiary border rounded">
<strong class="me-auto">Inbox (3)</strong>
<button class="btn btn-sm btn-outline-secondary">Refresh</button>
<button class="btn btn-sm btn-primary">Compose</button>
</div>Exercise 10 — Responsive visibility
Show "📱 Mobile menu" only below md, and "🖥 Desktop menu" only from md up. Then make the grid show the four tiles 2 per row on phones and 4 per row from md, using row-cols-*.
<!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="p-2 border mb-2">📱 Mobile menu</div>
<div class="p-2 border mb-2">🖥 Desktop menu</div>
<div class="row g-2">
<div class="col"><div class="p-3 bg-primary-subtle text-center">1</div></div>
<div class="col"><div class="p-3 bg-primary-subtle text-center">2</div></div>
<div class="col"><div class="p-3 bg-primary-subtle text-center">3</div></div>
<div class="col"><div class="p-3 bg-primary-subtle text-center">4</div></div>
</div>
</body>
</html>Show solution
<div class="p-2 border mb-2 d-md-none">📱 Mobile menu</div>
<div class="p-2 border mb-2 d-none d-md-block">🖥 Desktop menu</div>
<div class="row row-cols-2 row-cols-md-4 g-2">
<div class="col"><div class="p-3 bg-primary-subtle text-center">1</div></div>
<div class="col"><div class="p-3 bg-primary-subtle text-center">2</div></div>
<div class="col"><div class="p-3 bg-primary-subtle text-center">3</div></div>
<div class="col"><div class="p-3 bg-primary-subtle text-center">4</div></div>
</div>Resize your browser window to check both versions — the preview frame follows the width of the lesson column.
Test yourself
Which three pieces does every Bootstrap modal need inside .modal?
- .modal-dialog > .modal-content > header/body/footer
- .modal-box > .modal-text
- .dialog > .content
- .modal-wrapper > .modal-inner
Which pair of classes shows an element only below the md breakpoint?
- .d-md-block
- .d-none .d-md-block
- .d-md-none
- .d-sm-none