Styles That Apply Everywhere

Scoped styles are great for components, but some styles need to be global: typography, color variables, reset styles, and base element styling. SvelteKit provides several ways to add global CSS.


What You’ll Learn

  • Add global stylesheets to SvelteKit
  • Create CSS custom properties (variables)
  • Set up base typography
  • Use the :global() modifier

Adding a Global Stylesheet

Create a CSS file and import it in your layout:

/* filename: src/app.css */
/* Reset and base styles */
*,
*::before,
*::after {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  line-height: 1.6;
  color: #1f2937;
  background: #f9fafb;
}

h1, h2, h3, h4, h5, h6 {
  margin: 0 0 0.5em 0;
  line-height: 1.3;
}

p {
  margin: 0 0 1em 0;
}

a {
  color: #3b82f6;
}

img {
  max-width: 100%;
  height: auto;
}

Import it in your root layout:

<!-- filename: src/routes/+layout.svelte -->
<script>
  import '../app.css';
  
  let { children } = $props();
</script>

{@render children()}

Now these styles apply to every page.


CSS Custom Properties

Define design tokens as CSS variables for consistency:

/* filename: src/app.css */
:root {
  /* Colors */
  --color-primary: #3b82f6;
  --color-primary-dark: #2563eb;
  --color-primary-light: #eff6ff;
  
  --color-success: #10b981;
  --color-warning: #f59e0b;
  --color-error: #ef4444;
  
  --color-gray-50: #f9fafb;
  --color-gray-100: #f3f4f6;
  --color-gray-200: #e5e7eb;
  --color-gray-300: #d1d5db;
  --color-gray-400: #9ca3af;
  --color-gray-500: #6b7280;
  --color-gray-600: #4b5563;
  --color-gray-700: #374151;
  --color-gray-800: #1f2937;
  --color-gray-900: #111827;
  
  /* Typography */
  --font-sans: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, Menlo, Monaco, monospace;
  
  /* Spacing */
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-6: 1.5rem;
  --space-8: 2rem;
  
  /* Border radius */
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  --radius-full: 9999px;
  
  /* Shadows */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.1);
}

Use them in components:

<style>
  .card {
    background: white;
    border: 1px solid var(--color-gray-200);
    border-radius: var(--radius-lg);
    padding: var(--space-6);
  }
  
  .btn-primary {
    background: var(--color-primary);
    color: white;
  }
  
  .btn-primary:hover {
    background: var(--color-primary-dark);
  }
</style>

Benefits:

  • Change colors in one place
  • Consistent spacing throughout
  • Easy theming (dark mode later)

Typography Scale

Set up a consistent type scale:

/* filename: src/app.css */
body {
  font-size: 16px;
}

h1 {
  font-size: 2.25rem;   /* 36px */
  font-weight: 700;
}

h2 {
  font-size: 1.875rem;  /* 30px */
  font-weight: 600;
}

h3 {
  font-size: 1.5rem;    /* 24px */
  font-weight: 600;
}

h4 {
  font-size: 1.25rem;   /* 20px */
  font-weight: 600;
}

h5 {
  font-size: 1.125rem;  /* 18px */
  font-weight: 500;
}

h6 {
  font-size: 1rem;      /* 16px */
  font-weight: 500;
}

.text-sm {
  font-size: 0.875rem;  /* 14px */
}

.text-xs {
  font-size: 0.75rem;   /* 12px */
}

.text-lg {
  font-size: 1.125rem;  /* 18px */
}

.text-xl {
  font-size: 1.25rem;   /* 20px */
}

Utility Classes

Add commonly needed utilities:

/* filename: src/app.css */

/* Layout */
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 var(--space-4);
}

/* Text alignment */
.text-center { text-align: center; }
.text-left { text-align: left; }
.text-right { text-align: right; }

/* Font weight */
.font-normal { font-weight: 400; }
.font-medium { font-weight: 500; }
.font-semibold { font-weight: 600; }
.font-bold { font-weight: 700; }

/* Colors */
.text-primary { color: var(--color-primary); }
.text-muted { color: var(--color-gray-500); }
.text-error { color: var(--color-error); }

/* Visibility */
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

Use them in markup:

<p class="text-muted text-sm">Last updated: January 2025</p>
<h2 class="text-center">Our Services</h2>
<span class="sr-only">Opens in new window</span>

The :global() Modifier

Sometimes you need global styles within a component. Use :global():

<style>
  /* Scoped - only affects this component */
  .card {
    padding: 1rem;
  }
  
  /* Global - affects all elements */
  :global(body.modal-open) {
    overflow: hidden;
  }
  
  /* Nested global - child elements of this component */
  .content :global(a) {
    color: var(--color-primary);
  }
  
  /* Global modifier on class */
  :global(.tooltip) {
    position: absolute;
    z-index: 1000;
  }
</style>

Use sparingly — scoped styles are usually better.


Complete app.css

Here’s a comprehensive base stylesheet:

/* filename: src/app.css */

/* ===== CSS Variables ===== */
:root {
  /* Colors */
  --color-primary: #3b82f6;
  --color-primary-dark: #2563eb;
  --color-primary-light: #eff6ff;
  
  --color-success: #10b981;
  --color-warning: #f59e0b;
  --color-error: #ef4444;
  
  --color-gray-50: #f9fafb;
  --color-gray-100: #f3f4f6;
  --color-gray-200: #e5e7eb;
  --color-gray-300: #d1d5db;
  --color-gray-400: #9ca3af;
  --color-gray-500: #6b7280;
  --color-gray-600: #4b5563;
  --color-gray-700: #374151;
  --color-gray-800: #1f2937;
  --color-gray-900: #111827;
  
  /* Typography */
  --font-sans: system-ui, -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  
  /* Spacing */
  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-6: 1.5rem;
  --space-8: 2rem;
  --space-12: 3rem;
  
  /* Border radius */
  --radius-sm: 4px;
  --radius-md: 8px;
  --radius-lg: 12px;
  
  /* Shadows */
  --shadow-sm: 0 1px 2px rgba(0, 0, 0, 0.05);
  --shadow-md: 0 4px 6px rgba(0, 0, 0, 0.1);
  --shadow-lg: 0 10px 15px rgba(0, 0, 0, 0.1);
}

/* ===== Reset ===== */
*,
*::before,
*::after {
  box-sizing: border-box;
}

* {
  margin: 0;
}

html {
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

body {
  font-family: var(--font-sans);
  font-size: 1rem;
  line-height: 1.6;
  color: var(--color-gray-800);
  background: var(--color-gray-50);
}

img, picture, video, canvas, svg {
  display: block;
  max-width: 100%;
}

input, button, textarea, select {
  font: inherit;
}

/* ===== Typography ===== */
h1, h2, h3, h4, h5, h6 {
  line-height: 1.3;
  color: var(--color-gray-900);
}

h1 { font-size: 2.25rem; font-weight: 700; }
h2 { font-size: 1.875rem; font-weight: 600; }
h3 { font-size: 1.5rem; font-weight: 600; }
h4 { font-size: 1.25rem; font-weight: 600; }

p {
  margin-bottom: 1rem;
}

a {
  color: var(--color-primary);
  text-decoration: none;
}

a:hover {
  text-decoration: underline;
}

/* ===== Layout ===== */
.container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 var(--space-4);
}

.page {
  padding: var(--space-8) 0;
}

/* ===== Utilities ===== */
.text-center { text-align: center; }
.text-muted { color: var(--color-gray-500); }
.text-sm { font-size: 0.875rem; }

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

Using Variables in Components

Reference global variables anywhere:

<!-- filename: src/lib/components/ServiceCard.svelte -->
<style>
  .card {
    background: white;
    border: 1px solid var(--color-gray-200);
    border-radius: var(--radius-lg);
    padding: var(--space-6);
    transition: box-shadow 0.2s ease;
  }
  
  .card:hover {
    box-shadow: var(--shadow-md);
  }
  
  .price {
    color: var(--color-primary);
    font-weight: 600;
  }
</style>

Components stay consistent with the global design system.


Common Mistakes

Overusing :global()

<style>
  /* ❌ Too much global */
  :global(.card) { }
  :global(.btn) { }
  :global(.title) { }
  
  /* ✅ Keep styles scoped, use variables for consistency */
  .card {
    border-radius: var(--radius-lg);
  }
</style>

Hardcoding Values

<style>
  /* ❌ Inconsistent hardcoded values */
  .card { padding: 17px; border-radius: 11px; }
  .form { padding: 1.2rem; border-radius: 8px; }
  
  /* ✅ Use variables for consistency */
  .card { padding: var(--space-4); border-radius: var(--radius-lg); }
  .form { padding: var(--space-4); border-radius: var(--radius-lg); }
</style>

Module Complete! 🎉

You’ve finished Module 8: Styling. BookIt now has:

  • Scoped component styles
  • Professional card design
  • Dynamic class handling
  • Polished form styling
  • Smooth hover effects
  • Consistent global design system

🎯 Beginner Level Complete!

Congratulations! You’ve completed the Beginner level of the BookIt learning path.

What you’ve built:

  • SvelteKit project structure
  • Multiple pages with routing
  • Shared layouts with header/footer
  • Reactive state with $state
  • Control flow with {#if} and {#each}
  • Form inputs with data binding
  • Reusable components with props
  • Professional CSS styling

What works:

  • Browse services
  • View service details
  • Fill out booking form
  • See live form preview
  • Navigate between pages

What’s missing:

  • Data is still hardcoded
  • Form doesn’t actually save
  • No database
  • No server-side logic

Summary

Global styles establish the foundation: colors, typography, spacing, and base element styling. Use CSS variables for consistency, import global CSS in your root layout, and use :global() sparingly for edge cases.

Key takeaways:

  • Import app.css in +layout.svelte
  • Define design tokens as CSS variables
  • Create utility classes for common patterns
  • Use :global() only when necessary

Next Steps

The client-side app is complete! Continue to Module 9: Loading Data to start the Intermediate level, where you’ll load real data from the server and make BookIt actually work.

Track complete
You've finished Build BookIt.