Pen Settings

HTML

CSS

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

JavaScript

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

Packages

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.

Behavior

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.

HTML

              
                <main>
	<h1>Web Components - Custom Properties (Not Attributes)</h1>

	<!-- TODO: How can you set the name via a DOM attribute? -->
	<x-component></x-component>
	
	<script>
		// You can NOT set properties on a component BEFORE it's been defined.
		// document.querySelector('x-component').name = 'Example 1';
	</script>
</main>

              
            
!

CSS

              
                html {
	box-sizing: border-box;
}

*,
::after,
::before {
	box-sizing: inherit;
}

body {
	margin: 1rem auto 3rem;
	line-height: 1.4;
	tab-size: 4;
}

main {
	max-width: 42rem;
	margin: 0 auto;
	padding-inline: 1rem;
}

pre {
	padding: 1rem;
	overflow: auto;
	border: 1px solid #ccc;
}

/* ul li { margin: 1rem 0; } */
/* ul li li { margin: 0.5rem 0; } */

/* Colors */
.colors > *:nth-child(7n + 1) { background-color: red; }
.colors > *:nth-child(7n + 2) { background-color: orange; }
.colors > *:nth-child(7n + 3) { background-color: yellow; }
.colors > *:nth-child(7n + 4) { background-color: green; }
.colors > *:nth-child(7n + 5) { background-color: blue; }
.colors > *:nth-child(7n + 6) { background-color: indigo; }
.colors > *:nth-child(7n + 7) { background-color: violet; }

              
            
!

JS

              
                // Make a new dynamic template.
const template = document.createElement('template');
template.innerHTML = `
	<style>
		/* We can add styles inside our template. */
		div {
			padding: 1rem;
			border: 1px solid black;
			border-radius: 1rem;
			background: lightgray;
		}
	</style>
	<!-- And any other HTML we want too. -->
	<div>
		<!--
			To render dynamic "slotted" content,
			add a slot element:
		-->
		This component's name is "<span>empty</span>".
	</div>
`;

class XComponent extends HTMLElement {
	set name(value) {
		this._name = value;
		this.nameElement.innerText = this._name;
	}
	
	get name() {
		return this._name;
	}
	
	constructor() {
		super();
		
		// Slots require the "Shadow DOM" API.
		// Shadow DOM also encapsulates CSS (and other cool features).
		// attachShadow() creates a shadow root instance.
		this.attachShadow({ mode: 'open' });
		
		// Instead of simply appending to our component's HTML,
		// now we append to its shadow root...
		this.shadowRoot.appendChild(template.content.cloneNode(true));
		
		// Now I can also set child nodes to named properties.
		this.nameElement = this.shadowRoot.querySelector('span');
	}
}

customElements.define('x-component', XComponent);

// Dynamically set the name for our component.
document.querySelector('x-component').name = 'Example 2';

              
            
!
999px

Console