Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 58 ยท Getting Started

Installing Bootstrap

Add Bootstrap 5.3 to a page with CDN links, understand the bundle versus separate Popper, set up npm and Sass, and use the official starter template.

Before you can use a single Bootstrap class, the browser has to load Bootstrap's files. There are three common ways: link to a CDN, download the files and host them yourself, or install the package with npm and compile it with Sass. This lesson shows each one, explains the small details that trip up beginners (the viewport tag, the doctype, the bundle file) and ends with a starter page you can copy.

If you are new to HTML documents, the HTML tutorial explains the basic page structure used below.

Option 1: the CDN (fastest start)

A CDN, or content delivery network, serves popular files from servers around the world. jsDelivr hosts every Bootstrap release. You paste a stylesheet link into <head> and a script tag before </body>:

HTML
<link
  rel="stylesheet"
  href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
  integrity="sha384-QWTKZyjpPEjISv5WaRU9OFeRpok6YctnYmDr5pNlyT2bRjXh0JMhjY6hW+ALEwIH"
  crossorigin="anonymous">

<script
  src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"
  integrity="sha384-YvpcrYf0tY3lHB60NNkmXc5s9fDVZLESaAA55NDzOxhy9GkcIdslK1eN7N6jIeHz"
  crossorigin="anonymous"></script>

Three details matter:

  • The pinned version. [email protected] fixes the release. Never use a link without a version (such as bootstrap@5) in production, because a future update could change your layout.
  • integrity (SRI). Subresource Integrity is a hash of the file. The browser downloads the file, computes the hash and refuses to use it if it differs. That protects you if a CDN were ever compromised. The crossorigin="anonymous" attribute is required for the check to work. The examples in this tutorial leave integrity out to stay short, but real sites should include it.
  • .min. The minified files are smaller and faster. Use the readable, unminified files only while debugging.

The bundle versus separate Popper

Bootstrap ships two JavaScript builds:

File Contains Use it when
bootstrap.bundle.min.js Bootstrap plus Popper You want dropdowns, tooltips or popovers (the easy default)
bootstrap.min.js Bootstrap only You load Popper yourself, or you use no positioned components

Popper is a small library that positions floating elements (dropdown menus, tooltips, popovers) next to their trigger and keeps them on screen. The bundle already includes it, so one script tag is enough. If you chose bootstrap.min.js and also use a dropdown, you must add Popper before it:

HTML
<script src="https://cdn.jsdelivr.net/npm/@popperjs/[email protected]/dist/umd/popper.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.min.js"></script>

For almost every beginner project, use the bundle and move on. If your page only uses layout, typography and utilities, you can skip the script entirely, because those are pure CSS.

Two lines that are easy to forget

Bootstrap assumes a modern, standards-mode page. Two things in the HTML must be right or the page will behave oddly:

  1. <!doctype html> at the very top. Without it browsers use "quirks mode", where box sizes and other rules differ, and Bootstrap's styles can break in strange ways.
  2. The viewport meta tag in <head>:
HTML
<meta name="viewport" content="width=device-width, initial-scale=1">

Phones pretend to be a wide desktop screen (around 980 pixels) unless you tell them otherwise. This tag says "use the real device width". Without it, Bootstrap's responsive breakpoints (see Breakpoints) never trigger on a phone and your site looks like a zoomed-out desktop page.

The starter template

Put the pieces together and you get the page every project begins with. Press Run; the page shows a heading and a styled button, which proves the stylesheet loaded.

The Bootstrap starter template

Notice that the script is the last thing before </body>. The page content appears first, and the JavaScript loads afterwards, so visitors see the page sooner.

A quick test that the script loaded

Because the bundle is separate from the stylesheet, you can have working styles and broken behavior (or the other way round). A simple check is to read the global bootstrap object. This example shows the version string from the loaded script on the page itself.

Check that Bootstrap's JavaScript loaded

Option 2: download the files

On the official download page you can get the compiled CSS and JS as a ZIP file. Copy css/bootstrap.min.css and js/bootstrap.bundle.min.js into your project and link to them with relative paths such as href="css/bootstrap.min.css". This works offline and avoids a third-party request, at the cost of updating the files yourself.

Option 3: npm and Sass (read-only overview)

Larger projects install Bootstrap as a package. This needs Node.js on your computer, so it cannot run in the live editor. The commands below are for your own machine:

Terminal
npm init -y
npm install bootstrap@5.3.3
npm install --save-dev sass

The package puts the source in node_modules/bootstrap. You then write your own Sass file that imports Bootstrap and compile it to CSS:

SCSS
// scss/custom.scss
$primary: #6f42c1;          // change a variable BEFORE importing
@import "../node_modules/bootstrap/scss/bootstrap";
Terminal
npx sass scss/custom.scss css/custom.css

The big win is that changing $primary recolors every button, link and badge consistently. Customizing with Sass covers this in depth, and Customizing with CSS Variables shows a lighter alternative that needs no compiler. Bundlers and modern JavaScript projects can also import the plugins as modules; see Bootstrap JavaScript Plugins.

flowchart LR
  A["Choose a method"] --> B["CDN links"]
  A --> C["Download ZIP"]
  A --> D["npm install"]
  B --> E["Start building"]
  C --> E
  D --> F["Compile with Sass"] --> E

All three methods end with the same compiled CSS and JavaScript in the browser.

Starting out? Use the CDN. Switch to npm and Sass when you want to change theme colors, drop unused components or build a production bundle.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which file should you load if you want dropdowns and tooltips with the fewest script tags?

    The bundle contains Bootstrap's plugins plus Popper, which positions dropdowns, tooltips and popovers. One tag is enough.
  2. 2. What happens if you forget the viewport meta tag?

    Without width=device-width a mobile browser uses a virtual width of about 980 pixels, so the small-screen layout never applies.
  3. 3. What does the integrity attribute on a CDN link do?

    Subresource Integrity compares the downloaded file with a known hash. If the file was altered, the browser blocks it.
  4. 4. Where should the Bootstrap script tag normally go?

    Placing it at the end lets the visible page load first, and the elements the script works with already exist.

Summary

  • The quickest setup is two CDN links with a pinned version (5.3.3); add integrity and crossorigin for production.
  • bootstrap.bundle.min.js includes Popper; bootstrap.min.js does not.
  • Always start with <!doctype html> and the viewport meta tag.
  • Layout and utilities need only the CSS; interactive components need the script.
  • npm plus Sass gives full customization and is shown here read-only.

Next: Your First Bootstrap Page.