This interactive demo explores the practical application of Shadow DOM encapsulation and advanced styling techniques for Web Components, demonstrating:
open
and closed
modesExperiment with the controls below to see how these techniques work in practice!
Compare how external styles affect Shadow DOM components versus regular DOM elements:
This is a regular DOM element with class "card". External styles affect it.
Notice how the !important
styles from the global stylesheet are applied to this component.
This component uses Shadow DOM encapsulation. External styles don't affect it.
Notice how the !important
styles don't pierce the shadow boundary, protecting this component.
<styled-card variant="primary" title="My Card Title">
<p>Card content goes here</p>
<button>Card Button</button>
</styled-card>
These components demonstrate how Web Components can define variants and adapt to different contexts:
<styled-card
variant="primary|secondary|neutral"
title="Card Title">
<!-- Optional content -->
</styled-card>
This component detects it's in a "sidebar" context.
This component detects it's in a "main" context.
<!-- The component inherits context from a parent element -->
<div data-context="sidebar">
<context-card title="Sidebar Context Card">
<p>Card content goes here</p>
</context-card>
</div>
<!-- Or with direct context on the component -->
<context-card
data-context="main"
title="Main Context Card">
<p>Card content goes here</p>
</context-card>
See how Shadow DOM protects components from aggressive legacy styles:
This section simulates a legacy system with aggressive styling that would normally break components.
This regular DOM component is affected by the legacy styles.
This component is protected from the legacy styles by Shadow DOM encapsulation.
<!-- Component with all available options -->
<styled-card
variant="primary"
title="Card with Options"
data-context="sidebar">
<p>This card combines variant and context features</p>
<button>Action Button</button>
</styled-card>