Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

Save Automatically?

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

              
                <html>
	<head>
		<script>
			// Check if there's any override. If so, let the markup know by setting an attribute on the <html> element
			const colorModeOverride = window.localStorage.getItem('color-mode');
			const hasColorModeOverride = typeof colorModeOverride === 'string';
			if (hasColorModeOverride) {
				document.documentElement.setAttribute('data-force-color-mode', colorModeOverride);
			}
		</script>
	</head>
<body>
	<h1>Dark Mode + Override (Vanilla JS) <a id="reset-darkmode" href="#" title="Clear Dark Mode Override"><small>(reset)</small></a></h1>
	<div>
		<input type="checkbox" id="toggle-darkmode" />
		<label for="toggle-darkmode"><span>Toggle Light/Dark Mode</span></label>
		<script>
			// Check the dark-mode checkbox if
			// - The override is set to dark
			// - No override is set but the system prefers dark mode
			if ((colorModeOverride == 'dark') || (!hasColorModeOverride && window.matchMedia('(prefers-color-scheme: dark)').matches)) {
				document.querySelector('#toggle-darkmode').checked = true;
			}
		</script>
	</div>
	<aside>
		<p>Some things to try:</p>
		<ol>
			<li>Change your system preferences. This site will adjust accordingly <em>(if no override is set)</em></li>
			<li>Click the 🔆/🌙 to override the system preferences</li>
			<li>Use this reset link at the top to clear the override <em>(shown only if an override is set)</em></li>
		</ol>
	</aside>
</body>
              
            
!

CSS

              
                /* Light Color Scheme */
:root,
:root[data-force-color-mode="light"] {
	color-scheme: light dark;
	--text-color: #000;
	--background-color: #fff;
	--link-color: #00f;
}

/* Dark Color Scheme */
@media (prefers-color-scheme: dark) {
	:root {
		--text-color: #fff;
		--background-color: #333;
		--link-color: #2196f3;
	}
}
:root[data-force-color-mode="dark"] {
	--text-color: #fff;
	--background-color: #333;
	--link-color: #2196f3;
}

/* Use the Color Scheme */
body {
	color: var(--text-color);
	background-color: var(--background-color);
}

a {
	color: var(--link-color);
}

/* Only show reset button if override is set */
#reset-darkmode {
	display: none;
	text-decoration: none;
	font-style: italic;
}
[data-force-color-mode] #reset-darkmode {
	display: initial;
}

/* Visually hide the label text and Inject Light/Dark Mode Icon (based on checkbox value) */
#toggle-darkmode {
	display: none;
}
#toggle-darkmode + label > span {
	position: absolute;
	overflow: hidden;
	clip: rect(0 0 0 0);
	height: 1px;
	width: 1px;
	margin: -1px;
	padding: 0;
	border: 0;
}
#toggle-darkmode + label:after {
	font-size: 4em;
	content: "🔆";
	cursor: pointer;
}
#toggle-darkmode:checked + label:after {
	content: "🌙";
}

body {
	font-family: Helvetica, Arial, sans-serif;
	margin: 0;
	padding: 0;
	height: 100vh;
	display: flex;
	flex-direction: column;
	align-items: center;
	justify-content: center;
}

              
            
!

JS

              
                /*
 * Demo for https://www.bram.us/2020/04/26/the-quest-for-the-perfect-dark-mode-using-vanilla-javascript/
 */

const setColorMode = (mode) => {
	// Mode was given
	if (mode) {
		// Update data-* attr on html
		document.documentElement.setAttribute('data-force-color-mode', mode);
		// Persist in local storage
		window.localStorage.setItem('color-mode', mode);
		// Make sure the checkbox is up-to-date
		document.querySelector('#toggle-darkmode').checked = (mode === 'dark');
	}
	
	// No mode given (e.g. reset)
	else {
		// Remove data-* attr from html
		document.documentElement.removeAttribute('data-force-color-mode');
		// Remove entry from local storage
		window.localStorage.removeItem('color-mode');
		// Make sure the checkbox is up-to-date, matching the system preferences
		document.querySelector('#toggle-darkmode').checked = window.matchMedia('(prefers-color-scheme: dark)').matches;
	}
}

document.querySelector('#toggle-darkmode').addEventListener('click', (e) => {
	setColorMode(e.target.checked ? 'dark' : 'light');
});

document.querySelector('#reset-darkmode').addEventListener('click', (e) => {
	e.preventDefault();
	setColorMode(false);
});

// Keep an eye out for System Light/Dark Mode Changes
const mediaQuery = window.matchMedia('(prefers-color-scheme: dark)');
mediaQuery.addListener(() => {
	// Ignore change if there's an override set
	if (document.documentElement.getAttribute('data-force-color-mode')) {
		return;
	}

	// Make sure the checkbox is up-to-date
 	document.querySelector('#toggle-darkmode').checked = mediaQuery.matches;
});

              
            
!
999px

Console