The svelte:head tag lets you add or change content inside the HTML document's <head> section from your Svelte component. This helps set page titles, meta tags, and links dynamically.
svelte:head for document head
<svelte:head> <!-- head content here --> </svelte:head>
Anything inside svelte:head will be placed inside the HTML <head> tag.
You can use expressions and reactive variables inside svelte:head just like in the main component.
<svelte:head> <title>My Page Title</title> </svelte:head>
<script> let pageTitle = 'Welcome'; </script> <svelte:head> <title>{pageTitle}</title> </svelte:head>
<svelte:head> <meta name="description" content="This is a Svelte app"> <link rel="icon" href="/favicon.ico"> </svelte:head>
This Svelte component sets the page title and viewport meta tag inside the document head. The main content shows a heading using the same title variable.
<script> let title = 'Home Page'; </script> <svelte:head> <title>{title}</title> <meta name="viewport" content="width=device-width, initial-scale=1"> </svelte:head> <main> <h1>Welcome to the {title}</h1> </main>
Only one svelte:head block is needed per component, but you can have multiple if you want.
When components unmount, their head tags are removed automatically if they were added dynamically.
Use svelte:head to improve SEO and user experience by controlling the document head from your components.
svelte:head lets you add tags inside the HTML <head> from Svelte components.
You can set titles, meta tags, links, and more dynamically using variables.
This helps make your app's pages more meaningful and SEO-friendly.