Skip to main content
Svelte components can include a <style> element containing CSS that belongs to the component. This CSS is scoped by default, meaning that styles will not apply to any elements on the page outside the component in question.

How Scoped Styles Work

Svelte’s compiler adds a unique class to affected elements based on a hash of the component styles (e.g. svelte-123xyz). This ensures complete style isolation between components.
The compiled output adds the scoping class automatically:
Scoped styles are one of Svelte’s most powerful features. The compiler handles all the work of ensuring your component styles don’t leak to other parts of your application.

Specificity

Each scoped selector receives a specificity increase of 0-1-0, as a result of the scoping class (e.g. .svelte-123xyz) being added to the selector. This means that a p selector defined in a component will take precedence over a p selector defined in a global stylesheet, even if the global stylesheet is loaded later.
In this case, the component’s red color wins, even if global.css is loaded after the component.
In some cases, the scoping class must be added to a selector multiple times, but after the first occurrence it is added with :where(.svelte-xyz123) to avoid increasing specificity further.

Scoped Keyframes

If a component defines @keyframes, the name is scoped to the component using the same hashing approach. Any animation rules in the component will be similarly adjusted:
The compiler automatically renames both the @keyframes definition and the animation reference to something like bounce-svelte-123xyz, preventing naming conflicts between components.

Global Styles

To apply styles globally, use the :global(...) modifier:
For global keyframes, prepend your keyframe names with -global-:
The -global- part will be removed when compiled, and the keyframe will be referenced using just my-animation-name elsewhere in your code.