Svelte Attributes
Attributes describe how an HTML element looks and behaves. An image has a source, a link has a destination, and a button can be disabled. Svelte lets you fill these attributes with live values from your script.
Dynamic Attributes
Curly braces connect an attribute to a variable. When the variable changes, Svelte updates the attribute on the page automatically.
<script>
let photoUrl = "/images/team.webp";
let photoText = "Our team at work";
</script>
<img src={photoUrl} alt={photoText} />Plain text and curly braces combine inside one attribute value. Quotes wrap the whole value when the text mixes with an expression.
<script>
let user = "Maya";
</script>
<button title="Open profile of {user}">Profile</button>Attribute Flow Diagram
let photoUrl = "/images/team.webp"
|
v
<img src={photoUrl} />
|
v
Browser shows the image
photoUrl changes to "/images/office.webp"
|
v
Svelte updates only the src attributeShorthand Attributes
A variable that shares its name with the attribute needs no repetition. The shorthand form saves typing and reads cleanly.
<script>
let src = "/images/logo.webp";
let alt = "Company logo";
</script>
<img {src} {alt} />Boolean Attributes
Some attributes work like switches. The disabled, checked, and required attributes are either present or absent. Svelte adds the attribute when the value is true and removes it when the value is false.
<script>
let isSaving = false;
</script>
<button disabled={isSaving}>
{isSaving ? "Saving..." : "Save"}
</button>A value of null or undefined removes the attribute completely. This behavior prevents strange output such as the word undefined appearing in your HTML.
Spread Attributes
An object can supply many attributes in one step. The spread syntax unpacks every key and value from the object onto the element.
<script>
const linkSettings = {
href: "https://example.com",
target: "_blank",
rel: "noopener noreferrer"
};
</script>
<a {...linkSettings}>Visit Example</a>Spread attributes shine in reusable components. A button component can accept extra settings from its parent and pass them along to the real button element without naming each one.
Dynamic Classes And Styles
The class and style attributes accept expressions like any other attribute. A ternary picks between two class names based on a variable.
<script>
let isActive = true;
let size = 18;
</script>
<p class={isActive ? "tab active" : "tab"} style="font-size: {size}px">
Overview
</p>Changing isActive swaps the class name, and changing size updates the font immediately. Keep the class names short and descriptive so the markup stays easy to scan.
A Layman Analogy
Think of an element as a name badge at a conference. The badge has slots for a name, a company, and a role. Dynamic attributes fill each slot from a guest list that can change at any moment. Spread attributes work like a pre-printed sticker sheet that fills every slot together.
Accessibility Warnings
Svelte checks attributes while you build. An image without alt text, a link without a destination, and a misused role all trigger warnings in the terminal. Treat these warnings as helpful reviews, since screen reader users depend on correct attributes.
Common Mistakes To Avoid
- Wrapping a single expression in quotes when braces alone are enough
- Passing the text "false" to a boolean attribute, which counts as true because the text is not empty
- Forgetting alt text on images
- Spreading an object that contains untrusted keys onto an element
Key Points
- Curly braces bind attribute values to variables
- Shorthand syntax removes repeated names
- Boolean attributes appear or vanish based on true and false
- Spread syntax applies a whole object of attributes at once
