Bootstrap 5: the grid and utility classes

Lesson 11 of 17 · HTML, CSS and JavaScript

Bootstrap is the best-known CSS library: ready-made classes for layout, spacing, colours, buttons and components. Instead of writing CSS for every little thing yourself, you add classes to the HTML. This lesson covers linking Bootstrap, the 12-column grid and the most useful utility classes.

Before you start: required prior knowledge – responsive design, flexbox.
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.

What Bootstrap is and when to use it

AdvantagesDisadvantages
a tidy, responsive site is made quicklysites made only with Bootstrap look alike
works the same in all browsersloads a lot of CSS you may not use
ready-made components: menu, cards, forms, dialogsif you do not know CSS, it is hard to customize

Bootstrap is made of CSS – the class d-flex is nothing more than display: flex. That is why the course first explained plain CSS (flexbox and grid, media queries), and Bootstrap is now a tool that speeds up the work. The current version is Bootstrap 5; unlike version 4, it does not need the jQuery library.

Linking it: a page template

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>My site</title>
  <!-- 1. Bootstrap CSS (from a CDN) -->
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css">
  <!-- 2. my CSS – AFTER Bootstrap, so that it can change it -->
  <link rel="stylesheet" href="css/main.css">
</head>
<body>
  <div class="container">
    <h1>Hello, Bootstrap!</h1>
  </div>
  <!-- 3. Bootstrap JavaScript (needed for the phone menu, drop-down lists, dialogs) -->
  <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>

A CDN (Content Delivery Network) is a network of servers from which the browser downloads the library – you do not have to download anything by hand. If the site has to work without the internet, you can download the files from getbootstrap.com and link them locally. The complete documentation, with examples for every class, is there too.

The grid: container, row, col

A layout in Bootstrap is made of three levels: container (a centred frame) → row → col (columns). A row has 12 parts, and a column says how many of them it takes:

<div class="container py-2">
  <div class="row g-2 text-center">
    <div class="col-12 col-md-6 col-lg-4"><div class="p-3 bg-primary text-white rounded">col-12 col-md-6 col-lg-4</div></div>
    <div class="col-12 col-md-6 col-lg-4"><div class="p-3 bg-success text-white rounded">col-12 col-md-6 col-lg-4</div></div>
    <div class="col-12 col-md-12 col-lg-4"><div class="p-3 bg-danger text-white rounded">col-12 col-md-12 col-lg-4</div></div>
  </div>
  <div class="row g-2 mt-1 text-center">
    <div class="col-8"><div class="p-2 bg-light border rounded">col-8</div></div>
    <div class="col-4"><div class="p-2 bg-light border rounded">col-4</div></div>
  </div>
</div>

Result:

BreakpointApplies from the widthDevice
(no breakpoint) col-60all, including phones
sm → col-sm-6576pxa larger phone
md → col-md-6768pxa tablet
lg → col-lg-4992pxa laptop
xl, xxl1200px, 1400pxa large screen

It reads like this: col-12 col-md-6 col-lg-4 = the full width on a phone (12/12), half from a tablet up (6/12), a third from a laptop up (4/12). It is the same "phone first" principle as in the lesson on responsive design – only without writing media queries. The class g-2 on the row sets the space between the columns (the gutter).

Utility classes

<div class="container py-3">
  <p class="text-center fw-bold text-primary fs-4">text-center fw-bold text-primary fs-4</p>
  <div class="p-3 mb-3 bg-warning-subtle border border-warning rounded-3 shadow-sm">
    p-3 mb-3 bg-warning-subtle border rounded-3 shadow-sm
  </div>
  <div class="d-flex justify-content-between align-items-center p-2 bg-dark text-white rounded">
    <span>d-flex justify-content-between</span>
    <span class="badge bg-danger">NEW</span>
  </div>
  <p class="mt-3 d-none d-md-block text-success">This line is visible only from 768px up (d-none d-md-block).</p>
  <p class="mt-3 d-md-none text-danger">This line is visible only on a narrow screen (d-md-none).</p>
</div>

Result:

GroupExamplesMeaning
spacingm-3, mt-2, px-4, mb-0m = margin, p = padding; t/b/s/e/x/y = top/bottom/start/end/horizontal/vertical; 0–5 = size
texttext-center, fw-bold, fs-3, text-uppercasealignment, weight, size, upper case
colourstext-primary, bg-success, bg-light, text-whiteprimary (blue), secondary, success (green), danger (red), warning, info, light, dark
bordersborder, rounded, rounded-circle, shadowborder, rounding, shadow
flexboxd-flex, justify-content-center, align-items-center, flex-column, gap-2the same as CSS flexbox
displayd-none, d-md-blockhide / show from a certain width
imagesimg-fluidthe image shrinks with the screen (max-width: 100%)

Buttons

<div class="p-3 d-flex flex-wrap gap-2">
  <button class="btn btn-primary">Primary</button>
  <button class="btn btn-success">Success</button>
  <button class="btn btn-danger">Danger</button>
  <button class="btn btn-outline-primary">Outline</button>
  <a class="btn btn-dark btn-lg" href="#">A link as a button</a>
  <button class="btn btn-secondary btn-sm" disabled>Disabled</button>
</div>

Result:

The class btn gives the basic shape, and a second class the colour. The same class also works on <a> – the link looks like a button.

Practice tasks

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

TASK 01Difficulty: easy

Three subjects in columns

Make a page with a heading and three columns (Maths, Computer science, Physics), each with a paragraph and a button. On a phone the columns are one below the other, on a tablet two in a row, on a computer all three.

Common mistakes and tips
  • Link your own CSS file after Bootstrap, otherwise Bootstrap overrides your rules.
  • Columns (col-*) must be directly inside a row, and the row inside a container.
  • The columns in a row add up to 12; if they add up to more, the extra ones move to a new row.
  • Do not mix classes from Bootstrap 4 and 5 – e.g. version 5 uses ms-2 instead of ml-2.

Check your knowledge

Open the quiz

The Bootstrap grid

1. Into how many parts is a row divided in the Bootstrap grid?




2. What does col-12 col-md-6 mean?




3. Which class adds inner spacing on all sides?




4. Why is your own CSS linked after Bootstrap?