Basic Svelte
Introduction
Bindings
Classes and styles
Advanced Svelte
Advanced reactivity
Motion
Advanced bindings
Advanced transitions
Context API
Special elements
<script module>
Next steps
Basic SvelteKit
Introduction
Routing
Loading data
Headers and cookies
Shared modules
API routes
$app/state
Errors and redirects
Advanced SvelteKit
Page options
Link options
Advanced routing
Advanced loading
Environment variables
Conclusion
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:
<img src={src} />
Thatβs better. But if you hover over the <img>
in the editor, Svelte is giving us a warning:
`<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:
<img src={src} alt="A man dances." />
We can use curly braces inside attributes. Try changing it to "{name} dances."
β remember to declare a name
variable in the <script>
block.
Shorthand attributes
Itβs not uncommon to have an attribute where the name and value are the same, like src={src}
. Svelte gives us a convenient shorthand for these cases:
<img {src} alt="{name} dances." />
<script>
let src = '/tutorial/image.gif';
</script>
<img />