Skip to content
Tutorials Center
My learning

Topics

Lesson 3 of 35 ยท Getting Started

Anatomy of a Svelte Component

Understand the three parts of a Svelte 5 component: script, markup and scoped style, plus module scripts, imports and how components nest.

Every .svelte file is a component. It has up to three top-level parts, in any order: a <script>, the markup (everything else), and a <style>. Unlike many frameworks there is no wrapper function or class: the file itself is the component, and the compiler decides how to turn it into code.

The script block

The <script> runs once for each instance of the component. Each time you use <Counter />, the script runs again, so every counter gets its own state.

SVELTE
<script>
  let count = $state(0);
  let doubled = $derived(count * 2);

  function increment() {
    count += 1;
  }
</script>

Anything you declare at the top level is visible in the markup. You can import other components, helpers and data:

SVELTE
<script>
  import Header from './Header.svelte';
  import { formatPrice } from '$lib/money.js';

  const price = 1999;
</script>

TypeScript

Add lang="ts" to use TypeScript in the script block:

SVELTE
<script lang="ts">
  let name: string = $state('Ada');
</script>

Modern Svelte supports type annotations directly without extra preprocessing setup in most templates.

The markup

Everything outside <script> and <style> is the template. It is HTML plus a few curly-brace features:

  • prints a JavaScript expression.
  • {#if}, {#each}, {#await}, {#key} are block statements.
  • {@html}, {@const}, {@render} are tags.
  • Attributes accept expressions: <img src={url} alt={label}>.
  • Event attributes are plain properties: onclick={handler}.
SVELTE
<h1>{title}</h1>
<ul>
  {#each items as item}
    <li>{item}</li>
  {/each}
</ul>

A component can have more than one root element; there is no required wrapper like a fragment.

The scoped style block

CSS in <style> is scoped to the component. The compiler adds a generated class to your elements and rewrites selectors to match it, so p { color: tomato } only colors paragraphs in this component.

SVELTE
<p>This paragraph is styled.</p>

<style>
  p {
    color: tomato;
    font-weight: 600;
  }
</style>

To style something globally, use :global(...):

SVELTE
<style>
  :global(body) {
    margin: 0;
  }
  .card :global(a) {
    text-decoration: none;
  }
</style>

The compiler also removes CSS rules that match nothing in the markup, and warns you about them. That keeps shipped CSS small.

The module script

A second script, <script module>, runs once when the file is first loaded, not per instance. Use it for values and helpers shared by all instances.

SVELTE
<script module>
  let instances = 0;

  export function describe() {
    return `${instances} counters created`;
  }
</script>

<script>
  instances += 1;
  let count = $state(0);
</script>

Older Svelte used <script context="module">; that spelling is deprecated in Svelte 5.

Nesting components

You compose a page from smaller components. Component names start with a capital letter, which is how Svelte tells them apart from HTML elements:

SVELTE
<script>
  import Card from './Card.svelte';
  import Avatar from './Avatar.svelte';
</script>

<Card>
  <Avatar name="Grace" />
</Card>

Props pass data down; callbacks and snippets pass behavior and markup. Props is the next topic in this course after the reactivity chapter.

Seeing the three parts together

The stand-in below builds a "component" as a plain function: a script part (local state), a markup part (an HTML string) and a style part (a scoped class). It mirrors what the compiler arranges for you. Every call makes a fresh instance with its own state, which is exactly the per-instance behavior of the Svelte script block.

JS Instances have their own state (stand-in)

Press Run (or Ctrl + Enter) to see the console output here.

No output.

Two instances, two separate counters. That independence is why you can drop the same component on a page ten times.

Scoped style in a plain page

Common mistakes

  • Starting a component name with a lowercase letter, which Svelte treats as an HTML tag.
  • Expecting <style> rules to reach into child components. Scoped styles stop at the component boundary; use :global or CSS variables to cross it.
  • Putting per-instance state in <script module>, which would be shared by every instance.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. How many times does a normal <script> run for a component?

    Each <Counter /> usage gets its own run of the script, so its state is independent.
  2. 2. What does :global(body) do inside a component style block?

    :global(...) opts a selector out of scoping.
  3. 3. Which block runs once when the file is first loaded, shared by all instances?

    <script module> (formerly context="module") runs once per module, not per instance.
  4. 4. Why must component names start with an uppercase letter?

    <Card /> is a component; <card> would be treated as an HTML element.

Summary

  • A .svelte file has a script, markup and a scoped style.
  • The instance script runs once per component instance; <script module> runs once per file.
  • Styles are scoped by default; :global() opts out.
  • Components are capitalized and can be nested freely.

Next: The Compiler and the Bundle