Bootstrap 5 Get Started
There are two common ways to add Bootstrap to a page: link it from a CDN (Content Delivery Network), or install it with a package manager and bundle it with your own code. This course uses the CDN because it needs nothing but an HTML file.
Option 1: the CDN
Add one <link> in the <head> for the CSS, and one <script> just before </body> for the JavaScript plugins:
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
...
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>| File | Contains | Needed when |
|---|---|---|
bootstrap.min.css |
All styles: grid, components, utilities | Always |
bootstrap.bundle.min.js |
All plugins plus Popper (positioning for dropdowns, tooltips, popovers) | You use interactive components |
bootstrap.min.js |
Plugins without Popper | You load Popper separately |
Tip: Always pin an exact version (
bootstrap@5.3.3) in production. An unpinned URL could silently pick up a new release and change your design.
The starter template
Every Bootstrap page should have three things: the HTML5 doctype, a charset meta tag and the viewport meta tag. Without the viewport tag, phones pretend to be ~980px wide and shrink the page, so responsive classes never kick in.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap starter</title>
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/css/bootstrap.min.css" rel="stylesheet">
</head>
<body>
<div class="container mt-3">
<h1>My first Bootstrap page</h1>
<p>Everything inside <code>.container</code> is centred and padded.</p>
<div class="alert alert-info">The CSS is loaded — this box is styled by Bootstrap.</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
</body>
</html>Checking that the JavaScript works
Some components only need CSS (buttons, cards, alerts). Others need the JavaScript bundle: dropdowns, collapse, modals, carousels, tooltips and so on. Here's a quick test — remove the <script> line and the button stops working.
<!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 class="btn btn-primary" data-bs-toggle="collapse" data-bs-target="#secret">
Click me
</button>
<div id="secret" class="collapse mt-3">
<div class="card card-body">🎉 The JavaScript bundle is working!</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.3/dist/js/bootstrap.bundle.min.js"></script>
<script>
console.log('Bootstrap version:', bootstrap.Collapse.VERSION);
</script>
</body>
</html>The bundle defines a global bootstrap object. Every plugin is a class on it (bootstrap.Modal, bootstrap.Tooltip, …) and has a VERSION property.
Option 2: npm and a bundler
In a real project built with a tool such as Vite you usually install Bootstrap as a dependency:
npm install bootstrap@5.3.3Then import it from your JavaScript entry file:
import 'bootstrap/dist/css/bootstrap.min.css';
import { Modal, Tooltip } from 'bootstrap';This lets you import only the plugins you use, and — if you compile Bootstrap's Sass sources — change its variables (colours, breakpoints, spacing) at build time.
Where to put things
| Item | Location | Why |
|---|---|---|
| Viewport meta tag | <head> |
Makes phones use their real width |
| Bootstrap CSS | <head>, before your own stylesheet |
Your rules can override Bootstrap's |
| Bootstrap JS | End of <body> |
HTML is parsed before the scripts run |
| Your own scripts | After Bootstrap JS | They can use the bootstrap global |
Note: Because Bootstrap's CSS loads first, a rule in your own stylesheet with equal specificity wins. That's the simplest way to customise without touching Bootstrap's files.
Test yourself
Why is the viewport meta tag important?
- It loads Bootstrap's JavaScript
- It makes mobile browsers use the device width so responsive classes work
- It sets the page title
- It enables dark mode
What does bootstrap.bundle.min.js include that bootstrap.min.js doesn't?
- jQuery
- The CSS
- Popper
- Sass
Which component works without Bootstrap's JavaScript?
- Modal
- Dropdown
- Card
- Carousel