Table of Contents
What you will read?
- 1 Create a minimal HTML file
- 2 Choose a Bootstrap version
- 3 Link Bootstrap CSS via CDN (fastest)
- 4 Load the Bootstrap JS bundle
- 5 Verify with a simple component
- 6 Self-host with npm (offline and controlled)
- 7 Customize with Sass (optional)
- 8 Add SRI and tighten CSP
- 9 Optimize loading and performance
- 10 Troubleshoot common issues
Connecting Bootstrap to HTML lets you ship responsive, mobile-first layouts fast. You can link via CDN for speed or self-host for control. Steps below cover both methods, quick verification, security (SRI, CSP), and performance tweaks. Ideal for semi-technical users comfortable with terminals, npm, and basic HTML editing.
Create a minimal HTML file
Set up a clean HTML document that will load Bootstrap CSS in the head and Bootstrap JS before the closing body tag.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Bootstrap Connect Test</title>
<!-- Bootstrap CSS will be added in the next step -->
</head>
<body>
<div class="container py-4">
<h1 class="h3">Bootstrap Connection</h1>
<!-- Content will go here -->
</div>
<!-- Bootstrap JS will be added later -->
</body>
</html>
Choose a Bootstrap version
Use Bootstrap 5 for modern projects. Confirm the latest 5.x version with npm if desired.
npm show bootstrap version
Result is a version like 5.x.y. CDN “@5” tracks the latest 5.x safely.
Link Bootstrap CSS via CDN (fastest)
Add the CSS link in the head. This immediately styles your HTML with Bootstrap’s utilities and components.
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5/dist/css/bootstrap.min.css" rel="stylesheet">
Load the Bootstrap JS bundle
Add the JS bundle before the closing body tag. The bundle includes Popper, required by tooltips, dropdowns, and more.
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5/dist/js/bootstrap.bundle.min.js"></script>
Verify with a simple component
Insert a button and alert. If Bootstrap is connected, they render with proper styles and spacing.
<div class="container py-4">
<button class="btn btn-primary me-2">Primary Button</button>
<div class="alert alert-success d-inline-block m-0" role="alert">Bootstrap loaded successfully!</div>
</div>
Expected output: A blue button followed by a green success alert with Bootstrap spacing.
Self-host with npm (offline and controlled)
Install Bootstrap locally to avoid CDN dependencies and to pin versions.
mkdir bootstrap-project && cd bootstrap-project
npm init -y
npm install bootstrap
Reference files directly from node_modules for quick tests, or copy them into your assets for production.
<!-- quick local reference (development only) -->
<link rel="stylesheet" href="node_modules/bootstrap/dist/css/bootstrap.min.css">
<script src="node_modules/bootstrap/dist/js/bootstrap.bundle.min.js"></script>
mkdir -p assets/css assets/js
cp node_modules/bootstrap/dist/css/bootstrap.min.css assets/css/
cp node_modules/bootstrap/dist/js/bootstrap.bundle.min.js assets/js/
<!-- production-friendly local assets -->
<link rel="stylesheet" href="assets/css/bootstrap.min.css">
<script src="assets/js/bootstrap.bundle.min.js"></script>
Customize with Sass (optional)
Override Bootstrap variables and compile your own theme.
npm install -D sass
mkdir -p scss
printf '%s\n' '$primary: #2f6feb;' '@import "node_modules/bootstrap/scss/bootstrap";' > scss/custom.scss
npx sass scss/custom.scss assets/css/custom.min.css --style=compressed
<link rel="stylesheet" href="assets/css/custom.min.css">
Add SRI and tighten CSP
Protect CDN resources with Subresource Integrity and a CSP. Compute hashes locally and add integrity+crossorigin attributes.
curl -sL https://cdn.jsdelivr.net/npm/bootstrap@5/dist/css/bootstrap.min.css | openssl dgst -sha384 -binary | openssl base64 -A
curl -sL https://cdn.jsdelivr.net/npm/bootstrap@5/dist/js/bootstrap.bundle.min.js | openssl dgst -sha384 -binary | openssl base64 -A
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-HASH_GOES_HERE" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5/dist/js/bootstrap.bundle.min.js" integrity="sha384-HASH_GOES_HERE" crossorigin="anonymous"></script>
<meta http-equiv="Content-Security-Policy" content="default-src 'self'; style-src 'self' https://cdn.jsdelivr.net 'unsafe-inline'; script-src 'self' https://cdn.jsdelivr.net; font-src 'self' https://cdn.jsdelivr.net; img-src 'self' data:;">
Optimize loading and performance
Reduce connection latency and keep scripts non-blocking.
<link rel="preconnect" href="https://cdn.jsdelivr.net" crossorigin>
<!-- Option A: keep JS at the end of body (recommended) -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5/dist/js/bootstrap.bundle.min.js"></script>
<!-- Option B: move to head with defer -->
<script defer src="https://cdn.jsdelivr.net/npm/bootstrap@5/dist/js/bootstrap.bundle.min.js"></script>
Troubleshoot common issues
Check for 200 responses, console errors, and mixed content blocks.
curl -I https://cdn.jsdelivr.net/npm/bootstrap@5/dist/css/bootstrap.min.css
# Expect: HTTP/2 200
# If styles don't apply:
# - Ensure the CSS link is above your HTML content
# - Ensure no proxy/extension blocks cdn.jsdelivr.net
# - Ensure HTTPS on all resources
For more guides and support, and to buy servers with reliable assistance, use dropvps and visit dropvps.com
