The script template style Structure
Understand the three-part structure of a Svelte component file.
The script template style Structure is a free Sveltejs Academy lesson on CoddyKit — lesson 1 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Sveltejs Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Three Sections
A Svelte component file has three optional top-level blocks: <script>, the markup, and <style>.
The script Block
The script block holds component logic in regular JavaScript or TypeScript.
<script>
let name = "Ada";
</script>The Markup
Everything outside script and style tags is the component markup. It is HTML enhanced with Svelte template syntax.
<h1>Hello {name}!</h1>The style Block
Styles are scoped to this component only. Other components keep their own look.
<style>
h1 { color: rebeccapurple; }
</style>A Complete Component
Combine all three into one .svelte file.
<script>
let name = "Ada";
</script>
<h1>Hello {name}!</h1>
<style>
h1 { color: rebeccapurple; }
</style>Order Convention
Convention puts script first, markup second, style last. Svelte does not enforce the order.
TypeScript Variant
Add lang="ts" to the script tag to use TypeScript inside.
<script lang="ts">
let name: string = "Ada";
</script>Module Script Context
Use <script context="module"> for code that runs once per module, not per instance.
<script context="module">
export const VERSION = "1.0";
</script>No Multiple Blocks
You may have at most one instance script and one module script per file.
Style Tag Optional
If your component has no styles, omit the style block entirely.
Script Tag Optional
Static markup components can skip the script block too. A pure-markup .svelte file is valid.
Quick Check
Where do you put scoped CSS?
Recap
A Svelte file is up to three blocks: script for logic, the markup, and style for scoped CSS.
Frequently asked questions
Is the “The script template style Structure” lesson free?
Yes — the full text of “The script template style Structure” is free to read here on the web, and the Sveltejs Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Sveltejs Academy course, upgrade to CoddyKit PRO.
What will I learn in “The script template style Structure”?
Understand the three-part structure of a Svelte component file. You practise Sveltejs Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Sveltejs Academy?
No prior experience is required. Sveltejs Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “The script template style Structure” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Sveltejs Academy lesson?
Yes. Every Sveltejs Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- The script template style Structure
- Reactive Variables with let
- Interpolation: {expression}
- Computed Values: $: reactive declarations