Lesson 7 of 9 beginner 4 min read

Classes and Style

Dynamic classes with class:active, object shorthand, inline style directives, and scoped CSS.

Interactive Laboratory

Live Runtime

Experiment directly with Classes and Style concepts in real time

SvelteKit Full-Stack Pipeline
1. Request
Hook
2. Loader
load()
3. Server
SSR HTML
4. Browser
Hydrate

Classes and Style Directives

Svelte provides intuitive syntax for conditionally toggling CSS classes and applying dynamic inline styles without messy template string concatenation.


1. Class Shorthand and Directives

<script lang="ts">
	let isActive = $state(false);
	let isError = $state(false);
	let theme = $state<'dark' | 'light'>('dark');
</script>
 
<!-- 1. class:name directive -->
<button class="btn" class:active={isActive} class:danger={isError}> Toggle Status </button>
 
<!-- 2. Object class syntax -->
<div class={{ active: isActive, error: isError, 'theme-dark': theme === 'dark' }}>Status Panel</div>

2. Style Directives (style:property)

Apply dynamic inline CSS styles directly to elements:

<script lang="ts">
	let hue = $state(210);
	let size = $state(24);
</script>
 
<div
	style:background-color="hsl({hue}, 80%, 50%)"
	style:font-size="{size}px"
	style:padding="1rem"
	style:border-radius="8px"
>
	Dynamic Box
</div>

3. Scoped Styles and :global()

By default, all styles inside <style> blocks are strictly scoped to the current component. Svelte generates unique hash class selectors to eliminate style collisions.

To style child elements or targeted global selectors, use the :global() modifier:

<style>
	/* Scoped to this component only */
	h1 {
		font-weight: 800;
	}
 
	/* Targets all .highlight elements inside child components */
	:global(.highlight) {
		background-color: var(--primary);
	}
</style>