Svelte Special Elements
Regular HTML tags describe things inside your component. Special elements reach beyond it. They let a component listen to the browser window, change the page title, or pick a tag name at run time. Every special element starts with the svelte colon prefix.
The Window Element
The window element attaches listeners and bindings to the browser window without manual setup or cleanup. Svelte removes the listeners when the component disappears.
<script>
let width = 0;
let key = "";
</script>
<svelte:window bind:innerWidth={width} on:keydown={(e) => (key = e.key)} />
<p>Window width: {width}px</p>
<p>Last key pressed: {key}</p>Bindings such as innerWidth, innerHeight, scrollY, and online give live readings of the browser state.
Special Element Map
Your Component
|
+-- <svelte:window> --> browser window
+-- <svelte:document> --> whole document
+-- <svelte:body> --> body element
+-- <svelte:head> --> head section (title, meta)
+-- <svelte:element> --> tag chosen at run time
+-- <svelte:options> --> compiler settingsDocument And Body Elements
The document element listens to events such as visibilitychange. The body element attaches events and attributes to the body tag, such as mouse movement across the whole page.
<svelte:document on:visibilitychange={() => console.log("Tab changed")} />
<svelte:body on:mouseenter={() => console.log("Pointer entered")} />The Head Element
The head element inserts content into the document head. Each page can set its own title and description, which supports search engines and browser tabs.
<svelte:head>
<title>Pricing Plans</title>
<meta name="description" content="Compare plans and pick one." />
</svelte:head>The Element Element
The element tag renders an HTML tag whose name sits in a variable. A heading component can output h1 through h6 from a single level value.
<script>
export let level = 2;
$: tag = "h" + level;
</script>
<svelte:element this={tag}>
<slot />
</svelte:element>Dynamic Components
The component element chooses a component by variable. A tab panel swaps content with one line.
<script>
import Profile from "./Profile.svelte";
import Orders from "./Orders.svelte";
let current = Profile;
</script>
<button on:click={() => (current = Profile)}>Profile</button>
<button on:click={() => (current = Orders)}>Orders</button>
<svelte:component this={current} />Svelte 5 runes mode renders a component stored in a variable directly, so the component element becomes optional there.
The Options Element
The options element sits at the top level of a file and sets compiler options for that component, such as a custom element tag name.
<svelte:options customElement="info-card" />A Layman Analogy
A component lives inside a house. Regular elements are the furniture. Special elements are the wires that run to the outside world: the doorbell for the window, the mailbox for the head, and the remote control for swapping rooms. The house stays tidy because each wire has one clear purpose.
Common Mistakes To Avoid
- Placing window, document, or body elements inside blocks or other elements, which the compiler rejects
- Adding window listeners manually and forgetting cleanup
- Setting the page title in several components that compete with each other
Key Points
- Special elements connect a component to the window, document, body, and head
- The element tag chooses an HTML tag at run time
- The component tag swaps components from a variable
- Svelte handles listener cleanup automatically
