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 - Getting Started</h1>
	<x-component name="Example 3"></x-component>
</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

              
                const template = document.createElement('template');
template.innerHTML = `
	<style>
		div {
			padding: 1rem;
			border: 1px solid black;
			border-radius: 1rem;
			background: lightgray;
		}
	</style>
	<div>
		This component's name is "<span>empty</span>".
	</div>
`;

class XComponent extends HTMLElement {
	// To read attribute values, provide a list of your
	// attribute names to the observedAttributes() function.
	static get observedAttributes() {
		return ['name'];
	}

	// You can still provide an option to dynamically update
	// your attribute using JavaScript. NOTE: This requires
	// attributeChangedCallback() to actually sync together.
	set name(value) {
		this._name = value;
		this.nameElement.innerText = this._name;
	}
	
	get name() {
		return this._name;
	}
	
	constructor() {
		super();
		this.attachShadow({ mode: 'open' });
		this.shadowRoot.appendChild(template.content.cloneNode(true));

		// Keep track of element references.
		this.nameElement = this.shadowRoot.querySelector('span');
	}
	
	// Handle any changes to attribute values.
	attributeChangedCallback(attrName, oldValue, newValue) {
		if (attrName === 'name') {
			this.name = newValue;
		}
	}
}

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

              
            
!
999px

Console