Skip to main content

<svelte:element>

<svelte:element this={expression} />

The <svelte:element> element lets you render an element that is unknown at author time, for example because it comes from a CMS. Any properties and event listeners present will be applied to the element.

The only supported binding is bind:this, since Svelte’s built-in bindings do not work with generic elements.

If this has a nullish value, the element and its children will not be rendered.

If this is the name of a void element (e.g., br) and <svelte:element> has child elements, a runtime error will be thrown in development mode:

<script>
	let tag = $state('hr');
</script>

<svelte:element this={tag}>
	This text cannot appear inside an hr element
</svelte:element>

Svelte infers the namespace from the element’s surroundings, not from the value of this (except for 'svg' itself). If an SVG element is not inside a regular <svg> element or another element with a known SVG namespace, you must specify the namespace with an xmlns attribute:

<svelte:element this={tag} xmlns="http://www.w3.org/2000/svg" />

This also applies to SVG children of a <svelte:element> whose this value is 'svg'. Set xmlns on the parent so that its children inherit the SVG namespace:

<svelte:element this={tag} xmlns="http://www.w3.org/2000/svg">
	<svelte:element this={childTag} />
</svelte:element>

Without the correct namespace, SVG elements may appear after server rendering but disappear when recreated on the client.

this needs to be a valid DOM element tag, things like #text or svelte:head will not work.

Edit this page on GitHub llms.txt