How to use Bootstrap with a static site generator
To integrate Bootstrap with a static site generator, choose between using a CDN for simplicity or installing via npm for customization and performance. 2. Add Bootstrap’s CSS and JS links in your SSG’s layout template—place the CDN links in the head and before the closing body tag, or import Bootstrap in your SCSS/JS files if using npm with a build process. 3. Use Bootstrap classes directly in Markdown or template files, as SSGs convert Markdown to HTML where Bootstrap styling applies normally. 4. Optionally customize Bootstrap by overriding Sass variables (like $primary) or importing only specific components when using npm. 5. Optimize performance by importing only needed modules, minifying assets, and using tools like PurgeCSS with a safelist to remove unused classes during build. The best method depends on your need for speed of setup versus control over customization, and both CDN and npm approaches work seamlessly with SSGs like Jekyll, Hugo, or Eleventy.

Using Bootstrap with a static site generator (SSG) is a common way to build fast, responsive websites with minimal setup. Whether you're using Jekyll, Hugo, Eleventy, or another SSG, integrating Bootstrap gives you access to its pre-built components and grid system without needing to manage a full frontend framework. Here's how to do it effectively.

1. Choose How to Include Bootstrap
You have two main options: using a CDN or installing Bootstrap via package manager.
Option A: Use the CDN (Simplest)
Add Bootstrap’s CSS and JS links directly in your site’s HTML layout (usually in head.html or base.html):

<!-- In your <head> --> <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/css/bootstrap.min.css" rel="stylesheet"> <!-- Before closing </body> --> <script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.2/dist/js/bootstrap.bundle.min.js"></script>
✅ Pros:
- No build steps or dependencies
- Fast and easy to set up
❌ Cons:

- Less control over customization
- Slight performance delay (depends on CDN)
Option B: Install via npm (More Control)
If your SSG supports a build process (like Eleventy or Hugo with asset pipelines), install Bootstrap:
npm install bootstrap
Then import it in your main SCSS/JS file:
// styles.scss @import "bootstrap/scss/bootstrap";
Or in JavaScript (if bundling):
// main.js import 'bootstrap';
Use a tool like Sass, PostCSS, or Webpack to compile and bundle your assets.
✅ Pros:
- Full customization (override variables, include only needed components)
- Better performance (bundled with other assets)
❌ Cons:
- Requires a build setup
2. Set Up Your SSG Layouts
Most static site generators use layout templates. Add Bootstrap’s CSS and JS references in the appropriate layout file.
For example, in Jekyll, edit _layouts/default.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>{{ page.title }}</title>
<link href="/assets/css/styles.css" rel="stylesheet"> <!-- compiled Bootstrap -->
</head>
<body>
{{ content }}
<script src="/assets/js/bundle.js"></script> <!-- includes Bootstrap JS -->
</body>
</html>In Hugo, use a base template in layouts/_default/baseof.html.
Make sure paths match your asset output directories.
3. Use Bootstrap Components in Your Content
Once Bootstrap is loaded, you can use its classes directly in Markdown or template files.
For example, in a Markdown file (Jekyll or Eleventy):
---
title: Home
layout: default
---
<div class="container">
<div class="row">
<div class="col-md-8">
<h1>Welcome</h1>
<p class="lead">This site uses Bootstrap!</p>
</div>
<div class="col-md-4">
<div class="card">
<div class="card-body">
<h5>Quick Info</h5>
<p>Styled with Bootstrap components.</p>
</div>
</div>
</div>
</div>
</div>This works because most SSGs process Markdown into HTML, and Bootstrap classes apply normally.
4. Customize Bootstrap (Optional)
If you installed via npm and use Sass, you can customize Bootstrap before compiling.
Create a custom main.scss:
// Override variables $primary: #007cba; $enable-rounded: false; // Import Bootstrap @import "bootstrap/scss/bootstrap";
This lets you change colors, disable features, or tweak spacing — all while keeping Bootstrap’s structure.
Bonus: Optimize for Performance
- Only import Bootstrap modules you need (e.g., not carousel if unused)
- Minify CSS/JS in production
- Consider using PurgeCSS (or
safelistin newer versions) to remove unused classes
For example, in a build tool config:
// purgecss example
new PurgeCSSPlugin({
paths: glob.sync('./_site/**/*.{html,js}', { nodir: true }),
safelist: ['fade'] // keep classes used dynamically
})Basically, it's not complicated — just include Bootstrap in your layout and start using its classes. The method you choose depends on whether you want simplicity (CDN) or control (npm build). Most static site generators work seamlessly with either approach.
The above is the detailed content of How to use Bootstrap with a static site generator. For more information, please follow other related articles on the PHP Chinese website!
Hot AI Tools
Undress AI Tool
Undress images for free
AI Clothes Remover
Online AI tool for removing clothes from photos.
Undresser.AI Undress
AI-powered app for creating realistic nude photos
ArtGPT
AI image generator for creative art from text prompts.
Stock Market GPT
AI powered investment research for smarter decisions
Hot Article
Popular tool
Notepad++7.3.1
Easy-to-use and free code editor
SublimeText3 Chinese version
Chinese version, very easy to use
Zend Studio 13.0.1
Powerful PHP integrated development environment
Dreamweaver CS6
Visual web development tools
SublimeText3 Mac version
God-level code editing software (SublimeText3)
Hot Topics
20521
7
13633
4
How to ensure your Bootstrap site is fully accessible (WCAG)? (Accessibility)
Mar 08, 2026 am 01:25 AM
The Bootstrap component does not automatically add the aria attributes required for complete WCAG. You need to manually complete aria-expanded, role="dialog", aria-labelledby, etc.; the form must be equipped with an explicit label; :focus needs to use :focus-visible to ensure that the keyboard focus is visible; the color contrast must reach 4.5:1.
How to animate Bootstrap components for a more dynamic feel? (Advanced CSS)
Mar 13, 2026 am 01:13 AM
The fade and show classes of Bootstrap5 are only status markers and do not contain animation logic. You need to manually add @keyframes or transitions to achieve fade-in and fade-out; custom components need to configure additional animation rules, and when JS controls the display, the show class must be added with delay to ensure redrawing.
How to create a professional-looking landing page with Bootstrap? (Project-Based)
Mar 10, 2026 am 12:22 AM
Bootstrap’sdefaultstylingfeelsgenericduetouncustomizedspacing,typography,andcolor—fixableviaCSSvariablesorSass,notdirectedits;avoidfixedwidths,misuseofgridclasses,andrender-blockingassetstoensureresponsivenessandfastLCP.
How to integrate Bootstrap with a JavaScript framework like React or Vue? (Integration)
Mar 12, 2026 am 01:22 AM
Only BootstrapCSS should be introduced in React/Vue instead of JS to avoid global style pollution and jQuery dependency; SCSS modules should be imported on demand to reduce the size; interactive functions must be imported with encapsulation libraries (such as react-bootstrap) or ESM; grid classes need to adapt to JSX/Vue syntax, and explicit and implicit classes should be used with SSR with caution.
How to implement a carousel or image slider with Bootstrap? (Content Display)
Mar 07, 2026 am 01:01 AM
Need not. Bootstrap5's carousel is automatically initialized by default. You need to manually call newbootstrap.Carousel() only when data-bs-ride is modified or the element is dynamically inserted.
How to properly install and set up Bootstrap 5 in your web project? (Getting Started)
Mar 16, 2026 am 12:40 AM
To correctly introduce Bootstrap5, you need to load bootstrap.min.css first, then load bootstrap.bundle.min.js (including Popper), and ensure that the viewport tag exists; after npm installation, you need to manually introduce compiled files in the dist directory. SCSS customization must be preceded by variable declarations, and JS components must be explicitly initialized to avoid jQuery contamination.
How to use Bootstrap utility classes to speed up your workflow? (Efficiency)
Mar 11, 2026 am 12:18 AM
Use mt-3 to represent the top margin, mb-3 to represent the bottom margin, and the suffixes t/b/s/e correspond to top/bottom/start/end respectively; ms-auto implements right alignment in the flex container, me-2 replaces the abandoned mr-2, and responsive classes such as mt-md-4 need to have breakpoint prefixes.
How to use Bootstrap variables to maintain a consistent color scheme? (Branding)
Mar 14, 2026 am 12:59 AM
The safest way is to redefine Bootstrap's Sass variables (such as $primary, $primary-rgb, $primary-text-emphasis) in advance and introduce them before @import "bootstrap/scss/variables" to ensure that all dependent variables are updated synchronously to avoid compilation failure or style exceptions.





