Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs added here will be added as <link>s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.

+ add another resource


Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource


Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.


Auto Save

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.


                <h1>Attach a Shadow DOM to a DIV</h1>

	This is Demonstration of attaching a Shadow DOM to a basic HTML Element.
 	Some of the elements point to a slot using the slot attribute.
<div id="demodiv" hidden>
	<h2 slot="heading">This heading slot is Navy</h2>
	<p slot="subheading">As defined in the Shadow DOM</p>
	<p>The Element.attachShadow() method attaches a shadow DOM tree to the specified element and returns a reference to its ShadowRoot. <br>This text is now purple.</p>
	<p>Note that you can attach a shadow root to basic HTML Elements. Not only Custom Elements.</p>

<p>This can also be done with a Declaritive Shadow DOM so that we don't need JavaScript at all.</p>
<p>Here is an example of a <a href="" target="_parent">Declarative Shadow DOM and a DIV</a></p>

	This is a template element that also contains slot elements.
 	The named slots are the targets of elements with slot attribute above. 
<template id="template">
		:host {
			margin-block-start: 1em;
			padding-block: 1ex;
			padding-inline: 3ch;
			color: purple;
			background: lightBlue;
		::slotted(h2) {
			color: navy;
		header {
			color: steelBlue;
			font-size: 1.3333rem;
			font-weight: 300;
		<slot name="subheading"></slot>
		<slot name="heading"></slot>




                // Get the DIV 
const div = document.getElementById("demodiv");

// Get the Template
const { content } = document.getElementById('template');

// Attach a shadowRoot to the DIV
const shadowRoot = div.attachShadow({ mode: "closed" });

// Insert the template into the shadowRoot

// Show the DIV