Discover how keeping your code's parts neatly separated can make building web apps feel like a breeze!
Why Script, markup, and style sections in Svelte? - Purpose & Use Cases
Imagine building a web page where you write your JavaScript, HTML, and CSS all mixed together in one big file or scattered in separate files without clear organization.
Every time you want to change something, you have to hunt through the code to find where the logic, the structure, or the look is defined.
This messy approach makes your code hard to read and maintain.
It's easy to accidentally break something because the parts are tangled.
Also, browsers don't understand this mix well, so you need extra tools to bundle and fix it.
Svelte solves this by letting you write your script (logic), markup (HTML), and style (CSS) in clear separate sections inside one file.
This keeps your code neat, easy to understand, and ready to turn into fast web pages.
<script>function greet() { alert('Hi'); }</script><div onclick="greet()">Click me</div><style>div { color: red; }</style><script>function greet() { alert('Hi'); }</script>
<div on:click={greet}>Click me</div>
<style>div { color: red; }</style>It enables you to build interactive web components that are easy to write, style, and maintain all in one place.
Think of a button on a website that changes color when clicked and shows a message. With Svelte's sections, you write the button's look, behavior, and style together clearly.
Separate script, markup, and style keep code organized.
Improves readability and maintenance.
Makes building interactive components simpler and faster.
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
