Introduction to <slot>tag

The <slot>tag is a powerful HTML element primarily used within Web Components. It acts as a placeholder for dynamic content, allowing developers to define and inject custom markup into the shadow DOM of a web component. By enabling this functionality, the tag facilitates the creation of reusable, encapsulated components that can seamlessly integrate user-defined content into their structure.

Comparison with the <template>Tag

While the <slot>tag is used for injecting content into Web Components, the <slot>tag is designed to define HTML content that remains inactive until explicitly rendered. They complement each other in scenarios where both reusable structure and dynamic content are required. The <slot>tag is an essential tool for building robust and modular web applications, offering both flexibility and control in designing user interfaces. Its ability to separate component logic from user-defined content ensures scalability and maintainability, making it indispensable in modern web development.

Copy to clipboard
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Custom Card Example - Levoric Learn</title>
<style>
body {
margin: 0;
padding: 0;
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
background-color: #f5f5f7;
font-family: "Arial, sans-serif";
}
</style>
</head>
<body>
<template id="card-template">
<style>
:host {
display: block;
margin: 0 auto;
}
.card {
border: 1px solid #e0e0e0;
border-radius: 12px;
padding: 20px;
width: 340px;
background: #ffffff;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.card:hover {
cursor: pointer;
box-shadow: 0 8px 24px rgba(0, 0, 0, 0.2);
}
.card header {
font-size: 1.6em;
font-weight: bold;
color: #2c3e50;
margin-bottom: 16px;
text-align: center;
}
.card main {
font-size: 1em;
color: #34495e;
line-height: 1.6;
margin-bottom: 20px;
text-align: justify;
}
.card footer {
font-size: 0.9em;
color: #7f8c8d;
text-align: center;
padding-top: 12px;
border-top: 1px solid #ecf0f1;
}
</style>
<div class="card">
<header><slot name="title">Levoric Learn Card</slot></header>
<main><slot name="content">This card is styled for Levoric Learn, showcasing clean, professional content placement with a focus on clarity and aesthetics.</slot></main>
<footer><slot name="footer">© 2025 Levoric Learn</slot></footer>
</div>
</template>

<script>
class CardComponent extends HTMLElement {
constructor() {
super();
const template = document.getElementById("card-template").content;
const shadowRoot = this.attachShadow({ mode: "open" });
shadowRoot.appendChild(template.cloneNode(true));
}
}
customElements.define("custom-card", CardComponent);
</script>

<custom-card>
<span slot="title">Levoric Learn Title</span>
<p slot="content">Welcome to the Levoric Learn platform. This card demonstrates a professional, user-friendly component for tutorials, guides, and more.</p>
<small slot="footer">© 2025 Levoric Learn Platform</small>
</custom-card>
</body>
</html>

Global Attributes

AttributesDescription
accesskeySpecifies a shortcut key to activate or focus an element.
autocapitalizeControls whether and how text input is automatically capitalized.
classSpecifies one or more class names for the element, allowing CSS styling and JavaScript manipulation.
contenteditableSpecifies whether the content of an element is editable or not.
contextmenuSpecifies a context menu for the element.
dirDefines the text direction. Possible values are "ltr" (left-to-right), "rtl" (right-to-left), and "auto".
draggableSpecifies whether an element is draggable. Possible values are "true" or "false".
enterkeyhintSpecifies the action label or icon to be shown for the enter key on virtual keyboards.
hiddenIndicates that the element is not yet, or is no longer, relevant. The browser does not display elements that have the `hidden` attribute set.
idSpecifies a unique identifier for the element.
inputmodeProvides a hint to browsers for which virtual keyboard configuration to use when editing this element or its descendants.
isAllows you to specify the type of custom element.
langSpecifies the language of the element's content.
nonceA cryptographic nonce ("number used once") that can be used by Content Security Policy (CSP) to determine whether or not a given fetch will be allowed to proceed.
partSpecifies the element’s part in the shadow DOM.
slotAssigns a slot in a shadow DOM shadow tree.
spellcheckIndicates whether the element is subject to spell checking.
styleProvides inline CSS styling for the element.
tabindexSpecifies the tab order of the element.
titleAdds extra information about the element, displayed as a tooltip when hovering over the element.
translateSpecifies whether the content of the element should be translated. Possible values are "yes" or "no".
HTML AttributesGlobal AttributesEvent Attributes
To view the full list
To view the full list
To view the full list
elementsChrome BrowsersMicrosoft Edge BrowserFirefox BrowsersSafari BrowserOpera Browser
<small>YesYesYesYesYes