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.
Practice
<svelte:head> tag in a Svelte component?Solution
Step 1: Understand the role of <svelte:head>
The <svelte:head> tag is specifically designed to insert or update tags inside the HTML <head> section, such as titles and meta tags.Step 2: Compare with other options
Options A, B, and C describe unrelated tasks: event handling, body content, and styling, which are not the purpose of <svelte:head>.Final Answer:
To add or modify elements inside the HTML <head> section of the page. -> Option AQuick Check:
svelte:head modifies <head> = D [OK]
- Confusing svelte:head with component body content
- Thinking it styles components directly
- Assuming it handles events
<svelte:head> in a Svelte component?Solution
Step 1: Identify correct tag usage
The correct tag to modify the document head in Svelte is <svelte:head>, not <svelte-head> or <head> inside it.Step 2: Check nesting rules
You place the <title> tag directly inside <svelte:head>, without nesting another <head> tag inside it.Final Answer:
<svelte:head><title>My Page</title></svelte:head> -> Option CQuick Check:
Correct svelte:head syntax = C [OK]
- Using <head> instead of
- Adding extra <head> inside
- Misspelling the tag as
<script>
let pageTitle = 'Welcome';
</script>
<svelte:head>
<title>{pageTitle} to Svelte</title>
</svelte:head>
<h1>Hello!</h1>Solution
Step 1: Understand variable interpolation in svelte:head
The variablepageTitleis set to 'Welcome' and used inside the <title> tag with curly braces, so it will be replaced by its value.Step 2: Determine the final title text
The title becomes 'Welcome to Svelte' because the variable is replaced and concatenated with the string ' to Svelte'.Final Answer:
Welcome to Svelte -> Option BQuick Check:
Variable interpolation works in svelte:head = B [OK]
- Thinking variables show as literal text
- Confusing page content with title text
- Ignoring interpolation syntax
<svelte:head> <title>My Site</title> <meta name="description" content="A cool site"> </svelte:head>
Solution
Step 1: Check HTML validity inside svelte:head
In HTML5, the<meta>tag is a void element and does not require a closing slash. It is valid to write it without/>.Step 2: Confirm usage of title and meta tags
Both<title>and<meta>tags are allowed inside<svelte:head>to modify the document head.Final Answer:
Nothing is wrong; this code is valid. -> Option AQuick Check:
HTML5 void tags don't need closing slash = A [OK]
- Adding unnecessary closing slash to <meta>
- Thinking <title> is disallowed in svelte:head
- Confusing XHTML rules with HTML5
desc. Which Svelte code correctly updates the meta description in the document head when desc changes?<script> let desc = 'Initial description'; </script> <!-- Choose the correct svelte:head usage -->
Solution
Step 1: Understand attribute binding in svelte:head
To dynamically set an attribute value, use curly braces without quotes around the variable, likecontent={desc}.Step 2: Check invalid options
uses quotes around {desc}, so it treats it as a literal string, not variable. {desc} incorrectly places content inside<meta>, which is a void tag and cannot have children. misses curly braces arounddesc.Final Answer:
<svelte:head> <meta name="description" content={desc}> </svelte:head> -> Option DQuick Check:
Use content={variable} without quotes for dynamic meta tags = A [OK]
- Putting quotes around {desc} making it a string
- Adding children inside <meta> tag
- Using incorrect tag closing for void elements
Ready to prove it?
Pass the test and this pattern moves unknown
