Professional Documents
Culture Documents
Svelte 1 CH01 (Introduction)
Svelte 1 CH01 (Introduction)
1. Welcome to Svelte
Welcome to the Svelte tutorial! This will teach you everything you need
to know to easily build web applications of all sizes, with high
performance and a small footprint.
You can also consult the API docs and the examples, or — if you're
impatient to start hacking on your machine locally — create a project
with npm init svelte.
What is Svelte?
Svelte is a tool for building web applications. Like other user interface
frameworks, it allows you to build your app declaratively out of
components that combine markup, styles and behaviours.
These components are compiled into small, efficient JavaScript modules
that eliminate overhead traditionally associated with UI frameworks.
You can build your entire app with Svelte (for example, using an
application framework like SvelteKit, which this tutorial will cover), or
you can add it incrementally to an existing codebase. You can also ship
components as standalone packages that work anywhere.
Adding data
A component that just renders some static markup isn't very
interesting. Let's add some data.
First, add a script tag to your component and declare a name variable:
Inside the curly braces, we can put any JavaScript we want. Try
changing name to name.toUpperCase() for a shoutier greeting.
Challenge:
<h1>Hello world!</h1>
Solution:
3. Dynamic Attributes
Just like you can use curly braces to control text, you can use them to
control element attributes.
Our image is missing a src — let's add one:
That's better. But if you hover over the <img> in the editor, Svelte is
giving us a warning:
Note:
A11y: <img> element should have an alt attribute
When building web apps, it's important to make sure that
they're accessible to the broadest possible userbase, including people
with (for example) impaired vision or motion, or people without
powerful hardware or good internet connections. Accessibility
(shortened to a11y) isn't always easy to get right, but Svelte will help by
warning you if you write inaccessible markup.
In this case, we're missing the alt attribute that describes the image for
people using screenreaders, or people with slow or flaky internet
connections that can't download the image. Let's add one:
Challenge:
<script>
let src = '/image.gif';
</script>
<img />
Solution:
4. Styling
Just like in HTML, you can add a <style> tag to your component. Let's
add some styles to the <p> element:
Challenge:
Solution:
5. Nested Components
It would be impractical to put your entire app in a single component.
Instead, we can import components from other files and include them
in our markup.
Notice that even though Nested.svelte has a <p> element, the styles
from App.svelte don't leak in.
Note:
Component names are always capitalised, to distinguish them from
HTML elements.
Challenge:
Solution:
6. HTML Tags
Ordinarily, strings are inserted as plain text, meaning that characters like
< and > have no special meaning.
But sometimes you need to render HTML directly into a component. For
example, the words you're reading right now exist in a markdown file
that gets included on this page as a blob of HTML.
In Svelte, you do this with the special {@html ...} tag:
Note:
Warning! Svelte doesn't perform any sanitization of the expression
inside {@html ...} before it gets inserted into the DOM. This isn't an
issue if the content is something you trust like an article you wrote
yourself. However if it's some untrusted user content, e.g. a comment
on an article, then it's critical that you manually escape it, otherwise
you risk exposing your users to Cross-Site Scripting (XSS) attacks.
Challenge:
Solution: