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

              
                <form>
	<fieldset>
		<legend>Tweak Light/Dark Mode Preferences</legend>
		<label for="os">Operating System</label>
		<select name="os" id="os">
			<option value="light">Light Mode</option>
			<option value="dark">Dark Mode</option>
		</select>
		<label for="browser">Browser (All Sites)</label>
		<select name="browser" id="browser">
			<option value="inherit">Inherit</option>
			<option value="light">Light Mode</option>
			<option value="dark">Dark Mode</option>
		</select>
		<label for="website">Current Website</label>
		<select name="website" id="website">
			<option value="inherit">Inherit</option>
			<option value="light">Light Mode</option>
			<option value="dark">Dark Mode</option>
		</select>
	</fieldset>
</form>

<div class="browser">
	<div class="topbar">
		<div class="windowbuttons">
			<button class="close"></button>
			<button class="minimize"></button>
			<button class="maximize"></button>
		</div>
		<div class="tabbar">
			<a class="tab active" href="#" data-page="bram.us" data-url="https://www.bram.us/">
				<span class="title">Bram.us</span>
				<button class="close">&times;</button>
			</a>
			<a class="tab" href="#" data-page="other.site" data-url="https://www.other.site/">
				<span class="title">Other.site</span>
				<button class="close">&times;</button>
			</a>
			<a class="tab" href="#" data-page="stubborn.site" data-url="https://www.stubborn.site/">
				<span class="title">Stubborn.site</span>
				<button class="close">&times;</button>
			</a>
		</div>
	</div>
	<div class="toolbar">
		<button>←</button>
		<button>β†’</button>
		<button>β€Ύ</button>
		<input class="omnibox" type="text" value="https://www.bram.us/" readonly />
		<button id="togglebutton">
			<span data-show-when="light">πŸŒ•</span>
			<span data-show-when="dark">πŸŒ‘</span>
		</button>
	</div>
	<div class="page active" data-page="bram.us">
		<h1>Welcome to Bram.us!</h1>
		<p>Choose color theme:</p>
		<ul class="picker">
			<li><a href="#" data-value="light" data-page="bram.us">Light</a></li>
			<li><a href="#" data-value="inherit" data-page="bram.us">System</a></li>
			<li><a href="#" data-value="dark" data-page="bram.us">Dark</a></li>
		</ul>
	</div>
	<div class="page" data-page="other.site">
		<h1>Welcome to Other.Site</h1>
		<p>I respond to light/dark but don’t offer a toggle myself</p>
	</div>
	<div class="page" data-page="stubborn.site">
		<h1>Welcome to Stubborn.Site</h1>
		<p>I don’t take your preference into account 😈</p>
	</div>
</div>
<footer>
	<p>Demo for <a href="https://brm.us/browser-dark-mode-toggle" target="_top">https://brm.us/browser-dark-mode-toggle</a><br />This demo requires <code>:has()</code> to work</p>
</footer>
              
            
!

CSS

              
                :root {
    color-scheme: light; /* This demo page itself only light for demonstration purposes */
}

body {
    display: grid;
    place-content: center;
    gap: 2em;
	margin-top: 2em;
}

.browser[data-browser-theme="light"] {
	--topbar-background: #DFE1E5;
	--tabbar-tab-active-background: white;
	--tabbar-tab-active-foreground: black;
	--tabbar-tab-hover-background: #EBECEF;
	--toolbar-background: var(--tabbar-tab-active-background);
	--toolbar-foreground: var(--tabbar-tab-active-foreground);
	--toolbar-button-hover-background: #ECEDEF;
	--omnibox-background: #ECEDEF;
	--omnibox-foreground: black;
}
.browser[data-browser-theme="dark"] {
	--topbar-background: #202325;
	--tabbar-tab-active-background: #323639;
	--tabbar-tab-active-foreground: #EDEDED;
	--tabbar-tab-hover-background: #323639;
	--toolbar-background: var(--tabbar-tab-active-background);
	--toolbar-foreground: var(--tabbar-tab-active-foreground);
	--toolbar-button-hover-background: #282C2F;
	--omnibox-background: #282C2F;
	--omnibox-foreground: #EDEDED;
}

.browser[data-website-theme="light"] {
	--website-main: black;
	--website-background: white;
	--link-color: blue;
}
.browser[data-website-theme="dark"]:not(.browser:has(div.page.active[data-page="stubborn.site"])) {
	--website-main: white;
	--website-background: #222;
	--link-color: lightgreen;
}

.browser button {
    border: none;
	display: block;
	width: 2em;
    aspect-ratio: 1;
    border-radius: 50%;
    background: transparent;
	color: inherit;
}

.browser {
    border-radius: 0.5em;
    display: grid;
    grid-template-rows: auto auto 1fr;
    min-height: 10em;
    min-width: 25em;
	height:  20em;
	width: 40em;
    overflow: hidden;
	
	/* Nice Shadow: https://www.joshwcomeau.com/css/designing-shadows/ */
    box-shadow:
      0 1px 1px hsl(0deg 0% 0% / 0.075),
      0 2px 2px hsl(0deg 0% 0% / 0.075),
      0 4px 4px hsl(0deg 0% 0% / 0.075),
      0 8px 8px hsl(0deg 0% 0% / 0.075),
      0 16px 16px hsl(0deg 0% 0% / 0.075)
    ;
	
	resize: both;
}

.topbar, .toolbar {
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif, "Apple Color Emoji", "Segoe UI Emoji", "Segoe UI Symbol";
	user-select: none;
}

input {
    font-family: inherit;
}

.topbar {
    display: flex;
    gap: 1em;
    padding: 0 1em;
    background-color: var(--topbar-background);
}

.windowbuttons {
    display: flex;
    align-items: center;
    gap: 0.25em;
}

.windowbuttons button {
	width: 1em;
}

.windowbuttons button.close {
    background: #EC6A5E;
}
.windowbuttons button.minimize {
    background: #F4BF4F;
}
.windowbuttons button.maximize {
    background: #61C454;
}

.tabbar {
    flex: 1;
    display: flex;
    gap: 0.25em;
}

.tabbar .tab {
    align-self: flex-end;
    padding: 0.25em 0.5em 0.25em 0.75em;
    margin-top: 0.4em;
    border-top-left-radius: 0.4em;
    border-top-right-radius: 0.4em;

    text-decoration: none;

    min-width: 8em;
    display: flex;
	align-items: center;
    justify-content: space-between;
    color: var(--tabbar-tab-active-foreground);
}

.tabbar .tab:hover {
    background: var(--tabbar-tab-hover-background);
}

.tabbar .tab.active {
    background: var(--tabbar-tab-active-background);
}

.tabbar .tab .close:hover {
    background: rgba(0 0 0 / 0.15);
}

.tabbar .tab .title {
	font-size: 0.9em;
}

.toolbar {
    display: flex;
    gap: 0.25em;
    padding: 0.25em 0.25em;
    border-bottom: 1px solid rgba(0 0 0 / 0.25);
	background: var(--toolbar-background);
	color: var(--toolbar-foreground);
}

.toolbar button:hover {
    background-color: var(--toolbar-button-hover-background);
}

.toolbar input {
    flex: 1;
    border: none;
    background: var(--omnibox-background);
	color: var(--omnibox-foreground);
    border-radius: 10em;
    padding: 0 1em;
}

.page {
    padding: 1em;
	color: var(--website-main);
	background: var(--website-background);
    display: none;
}

.page.active {
    display: block;
}

.page h1 {
    margin: 0;
    padding: 0;
}

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

fieldset {
	display: grid;
	gap: 0.5em;
	grid-template-columns: 1fr 2fr;
}

footer {
	text-align: center;
}

[data-show-when] {
	display: none;
}

[data-website-theme="light"] [data-show-when="light"],
[data-website-theme="dark"] [data-show-when="dark"] {
	display: block;
}

[data-website-value="light"] [data-value="light"]::before,
[data-website-value="inherit"] [data-value="inherit"]::before,
[data-website-value="dark"] [data-value="dark"]::before {
	content: 'βœ” ';
}

footer {
    font-style: italic;
}

#togglebutton {
	position: relative;
}
.browser #togglebutton::before {
	position: absolute;
	left: 50%;
	bottom: 0;
	translate: -50% 0%;
	background: #333;
	color: white;
	height: 1em;
	padding: 0.2em 0.2em;
	font-size: 50%;
	border-radius: 0.5em;
}
[data-website-theme="light"] #togglebutton::before {
	content: 'LIGHT';
}
[data-website-theme="dark"] #togglebutton::before {
	content: 'DARK';
}
form:has(#website option[value="inherit"]:checked) + .browser #togglebutton::before {
	content: 'AUTO';
}

.page {
	position: relative;
}
.page::after {
	position: absolute;
	right: 0.75em;
	top: 0.25em;
	content: ' Try me ‴';
	color: red;
	font-family: 'Permanent Marker', cursive;
	transform-origin: 100% 100%;
	rotate: -3.5deg;
}

/* form:has(#website option[value="inherit"]:checked) + .browser #togglebutton::before {
	content: 'πŸŒ“';
}
form:has(#website option[value="inherit"]:checked) + .browser #togglebutton [data-show-when] {
	display: none;
} */
              
            
!

JS

              
                document.querySelector('form').addEventListener('submit', (e) => {
    e.preventDefault();
});

const $osSetting = document.querySelector('#os');
const $browserSetting = document.querySelector('#browser');
const $siteSetting = document.querySelector('#website');

let activeSite = 'bram.us';
const siteSettings = {
    'bram.us': 'inherit',
    'other.site': 'inherit',
	'stubborn.site': 'inherit',
};

const getThemeToApplyOnSite = () => {
	if (['light', 'dark'].includes(siteSettings[activeSite])) {
		return siteSettings[activeSite];
	}

	if (['light', 'dark'].includes($browserSetting.value)) {
		return $browserSetting.value;
	}
	
	return $osSetting.value;
}

const getThemeToApplyOnBrowserUI = () => {
	const $osSetting = document.querySelector('#os');
	return $osSetting.value;
}

const $browser = document.querySelector('.browser');
const syncSettings = () => {
	$browser.setAttribute('data-browser-theme', getThemeToApplyOnBrowserUI());
	$browser.setAttribute('data-website-theme', getThemeToApplyOnSite());
	$browser.setAttribute('data-website-value', document.getElementById('website').value);
}

window.addEventListener('load', (e) => {
	syncSettings();
});

$siteSetting.addEventListener('change', (e) => {
    siteSettings[activeSite] = $siteSetting.value;
});

document.querySelectorAll('select').forEach(($dropdown) => {
	$dropdown.addEventListener('change', (e) => {
		syncSettings();
	});
});

document.querySelectorAll('.tab').forEach(($tab) => {
    $tab.addEventListener('pointerdown', (e) => {
        // Deactivate current tab
        document.querySelector('.tab.active').classList.remove('active');
        document.querySelector('.page.active').classList.remove('active');

        // Activate tab and page for new site
        activeSite = e.currentTarget.getAttribute('data-page');
        document.querySelector(`.tab[data-page="${activeSite}"]`).classList.add('active');
        document.querySelector(`.page[data-page="${activeSite}"]`).classList.add('active');

        // Update site dropdown to use stored value
        $siteSetting.value = siteSettings[activeSite];

        // Update URLbar
        document.querySelector('.toolbar input').value = e.currentTarget.getAttribute('data-url');

        // Rerender
        syncSettings();
    });
});

document.querySelectorAll('.picker a').forEach(($a) => {
	$a.addEventListener('click', (e) => {
		e.preventDefault();
		document.querySelector('#website').value = e.target.getAttribute('data-value');
		siteSettings[activeSite] = e.target.getAttribute('data-value');
		syncSettings();
	});
});

document.querySelector('#togglebutton').addEventListener('pointerdown', (e) => {
	// if (siteSettings[activeSite] == 'inherit') {
	// 	siteSettings[activeSite] = getThemeToApplyOnSite() == 'dark' ? 'light' : 'dark';
	// } else {
	// 	siteSettings[activeSite] = 'inherit';
	// }
	
	// Flip theme
	siteSettings[activeSite] = getThemeToApplyOnSite() == 'dark' ? 'light' : 'dark';
	
	// Switch back to inherit if flipped value results into that
	if ($browserSetting.value === siteSettings[activeSite]) {
		siteSettings[activeSite] = 'inherit';
	}
	if (($browserSetting.value === 'inherit') && ($osSetting.value === siteSettings[activeSite])) {
		siteSettings[activeSite] = 'inherit';
	}
		
	// Update site dropdown to use stored value
    $siteSetting.value = siteSettings[activeSite];
	
	// Rerender
    syncSettings();
});
              
            
!
999px

Console