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.
a menu that is fully shown from md (768px), and collapsed below that
navbar-toggler + data-bs-toggle="collapse"
the "hamburger" button; data-bs-target says which part it opens (by id)
collapse navbar-collapse
the part that collapses
ms-auto
margin-start: auto – pushes the items to the right
dropdown + data-bs-toggle="dropdown"
a drop-down list
data-bs-theme="dark"
light letters for a dark background
Make the window narrower: the menu turns into a button with three lines. This is done by Bootstrap's JavaScript (bootstrap.bundle.min.js) – without it the menu does not open. The aria-* attributes help screen readers.
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.
a field with an add-on or a button in the same row
mb-3
spacing 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 4
Bootstrap 5
jQuery and Popper needed (3 scripts)
one script bootstrap.bundle.min.js, without jQuery
data-toggle, data-target, data-dismiss
data-bs-toggle, data-bs-target, data-bs-dismiss
ml-*, mr-*, pl-*, pr-*
ms-*, me-*, ps-*, pe-* (start/end)
float-left, text-left
float-start, text-start
form-group, custom-select
mb-3, form-select
no-gutters
g-0 (and g-1 … g-5)
maxcdn.bootstrapcdn.com/bootstrap/4.x
cdn.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).
Hint
Start from the template at the beginning of the previous lesson. Copy the navbar from the example and change the text; the cards in a row g-3 with col-md-4; the form in a container with style="max-width: 500px".
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)?
Podešavanja kolačića
Koristimo kolačiće da bismo vam pružili najbolje moguće iskustvo. Takođe nam omogućavaju da analiziramo ponašanje korisnika kako bismo stalno poboljšavali veb lokaciju za vas.