Your First Svelte Component

One .svelte file is one component, a piece of the interface that you write once and can place many times. A component has three optional parts (they can be…

Mrs. Sri's stall page needs the same menu card again and again. Writing the card's HTML three times is tiring, and once the design changes, you have to change three places. The solution is a component: a piece of the interface that you write once and then place as many times as you like. In Svelte, one component is one .svelte file.

Think of a component as the stall's rubber stamp. The stamp is carved once, then pressed on every receipt, and every print comes out the same. The ink only lands where the stamp touches, never spreading onto the rest of the receipt. The .svelte file is the stamp. Each time the component is placed on the page (later written like <MenuItem />), that is one press. The style inside the component is the ink: it only touches the part that belongs to that component.

One file, three parts

Here is the menu card of Mrs. Sri's stall as a component, in the file MenuItem.svelte:

<script>
  let name = "Fried Rice"
  let price = 15
</script>

<h3>{name}</h3>
<p>${price.toFixed(2)}</p>

<style>
  p {
    color: #c2410c;
    font-weight: bold;
  }
</style>

Using a component inside another

To place a component, import it in <script>, then write it like a tag. This is App.svelte, the app's outermost component, the one that holds the others:

<script>
  import MenuItem from "./MenuItem.svelte"
</script>

<h1>Mrs. Sri's Stall</h1>
<p>Open every day, 07:00 to 21:00.</p>

<MenuItem />
<MenuItem />
<MenuItem />

The result on the page: the title Mrs. Sri's Stall, the opening hours paragraph, then three pairs of <h3>Fried Rice</h3> and <p>$15.00</p>.

Scoped style

Style in a Svelte component is scoped automatically: it only applies to elements that belong to that component. The compiler does it by giving a unique class (a class name that is used nowhere else) to the elements of the component that its style targets. From the code above, Svelte produces the CSS and HTML below. The HTML is cut short: only the first card is written.

p.svelte-n0sg1r {
  color: #c2410c;
  font-weight: bold;
}
<h1>Mrs. Sri's Stall</h1>
<p>Open every day, 07:00 to 21:00.</p>
<h3>Fried Rice</h3>
<p class="svelte-n0sg1r">$15.00</p>

So only the price paragraph inside MenuItem turns orange (#c2410c). It also works the other way: if App.svelte has its own p style, that style doesn't touch the paragraph inside MenuItem, because App gets its own class (svelte-5kgbet).

If the style names an element that isn't in the component, such as h1 { color: red; } in MenuItem, the compiler warns Unused CSS selector "h1" and removes that rule.

Exercise: your first component

In this lesson's exercise, you make one component with markup only. The starter code is just a comment. Replace that comment with:

<h1>Hyper Sheets</h1>
<p>Learning Svelte is fun!</p>

The <h1> holds Hyper Sheets and the <p> holds Learning Svelte is fun!, exactly, exclamation mark included. No <script> or <style> needed.

Svelte 4 and Svelte 5

The file structure, the import, and placing <MenuItem /> are exactly the same in both versions. The only difference is how the outermost component is placed on the page, in the main.js file. In Svelte 5 that file has mount(App, { target: document.getElementById("app") }), with mount imported from svelte. target is the element on the page where App is placed. In older Svelte 4 code you'll see new App({ target: document.getElementById("app") }). If you use SvelteKit, that part is done for you.

⚠️ Card missing, or the style not applied?

TL;DR: One .svelte file is one component, a piece of the interface that you write once and can place many times. A component has three optional parts (they can be left out): <script> for logic, markup (HTML) for what it shows, and <style> for CSS. A component such as MenuItem is used with import MenuItem from "./MenuItem.svelte" and then <MenuItem />, as many times as you want. Its name is PascalCase (every word starts with a capital letter), because a lowercase <menuItem /> is treated as an HTML tag. Style in a component is scoped automatically: the compiler adds a class such as svelte-n0sg1r only to that component's elements, so its style doesn't leak out, and style from outside doesn't leak in.

⚡ One file, three parts

Write:

<script>
  let name = "Fried Rice"
  let price = 15
</script>

<h3>{name}</h3>
<p>${price.toFixed(2)}</p>

<style>
  p {
    color: #c2410c;
    font-weight: bold;
  }
</style>

Meaning: One .svelte file is one component, a piece of the interface you can place many times. It holds <script> for logic, markup (HTML) for what it shows, <style> for CSS. All three are optional. The file name becomes the component's name: this file is named MenuItem.svelte, so the component is MenuItem.

⚡ Import it, then use it like a tag

Write:

<script>
  import MenuItem from "./MenuItem.svelte"
</script>

<MenuItem />
<MenuItem />
<MenuItem />
Guess first: if you write <menuItem />, with a lowercase first letter, does the card appear?

No. A lowercase name is treated as a plain HTML tag, not a component. Component names always start with a capital letter.

Meaning: This code places the MenuItem component from the previous step. The path (the file's address) in import is written in full, .svelte included. Each <MenuItem /> places one copy: three lines, three cards.

⚡ The style is scoped

Guess first: p { color: #c2410c } (orange) is written in MenuItem's <style>. App, the component that places MenuItem, has its own paragraph too, such as the stall's opening hours. Does that paragraph in App turn orange as well?

No. The compiler adds a unique class such as svelte-n0sg1r only to the <p> that belongs to MenuItem, so the paragraph in App stays black.

Meaning: Style in a component is scoped automatically, meaning it only applies to that component's own elements: it doesn't leak out, and style from outside doesn't leak in.

What you will learn