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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

              
                <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <link rel="stylesheet" href="styles.css">
    <script src="scripts.js" defer></script>
</head>
<body>
    <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" inert>
        <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>
            </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>
        </div>
        <div class="page" data-page="other.site">
            <h1>Welcome to Other.Site</h1>
        </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></p>
    </footer>
</body>
</html>
              
            
!

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;
}
.browser[data-website-theme="dark"] {
	--website-main: white;
	--website-background: #222;
}

.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: 35em;
    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;
}

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;
}

footer {
    font-style: italic;
}
              
            
!

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',
};

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());
}

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('click', (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();
    });
});
              
            
!
999px

Console