Svelte uses three parts to build a component: script for logic, markup for structure, and style for look. This keeps things clear and organized.
Script, markup, and style sections in Svelte
<script> // JavaScript code here </script> <!-- HTML markup here --> <style> /* CSS styles here */ </style>
The <script> section holds JavaScript logic like variables and functions.
The markup is plain HTML but can use Svelte features like curly braces for variables.
<script> let count = 0; </script> <button on:click={() => count++}> Clicked {count} times </button> <style> button { font-size: 1.2rem; } </style>
<script> let name = 'Friend'; </script> <h1>Hello {name}!</h1> <style> h1 { color: teal; } </style>
This Svelte component shows how script, markup, and style work together. The script holds a message and a function to change it. The markup displays the message and a button that calls the function when clicked. The style makes the text and button look nice and accessible.
<script> let message = 'Welcome to Svelte!'; function changeMessage() { message = 'You clicked the button!'; } </script> <main> <h2>{message}</h2> <button on:click={changeMessage} aria-label="Change message"> Click me </button> </main> <style> main { font-family: system-ui, sans-serif; text-align: center; margin-top: 2rem; } h2 { color: #2c3e50; } button { background-color: #42b983; border: none; padding: 0.5rem 1rem; color: white; font-size: 1rem; border-radius: 0.3rem; cursor: pointer; } button:hover, button:focus { background-color: #369870; outline: none; } </style>
The <style> section styles only this component by default, avoiding conflicts.
Use on:click to handle user clicks in markup.
Always add aria-label or other accessibility attributes to interactive elements.
Svelte components have three parts: script for logic, markup for structure, and style for appearance.
This separation helps keep code clean and easy to manage.
Styles inside a component apply only there, making your app easier to maintain.
Practice
Solution
Step 1: Identify the purpose of each section
The<script>section is for JavaScript logic,<style>is for CSS, and markup is the HTML structure.Step 2: Match the section to JavaScript logic
Since logic means JavaScript code, it belongs in the<script>section.Final Answer:
<script> section -> Option BQuick Check:
Logic = <script> section [OK]
- Confusing <style> with <script>
- Thinking markup is for logic
- Assuming a <logic> tag exists
Solution
Step 1: Understand Svelte's style scoping
Svelte automatically scopes styles inside the<style>tag to the component without needing extra attributes.Step 2: Identify correct syntax
The correct syntax is just<style>with CSS inside. Addingscopedorglobalchanges behavior or is invalid.Final Answer:
<style> h1 { color: red; } </style> -> Option CQuick Check:
Default <style> is scoped in Svelte [OK]
- Adding 'scoped' attribute which is unnecessary
- Using <css> tag instead of <style>
- Confusing global styles with scoped styles
<script>
let name = 'Anna';
</script>
<h1>Hello {name}!
</h1>
<style>
h1 { color: blue; }
</style>Solution
Step 1: Understand variable interpolation in markup
The{name}inside markup inserts the value of the variablename, which is 'Anna'.Step 2: Recognize scoped styles effect
The<style>section styles theh1with blue color scoped to this component.Final Answer:
<h1> Hello Anna! </h1> with blue text -> Option DQuick Check:
Variable interpolation + scoped style = blue 'Hello Anna!' [OK]
- Thinking {name} prints literally
- Assuming styles don't apply inside component
- Confusing style scoping with errors
<script>
let count = 0
</script>
<button on:click="count++">Click me</button>
<style>
button { font-size: 1.2rem; }
</style>Solution
Step 1: Check event handler syntax in markup
In Svelte, event handlers useon:event={handler}without quotes around the handler expression.Step 2: Identify the incorrect usage
The code useson:click="count++"with quotes, which is invalid syntax in Svelte.Final Answer:
Incorrect event handler syntax with quotes -> Option AQuick Check:
Event handlers need curly braces, no quotes [OK]
- Adding quotes around event handler
- Thinking semicolon is mandatory in script
- Believing style must come before script
error is true. Which combination of script, markup, and style is correct?
<script>
let error = true;
</script>
<!-- markup here -->
<style>
.error { color: red; }
</style>Solution
Step 1: Use conditional markup to show message only if error is true
The{#if error} ... {/if}block conditionally renders content based on the variable.Step 2: Apply CSS class for red color
Usingclass="error"applies the scoped style.error { color: red; }correctly.Final Answer:
{#if error}<p class="error">Error occurred</p>{/if} -> Option AQuick Check:
Conditional + class with style = correct red message [OK]
- Showing message always without condition
- Not applying CSS class for color
- Using inline style ignoring style section
Ready to prove it?
Pass the test and this pattern moves unknown
