A useful interface does not need to be complicated. Start with native HTML, then add JavaScript only when you need it.
What You Need to Know
Named slots let a component place different pieces of page-supplied content into specific positions. A default slot receives content without an explicit slot name.
Try the Example
Take a look at the example. We’ll explain the important lines right below it.
<student-card>
<template shadowrootmode="open">
<h2><slot name="name">Unknown student</slot></h2>
<p><slot>About this student</slot></p>
</template>
<span slot="name">Alex</span>
<span>Learning HTML</span>
</student-card>
Let’s Read the Code
- slot: Slot assigns content to a matching named slot.
- <template>: A template stores markup that is inactive until it is cloned or used as declarative shadow content.
- <h2>: A heading divides the page into understandable parts.
- shadowrootmode: Shadowrootmode lets an HTML parser create a declarative shadow root.
Read the named elements and attributes together. They explain how the browser connects this feature to the rest of the page.
What Happens in the Browser?
When this example includes JavaScript, it supplies behavior that ordinary HTML does not automatically provide. Always think about a fallback.
Start with native behavior. Details, dialog, and popover each solve a different interface problem. Choose the simplest control that actually matches what a visitor wants to do, then test focus and dismissal as well as appearance.
Your Turn
Run this on a local development server if it loads modules or resources. Change the visible text or trigger and check what still works with scripts disabled.
Common Beginner Mistakes
- Assuming new HTML syntax works in every browser without testing.
- HTML supplies structure; JavaScript is necessary for the scripted behavior in this example. Check keyboard access and focus movement too.
Key Takeaways
- Named slots let a component place different pieces of page-supplied content into specific positions.
- slot: Slot assigns content to a matching named slot.
- Test the behavior you expect rather than trusting how the snippet looks in your editor.