Skip to main content
Basic Svelte
Introduction
Reactivity
Props
Logic
Events
Bindings
Classes and styles
Actions
Transitions
Advanced Svelte
Advanced reactivity
Reusing content
Motion
Advanced bindings
Advanced transitions
Context API
Special elements
<script module>
Next steps
Basic SvelteKit
Introduction
Routing
Loading data
Headers and cookies
Shared modules
Forms
API routes
$app/state
Errors and redirects
Advanced SvelteKit
Hooks
Page options
Link options
Advanced routing
Advanced loading
Environment variables
Conclusion

The <svelte:html> element allows you to listen to events and set attributes on the <html>. This is useful to for example set the lang tag dynamically, or to update classes that influence the whole page.

Add a conditional class attribute to the <svelte:html> tag...

App
<svelte:html
	class={darkmode ? 'dark' : 'light'}
/>

...and click the button to toggle the mode.

Edit this page on GitHub

1
2
3
4
5
6
7
8
<script>
	let darkmode = $state(false);
</script>
 
<svelte:html />
 
<button onclick={() => darkmode = !darkmode}>toggle dark mode</button>