📖 Scenario: You are building a simple Svelte component to display a greeting message on a webpage. This will help you understand the basic structure of a Svelte component file.
🎯 Goal: Create a Svelte component file with the correct sections: a script block for JavaScript, a style block for CSS, and markup for HTML content. The component will show a greeting message styled with a color.
📋 What You'll Learn
Create a <script> block with a variable greeting set to 'Hello, Svelte!'
Add a <style> block that sets the text color of a class .greeting to blue
Write HTML markup that uses a <h1> element with class greeting to display the greeting variable
Ensure the component file has all three sections properly placed
💡 Why This Matters
🌍 Real World
Svelte components are the building blocks of modern web apps. Understanding their structure helps you create interactive and styled UI parts.
💼 Career
Knowing how to organize Svelte files is essential for frontend developer roles using Svelte. It improves code readability and maintainability.
Progress0 / 4 steps
1
Create the script block with greeting variable
Create a <script> block and inside it declare a variable called greeting with the exact value 'Hello, Svelte!'.
Svelte
Hint
Use let greeting = 'Hello, Svelte!'; inside the <script> tags.
2
Add a style block for greeting color
Add a <style> block below the script block. Inside it, create a CSS rule for the class .greeting that sets color to blue.
Below the style block, add an <h1> element with class greeting. Inside it, use curly braces to insert the greeting variable so it displays the text.
Svelte
Hint
Use <h1 class="greeting">{greeting}</h1> to show the message.
4
Complete the Svelte component structure
Ensure the component file has the <script> block first, then the <style> block, and finally the HTML markup with the <h1> element displaying the greeting. The order and structure must be correct.
Svelte
Hint
The script block must come first, then style, then markup. Check your file order.
Practice
(1/5)
1. What are the three main parts you can find inside a .svelte component file?
easy
A. JavaScript, HTML, and SQL
B. JavaScript, XML, and CSS
C. HTML, CSS, and JSON
D. JavaScript, HTML, and CSS
Solution
Step 1: Identify the parts of a .svelte file
A .svelte file combines JavaScript for logic, HTML for structure, and CSS for styling.
Step 2: Confirm the correct combination
Only JavaScript, HTML, and CSS lists JavaScript, HTML, and CSS, which are the correct parts.
In Svelte, event handlers like on:click should be assigned JavaScript expressions without quotes.
Step 2: Identify the error in the code
The code uses on:click="count++" with quotes, which causes a syntax error.
Final Answer:
The on:click handler should not use quotes -> Option B
Quick Check:
Event handlers = no quotes around JS code [OK]
Hint: Use on:event={handler} without quotes in Svelte [OK]
Common Mistakes:
Adding quotes around event handlers
Using const instead of let for reactive variables
Thinking semicolons are mandatory in Svelte scripts
5. You want to create a .svelte component that shows a list of items with each item styled differently. Which approach correctly uses the component file anatomy to achieve this?
hard
A.
{#each items as item}
{item}
{/each}
B.
{items.map(item => `
${item}
`)}
C.
{items.forEach(item =>
{item}
)}
D.
{#each items as item}
{item}
{/each}
Solution
Step 1: Understand Svelte's each block for lists
Svelte uses {#each} blocks to loop over arrays and render elements for each item.
Step 2: Check styling and rendering correctness
{#each items as item}
{item}
{/each}
uses {#each} correctly and applies scoped CSS to li elements.
{#each items as item}
{item}
{/each}
overrides color inline, which is inconsistent with the style block.
Final Answer:
Use {#each} with scoped CSS for list items -> Option A
Quick Check:
List rendering + scoped CSS =
{#each items as item}
{item}
{/each}
[OK]
Hint: Use {#each} for lists and style with <style> tag [OK]
Common Mistakes:
Using map or forEach inside markup incorrectly
Mixing inline styles with scoped CSS unnecessarily
Styling wrong elements like ul instead of li
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.