HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
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.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
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.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
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.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<h1>System-Mirroring Dark Mode with Persisted Overrides</h1>
<p>This page will be light or dark based on the user’s system preferences until overridden with site-specific preference (either <code>light</code> or <code>dark</code>). CSS-wise, the change is made by adding a <code>theme--dark</code> class to <code><html></code>.</p>
<p>Locally saved user preference: <output class="badge" id="theme_preference" style="text-transform: uppercase">…<noscript> We really need JavaScript</noscript></output></p>
<p>
<button id="light">Light</button>
<button id="system">System</button>
<button id="dark">Dark</button>
</p>
<p>JavaScript is required for dark mode, even though it can be done with pure CSS. (It is currently not possible to have manual overrides <em>and</em> system-mirroring dark mode without one of: JavaScript, duplicated CSS, or multiple stylesheets. If I am wrong, <a href="https://alanhogan.com/contact?from=https://codepen.io/alanhogan/pen/PoEgwxL&reason=removing%20duplication%20without%20losing%20CSS-only%20automatic%20dark%20mode">let me know</a>.)</p>
<p><a href="https://alanhogan.com/code/modern-html-template">Based on the modern HTML template</a> by <a href="https://alanhogan.com/">Alan Hogan</a></p>
<fieldset>
<legend>Unstyled Elements</legend>
<input type=checkbox />
<input value="" />
<input type=submit value=Go />
</fieldset>
<div class=scroll>
This box is intended to
show how scrollbars look,
which may be affected by the
<code>color-scheme</code>
CSS property.
<br>
<br>
Exciting, I know.
</diV>
button.current {
background-color: var(--page-bg-color);
color: var(--text-color);
border-color: var(--page-bg-color);
border-style: solid;
font-weight: 600;
filter: contrast(0.8);
}
button {
min-width: 6.5em;
border-radius: 0.2em;
}
button:not(.current) {
color: var(--page-bg-color);
background-color: var(--link-color);
border-color: var(--link-color);
}
button:not(:disabled) {
cursor: pointer;
}
.scroll {
margin: 1.6em 0;
padding: 0.6em;
border: 1px solid currentColor;
max-width: 12em;
max-height: 4em;
overflow: auto;
}
/* Automatic dark mode, system fonts, and readable max-width
* via https://ajh.us/template */
:root {
--page-bg-color: white;
--text-color: #222;
--link-color: #06d;
--visited-link-color: #91e;
--code-color: #456658;
--badge-bg: #eee;
--badge-text-color: #444;
color-scheme: light;
}
.theme--dark:root {
--page-bg-color: #141414;
--text-color: #eee;
--link-color: #2af;
--visited-link-color: #c5f;
--code-color: #c5eddc;
--badge-bg: #252525;
--badge-text-color: #ccc;
color-scheme: dark;
}
* {
box-sizing: border-box;
}
html {
margin: 0;
padding: 0;
}
html,
body,
input,
button,
textarea {
font-family: system-ui, sans-serif;
}
body {
padding: calc(0.5em + 0.5vmin);
margin: 0 auto;
max-width: 40em;
background-color: var(--page-bg-color);
color: var(--text-color);
}
:link {
color: var(--link-color);
}
:visited {
color: var(--visited-link-color);
}
code,
tt,
kbd,
pre {
font-family: "JetBrains Mono", "IBM Plex Mono", "Source Code Pro",
"Cascadia Code", ui-monospace, Menlo, "Ubuntu Monospace", "Inconsolata",
"Fira Code", "Deja Vu Sans Mono", "Bitstream Vera Sans Mono", Monaco,
"Segoe UI Mono", "Roboto Mono", "Oxygen Mono", Consolas, monospace;
font-size: 90%;
color: var(--code-color);
}
pre code,
pre tt,
pre kbd {
font-size: 100%;
}
.badge {
font-size: 80%;
font-weight: 500;
display: inline-block;
padding: 0.05em 0.4em;
background: var(--badge-bg);
color: var(--badge-text-color);
border-radius: 0.2em;
text-decoration: none;
position: relative;
bottom: 0.1em;
}
// Constants
const themes = { system: "system", dark: "dark", light: "light" };
const themeMediaQuery = matchMedia("(prefers-color-scheme: dark)");
// Helpers
const getThemePreference = () => localStorage.getItem("themePreference");
// State
const themeSettings = {
system: themeMediaQuery.matches ? themes.dark : themes.light,
preference: getThemePreference() || themes.system
};
function calculateTheme(settings) {
if (settings.preference == themes.system) {
return settings.system;
} else if (settings.preference == themes.dark) {
return themes.dark;
} else return themes.light;
}
function applyTheme(theme) {
if (theme === themes.dark) {
document.documentElement.classList.add("theme--dark");
} else {
document.documentElement.classList.remove("theme--dark");
}
}
function recalculateAndApplyTheme() {
applyTheme(calculateTheme(themeSettings));
}
function initializeThemes() {
recalculateAndApplyTheme();
themeMediaQuery.addEventListener("change", (e) => {
themeSettings.system = e.matches ? themes.dark : themes.light;
recalculateAndApplyTheme();
});
}
// Should run this in document.head() to avoid flashes of incorrect theme.
initializeThemes();
///////////////////////////////////////////////
/// BELOW THIS LINE CAN BE AFTER BODY/LOAD. ///
///////////////////////////////////////////////
// DOM HELPERS ----------
const qs = (s) => document.querySelector(s);
// USER PREFERENCE UPDATING -------
const setThemePreference = (pref) => {
if (!themes.hasOwnProperty(pref)) {
console.error("Invalid theme", pref, "not found in", themes.keys());
} else {
themeSettings.preference = pref;
localStorage.setItem("themePreference", pref);
recalculateAndApplyTheme();
}
};
// HOOKING UP THE BUTTONS ----------
var showCurrentPreference = () => {
const curPref = getThemePreference();
qs("#theme_preference").innerText = curPref || "Never Set";
const oldBtn = qs(".current");
if (oldBtn) {
oldBtn.classList.remove("current");
oldBtn.disabled = false;
}
if (themes[curPref]) {
const newBtn = qs(`#${curPref}`);
newBtn.classList.add("current");
newBtn.disabled = true;
}
};
qs("#light").addEventListener("click", (e) => {
setThemePreference(themes.light);
showCurrentPreference();
});
qs("#system").addEventListener("click", (e) => {
setThemePreference(themes.system);
showCurrentPreference();
});
qs("#dark").addEventListener("click", (e) => {
setThemePreference(themes.dark);
showCurrentPreference();
});
// End button hook-ups
// Show current:
showCurrentPreference();
// TODO: Listen to event for theme preference changes to update the buttons.
Also see: Tab Triggers