DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
← From Beginner to Expert in Fundamentals of Frontend
  • Semantic HTML
  • Accessibility (a11y)
  • Forms & Validation
  • Document Metadata & The <head>
  • The Document Object Model (DOM)
  • Character Encoding & Entities
  • Data Attributes & Custom Data
  • Template, Slot & Web Components
  • Responsive Images: Picture, Source & srcset
  • Loading Strategies & Resource Hints
  • The Native Dialog Element
  • Audio, Video & Media APIs
  • SVG Integration & Canvas Basics
  • Advanced Input Types & Datalist
  • Details, Summary, Progress & Meter
  • Global Attributes & Iframe Security
  • Microdata, Schema.org & Structured Data
  • Open Graph & Social Meta Tags
  • Security Headers & CSP in HTML
  • Link Relations & Canonical Tags
← From Beginner to Expert in Fundamentals of Frontend

Template, Slot & Web Components

20 min read
templateslotWeb ComponentsCustom ElementsShadow DOMShadow Parts

Web Components: Framework-Free Components

Web Components are a set of browser-native APIs that let you create reusable, encapsulated custom elements — no React or Vue required.

The Three Pillars

  1. Custom Elements: Define your own HTML tags like <user-card>
  2. Shadow DOM: Encapsulate styles so they do not leak in or out
  3. HTML Templates: Define markup that is not rendered until cloned

The <template> Element

Content inside <template> is parsed but NOT rendered. It exists as a DocumentFragment you can clone and insert.

Slots: Content Projection

<slot> elements define where consumer content should appear inside a shadow DOM. Named slots allow multiple insertion points.

Shadow DOM Encapsulation

  • Styles inside shadow DOM do NOT affect the rest of the page
  • External styles do NOT affect shadow DOM content
  • JavaScript querySelector from outside cannot reach into shadow DOM
  • ::part() CSS pseudo-element allows controlled external styling

Lifecycle Callbacks

CallbackWhen
connectedCallbackElement added to DOM
disconnectedCallbackElement removed from DOM
attributeChangedCallbackAn observed attribute changes
adoptedCallbackElement moved to new document

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.