Skip to main content
Markup inside a Svelte component can be thought of as HTML++. You can use standard HTML elements alongside dynamic expressions and special Svelte features.

HTML Tags

Svelte distinguishes between regular HTML elements and components based on naming:
  • Lowercase tags like <div> denote regular HTML elements
  • Capitalized tags or dot notation like <Widget> or <my.stuff> indicate components

Element Attributes

Attributes work like their HTML counterparts, but with powerful enhancements for dynamic values.

Static Attributes

Values may be unquoted:

Dynamic Attributes

Attribute values can contain JavaScript expressions:
Or they can be JavaScript expressions:

Boolean and Nullish Attributes

Boolean Attributes

Included if truthy, excluded if falsy

Other Attributes

Included unless nullish (null or undefined)

Attribute Shorthand

When attribute name and value match, use shorthand syntax:

Component Props

Values passed to components are called props (not attributes):
Shorthand syntax works for props too:

Spread Attributes

Pass multiple attributes or props at once using spread syntax:
Order matters! Later attributes override earlier ones. In the example above, if things.a exists it overrides a="b", while c="d" overrides things.c.

Text Expressions

Embed JavaScript expressions in text using curly braces:

Expression Behavior

  • null or undefined values are omitted
  • All other values are coerced to strings
  • Regular expressions need parentheses:

Rendering HTML

Expressions are escaped by default. To render HTML, use the {@html} tag:
Always escape user-provided strings or use trusted values to prevent XSS attacks.

Comments

Use HTML comments inside components:

Disabling Warnings

Comments starting with svelte-ignore disable warnings for the next markup block:

Component Documentation

Add @component comments to show documentation when hovering over component usage:
—>