Styling for Sveltes
Styling in Svelte covers how CSS is attached to components and how elements move. Styles written in a style block are scoped to their component by default, classes can be toggled with the class directive, and built-in transitions and animations handle entering, leaving and reordering elements in the DOM.
Stage: Svelte Basics · Part of the Svelte roadmap · Last reviewed 19 November 2026
What to learn in Styling
- Write component-scoped CSS and explain why it will not leak elsewhere
- Apply conditional classes using the class directive or class attribute
- Reach global selectors deliberately with the :global modifier
- Pass values into CSS through custom properties
- Add fade, fly or slide transitions to elements that enter and leave
- Animate a keyed list reorder using the animate directive
Styling interview questions
How does Svelte scope CSS to a component?
At compile time Svelte adds a generated class to the component's elements and rewrites the selectors in its style block to match that class. Rules therefore only affect that component's own markup unless you opt out with :global.
When does a Svelte transition actually run?
A transition plays when an element is added to or removed from the DOM, typically by an if or each block. Changing a style on an element that stays mounted does not trigger it, so use CSS transitions or animations for that.
Where Styling fits in the Svelte roadmap
Styling belongs to the Svelte Basics stage (beginner level), which takes about 15 hours. Components and runes.
Other units in this stage
Back to the full Svelte roadmap