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.
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.
| Advantages | Disadvantages |
|---|---|
| a tidy, responsive site is made quickly | sites made only with Bootstrap look alike |
| works the same in all browsers | loads a lot of CSS you may not use |
| ready-made components: menu, cards, forms, dialogs | if 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.
<!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.
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:
| Breakpoint | Applies from the width | Device |
|---|---|---|
(no breakpoint) col-6 | 0 | all, including phones |
sm → col-sm-6 | 576px | a larger phone |
md → col-md-6 | 768px | a tablet |
lg → col-lg-4 | 992px | a laptop |
xl, xxl | 1200px, 1400px | a 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).
<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:
| Group | Examples | Meaning |
|---|---|---|
| spacing | m-3, mt-2, px-4, mb-0 | m = margin, p = padding; t/b/s/e/x/y = top/bottom/start/end/horizontal/vertical; 0–5 = size |
| text | text-center, fw-bold, fs-3, text-uppercase | alignment, weight, size, upper case |
| colours | text-primary, bg-success, bg-light, text-white | primary (blue), secondary, success (green), danger (red), warning, info, light, dark |
| borders | border, rounded, rounded-circle, shadow | border, rounding, shadow |
| flexbox | d-flex, justify-content-center, align-items-center, flex-column, gap-2 | the same as CSS flexbox |
| display | d-none, d-md-block | hide / show from a certain width |
| images | img-fluid | the image shrinks with the screen (max-width: 100%) |
<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.
Each task has an example, a hint and a solution. The solutions have been checked in the browser.
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.
col-*) must be directly inside a row, and the row inside a container.ms-2 instead of ml-2.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?