Chapter 8: Bootstrap with Svelte

Pairing Bootstrap's grid and design tokens with Svelte's reactive templates gives you consistent layout primitives while keeping interactive state firmly under Svelte's control.

Installing Bootstrap

Install Bootstrap as a project dependency so Vite bundles and versions its CSS alongside your app rather than relying on external CDN availability:

npm install bootstrap

Import the compiled stylesheet once in your application entry point (such as src/main.js). Avoid importing Bootstrap's imperative DOM JavaScript bundle when Svelte manages component state—instead, toggle visibility, tabs, and alerts with Svelte {#if} blocks and class: directives so Svelte never conflicts with external DOM mutations:

// src/main.js
import 'bootstrap/dist/css/bootstrap.min.css';
import App from './App.svelte';

export default new App({ target: document.getElementById('app') });

Using Bootstrap Components

Wrap repeated Bootstrap markup—like dismissible alerts, cards, or action buttons—inside thin Svelte components so feature pages pass clean semantic props instead of duplicating utility classes everywhere:

<script>
  let showAlert = true;
</script>

{#if showAlert}
  <div class="alert alert-info d-flex justify-content-between align-items-center" role="alert">
    <span>Your workspace settings were saved.</span>
    <button
      type="button"
      class="btn-close"
      aria-label="Close"
      on:click={() => showAlert = false}
    ></button>
  </div>
{/if}

<div class="d-flex gap-2">
  <button type="button" class="btn btn-primary">Save</button>
  <button type="button" class="btn btn-outline-secondary">Cancel</button>
</div>

Responsive Layouts

Use Bootstrap's 12-column responsive grid (.container, .row, .col-md-*) for page-level structure while reserving Svelte's scoped <style> blocks for component-specific polish:

<div class="container mt-4">
  <div class="row g-3">
    <div class="col-12 col-md-6">
      <div class="card p-3">
        <h2 class="h5">Summary</h2>
        <p class="mb-0">Full width on mobile, 6 columns on medium screens and up.</p>
      </div>
    </div>
    <div class="col-12 col-md-6">
      <div class="card p-3">
        <h2 class="h5">Activity</h2>
        <p class="mb-0">Stacks cleanly below on narrow viewports.</p>
      </div>
    </div>
  </div>
</div>

Form Components

Bootstrap's .form-label, .form-control, and .is-invalid classes pair naturally with Svelte's bind:value and reactive validation state:

<script>
  let formData = { email: '', password: '' };
  let submitted = false;

  $: emailInvalid = submitted && !formData.email.includes('@');

  function handleSubmit() {
    submitted = true;
    if (!emailInvalid) {
      console.log('Submitting:', formData.email);
    }
  }
</script>

<form on:submit|preventDefault={handleSubmit} novalidate>
  <div class="mb-3">
    <label class="form-label" for="bs-email">Email</label>
    <input
      type="email"
      id="bs-email"
      class="form-control"
      class:is-invalid={emailInvalid}
      bind:value={formData.email}
    />
    {#if emailInvalid}
      <div class="invalid-feedback">Enter a valid email address.</div>
    {/if}
  </div>
  <button type="submit" class="btn btn-primary">Sign in</button>
</form>

Practice: Build a two-column settings page using Bootstrap's grid, a dismissible Svelte alert banner, and a validated Bootstrap form with class:is-invalid feedback.