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>
<script>holds the logic: variables, functions, imports.- The markup in the middle is what it shows: HTML plus curly braces.
<style>holds CSS just for this component.- All three are optional (they can be left out). A file with only markup is still a valid component. The usual order: script, markup, style.
- The file name becomes the component's name:
MenuItem.svelteis the componentMenuItem. The name is written in PascalCase, meaning every word starts with a capital letter.
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 />
import MenuItem from "./MenuItem.svelte"takes the component from its file. The text in quotes is the path (the file's address) to that component's file,.svelteincluded../means the same folder. The same pattern works for any component, for exampleimport Greeting from "./Greeting.svelte".<MenuItem />places one copy. Three lines, three cards. In the compiled code the component becomes a plain function, called three times asMenuItem(node, {}).- The three cards are exactly the same, all "Fried Rice". To give each copy its own content, we use props (data sent to a component from outside). Props are the next lesson.
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>
svelte-n0sg1ris a class the compiler makes from the style's content. The selectorpbecomesp.svelte-n0sg1r.- That class is only added to the
<p>written inMenuItem.svelte. The<h3>gets no class because no style targets it. - The opening hours paragraph is written in
App.svelte, so it doesn't have that class and stays black.
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?
<menuItem />, starting with a lowercase letter, is treated as a plain HTML tag, not a component, so the card doesn't appear. If the import isimport MenuItem(capital M), the compiler only gives the warningelement_invalid_self_closing_tag:Self-closing HTML tags for non-void elements are ambiguous. That warning only talks about the/>on a plain HTML tag and says nothing about the capital letter. The warning<menuItem> will be treated as an HTML element unless it begins with a capital letterappears only when the import itself is lowercase (import menuItem). Component names always start with a capital letter.- If you write
import MenuItem from "./MenuItem", without.svelte, the file can't be found at build time. Write it in full:"./MenuItem.svelte". - App's style doesn't reach a child component's content (a child component is one placed inside another, like MenuItem in App), because scoped style works both ways. To style a child component, write the style in that child's own file.
- One
.sveltefile is always one component. The menu card and the cart are two files.
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.