← Learning Hub
Advanced

Customising Bootstrap and Dark Mode

Every site using plain Bootstrap looks the same. Make it yours with CSS variables, your own classes, or Sass.

A site built with plain Bootstrap is recognisable at a glance. Customising the colours, fonts and corner radius makes it look like your brand while keeping all the convenience. There are three levels: quick changes with CSS variables, dark mode with one attribute, and full control (including smaller files) by compiling Bootstrap's Sass yourself.

1. Change CSS variables

Bootstrap 5.3 uses CSS variables such as --bs-primary and --bs-border-radius. Many components also have their own (for example --bs-btn-bg).

Example
<!DOCTYPE html>
<html lang="en">
  <head>
    <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">
    <style>
      :root {
        --bs-primary: #7c3aed;
        --bs-primary-rgb: 124, 58, 237;
        --bs-border-radius: 0.75rem;
        --bs-body-font-family: system-ui, sans-serif;
      }
      .btn-brand {
        --bs-btn-bg: #7c3aed;
        --bs-btn-border-color: #7c3aed;
        --bs-btn-hover-bg: #6d28d9;
        --bs-btn-hover-border-color: #6d28d9;
        --bs-btn-color: #fff;
        --bs-btn-hover-color: #fff;
      }
    </style>
  </head>
  <body class="p-3">
    <button class="btn btn-brand">Brand button</button>
    <span class="badge rounded-pill text-bg-primary ms-2">Primary badge</span>
  </body>
</html>
▶ Try it yourself

2. Dark mode with colour modes

Put data-bs-theme="dark" on <html> (or on one part of the page) and Bootstrap switches to its dark colours.

Example
<!DOCTYPE html>
<html lang="en">
  <head>
    <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">
    <div class="card mb-3" style="max-width: 300px">
      <div class="card-body">Light card</div>
    </div>
    <div class="card" data-bs-theme="dark" style="max-width: 300px">
      <div class="card-body">Dark card</div>
    </div>
    <button class="btn btn-secondary mt-3" onclick="toggle()">Switch whole page</button>
    <script>
      function toggle() {
        const html = document.documentElement;
        html.dataset.bsTheme = html.dataset.bsTheme === "dark" ? "light" : "dark";
      }
    </script>
  </body>
</html>
▶ Try it yourself

3. Sass for full control

Install Bootstrap with npm and change its Sass variables before importing it. This builds a CSS file with your colours everywhere, and lets you leave out parts you don't use.

Example
// custom.scss
$primary: #7c3aed;
$border-radius: .75rem;
$font-family-sans-serif: system-ui, sans-serif;

@import "../node_modules/bootstrap/scss/bootstrap";
Example
npm install bootstrap sass
npx sass scss/custom.scss css/custom.css

Keep custom.scss in a scss folder next to node_modules, then link css/custom.css instead of Bootstrap's CSS file. Newer Sass versions may print warnings about @import; Bootstrap 5.3 still uses it, so the warnings are safe to ignore.

Bootstrap is now on version 5.3. If you ever see examples with jQuery or data-toggle (no bs-), they are for the old Bootstrap 4.
Key points
  • Override CSS variables like --bs-primary for quick theming.
  • data-bs-theme="dark" switches to dark mode.
  • Sass gives full control and lets you drop unused parts.

📝 Test yourself

1. Which attribute turns on Bootstrap's dark colours?
2. When must Sass variables like $primary be set?
3. Fill in the start of Bootstrap's CSS variable for the main colour:
---primary: #7c3aed;