Chapter 7: Styling in Svelte

Svelte compiles <style> blocks into component-scoped CSS at build time, giving you isolation without runtime CSS-in-JS overhead, plus clean directives for dynamic classes and custom properties.

Component-Scoped Styles

Any CSS written inside a component's <style> tag is automatically scoped to that component by adding a deterministic hash class (such as svelte-1a2b3c) to matching elements. You can write plain element or class selectors like h2 or .card without worrying about colliding with another component's styles, and the Svelte compiler warns you if a selector does not match any element in the markup:

<script>
  let count = 0;
</script>

<section class="counter-box">
  <h2>Count: {count}</h2>
  <button type="button" on:click={() => count++}>Increment</button>
</section>

<style>
  .counter-box {
    background: #1e293b;
    border: 1px solid #334155;
    border-radius: 0.5rem;
    padding: 1rem;
  }

  button {
    background: #0284c7;
    color: #f8fafc;
    border: none;
    padding: 0.5rem 1rem;
    cursor: pointer;
  }

  /* Always preserve visible keyboard focus indicators */
  button:focus-visible {
    outline: 2px solid #38bdf8;
    outline-offset: 2px;
  }
</style>

Dynamic Styles

To toggle a CSS class conditionally based on reactive state, use Svelte's class: directive instead of string concatenation. When the class name matches the boolean variable name, you can even use the shorthand class:active:

<script>
  let active = false;
</script>

<button
  type="button"
  class:active
  aria-pressed={active}
  on:click={() => active = !active}
>
  {active ? 'Enabled' : 'Disabled'}
</button>

<style>
  button {
    padding: 0.5rem 1rem;
    background: #334155;
    color: #e2e8f0;
  }

  .active {
    background: #16a34a;
    color: #ffffff;
  }
</style>

Inline Styles

For values driven by runtime state—such as progress-bar widths, coordinates, or user-chosen theme colors—use the style: directive or CSS custom properties passed to a child component (<ProgressBar --bar-color="#38bdf8" />):

<script>
  let progress = 65;
  let accent = '#38bdf8';
</script>

<div class="track">
  <div class="fill" style:width={`${progress}%`} style:background={accent}></div>
</div>

<style>
  .track { background: #1e293b; height: 0.75rem; border-radius: 999px; overflow: hidden; }
  .fill { height: 100%; transition: width 0.2s ease; }
</style>

Global Styles

When you genuinely need a rule inside a component to style child-slot content or document-level elements like body, wrap only that selector in :global(...). Keep global overrides rare and prefer a dedicated global token stylesheet for app-wide typography and colors:

<style>
  /* Scoped wrapper styles any injected <a> inside a slot */
  .prose :global(a) {
    color: #38bdf8;
    text-decoration: underline;
  }
</style>

Practice: Build a StatusPill.svelte component with scoped base styles, a class:selected toggle, a dynamic style:border-color prop, and :focus-visible keyboard outlines.