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

              
                <H1>Color Contrast &amp; Mix</h1>

<p>Here we see several Web Components that are identical and have one property we pass on to a single custom property in CSS.</p>
<p>This property sets the text color, and only the text color.
	The color contrast function looks at the <code>--text-color</code> and compares this to a set of colors.
	For this I've created three sets of colors (primary, secondary and tertiary) and a contrast color for each set.</p>
<p>There are two mixed colors that each use two of the contrast color sets. And these two mixed values create the gradient that is applied to the web component. Also one mixed color is used to set the background color surrounding the gradient. </p>
<p><strong>Note</strong>: This is highly experimental and is subject to change. You can view this pen in Safari TP with color-contrast enabled and in Chrome with experimental web features enabled.</p>

<test-card data-color="blue"></test-card>
<test-card data-color="yellow"></test-card>
<test-card data-color="purple"></test-card>
<test-card data-color="pink"></test-card>
<test-card data-color="black"></test-card>
<test-card data-color="white"></test-card>

<template>
	<style>
		@import "https://unpkg.com/open-props";
		@import "https://unpkg.com/open-props/normalize.min.css";

		/* Properties */
		:host {
			--black: black;
			--white: white;
			/* Primary */
			--red: var(--red-9, red);
			--yellow: var(--yellow-4, yellow);
			--blue: var(--blue-7, blue);
			/* Secondary */
			--orange: var(--orange-8, orange);
			--green: var(--green-9, green);
			--purple: var(--violet-9, purple);
			/* Tertiary */
			--pink: var(--pink-4, pink);
			--teal: var(--teal-6, teal);
			--grape: var(--grape-9, grape);
			/* Note: incorrect references in a list of colors invalidates the entire list. */
			--primary-list: var(--red), var(--yellow), var(--blue);
			--secondary-list: var(--orange), var(--green), var(--purple);
			--tertiary-list: var(--pink), var(--teal), var(--grape);
			--contrast-01: color-contrast(var(--text-color) vs var(--primary-list) to AAlarge);
			--contrast-02: color-contrast(var(--text-color) vs var(--secondary-list));
			--contrast-03: color-contrast(var(--text-color) vs var(--tertiary-list));
			--mix-01: color-mix(in oklch,
					var(--contrast-01) 66.6666%,
					var(--contrast-02));
			--mix-02: color-mix(in oklch,
					var(--contrast-02) 66.6666%,
					var(--contrast-03));
			--gradient: linear-gradient(var(--contrast-01), var(--contrast-02));
			--background-color: var(--contrast-01);
			--card-display: grid;
			--card-columns: 1fr;
			--card-padding: var(--size-3, 1rem);
		}

		@supports (color: color-mix(in oklch, yellow 50%, navy)) {
			:host {
				--mix-01: color-mix(in oklch,
						var(--contrast-01) 66.6666%,
						var(--contrast-02));
				--mix-02: color-mix(in oklch,
						var(--contrast-02) 66.6666%,
						var(--contrast-03));
				--gradient: linear-gradient(320deg, var(--mix-02) 10%, var(--mix-01) 100%);
				--background-color: var(--mix-02);
			}
		}

		/* Rules */
		:host {
			display: block;
			padding-block: var(--multicol-gap, 0);
			break-inside: avoid;
			container: component / inline-size;
			flex: 1;
			min-width: min(20rem, 100%);
			color: var(--text-color);
			background-color: var(--background-color);
		}

		article {
			margin: 3rem;
			display: var(--card-display);
			grid-template-columns: var(--card-columns);
			gap: var(--gap);
			padding: var(--card-padding);
			background: var(--toggle-gradient, var(--gradient));
			color: var(--text-color);
		}

		h2 {
			margin: 0;
			color: var(--text-color);
		}

		/* Overrides */
		article:has(h2) {
			--card-padding: var(--size-7, 2rem);
		}

		@container component (min-width: 35em) {
			article {
				--card-columns: 1fr 1fr;
			}
		}
	</style>
	<article>
		<h2></h2>
		<p>Lorem ipsum dolor sit amet consectetur adipisicing elit. Tempore ducimus autem illo quidem nobis aperiam cupiditate quia excepturi quos.</p>
	</article>
</template>
              
            
!

CSS

              
                h1,
p {
	margin: 0.5em 4rem;
}
p:last-of-type {
	margin-block-end: 3rem;
}
code,
pre {
	display: inline;
}

              
            
!

JS

              
                function markup() {
	const template = document.querySelector("template");
	const h2 = template.content.querySelector("h2");
	h2.innerText = this.color;
	console.log(template.content);
	return template.content.cloneNode(true);
}

class TestCard extends HTMLElement {
	constructor() {
		super();
		this.attachShadow({ mode: "open" });
		this.compTitle = this.dataset?.compTitle || ``;
		this.color = this.dataset?.color || ``;
		this.style = `--text-color:var(--${this.color})`;
		this.shadowRoot.appendChild(markup.call(this));
	}
}
customElements.define("test-card", TestCard);

              
            
!
999px

Console