0Pricing
HTML Academy · Lesson

Declarative Shadow DOM

Attach shadow DOM from HTML without JavaScript using shadowrootmode.

The Problem It Solves

Traditional Shadow DOM is created via JavaScript (attachShadow). For server-rendered pages, this means the shadow tree is empty until JS runs — defeating SSR's purpose of delivering complete HTML on first byte. Declarative Shadow DOM fixes this.

The shadowrootmode Attribute

A <template> with shadowrootmode="open" declares a shadow root in HTML at parse time. The browser attaches the shadow root to the parent element automatically; the template's content becomes the shadow tree. No JavaScript required.

<my-card>
  <template shadowrootmode="open">
    <style>:host { display: block; padding: 16px; }</style>
    <slot></slot>
  </template>
  <p>This goes into the slot</p>
</my-card>

All lessons in this course

  1. Declarative Shadow DOM
  2. The Popover API
  3. The selectlist Element
  4. Interest Invokers and Command Attribute
← Back to HTML Academy