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.
<main data-theme="triad" data-colors>
<h1>Cascading Color Palette</h1>
<menu>
<label for="theme-select">
<span class="label">theme:</span>
<select name="theme-select" id="theme-select">
<option value="default-pink">default-pink</option>
<option value="default-blue">default-blue</option>
<option value="black">black & white</option>
<option value="complement">complement</option>
<option value="triad">triad</option>
<option value="adjacent" selected>adjacent</option>
<option value="monochrome">monochrome</option>
</select>
</label>
<button data-toggle>invert</button>
<label for="hue-select">
<span class="label">hue:</span>
<input id="hue-select" type="range" min="0" max="360" value="330">
</label>
<label for="sat-select">
<span class="label">saturation:</span>
<input id="sat-select" type="range" min="0" max="100" value="80">
</label>
<label for="light-select">
<span class="label">lightness:</span>
<input id="light-select" type="range" min="25" max="75" value="50">
</label>
<label for="contrast-select">
<span class="label">contrast:</span>
<input id="contrast-select" type="range" min="25" max="150" value="45">
</label>
</menu>
<h2>initial Light/Dark Based on User-OS</h2>
<p>
Each theme comes with different levels of customization.
Since "triad" and "adjacent" themes
could go either way around the color wheel,
we use one direction for the light mode,
and the other for dark mode.
Sass is only used to simplify the range-selector prefixes,
and JS is only used to set custom properties.
This is <a href="#">what a link looks like</a>.
</p>
<section data-colors="invert">
<h2>data-colors='invert'</h2>
<p>
Lorem ipsum dolor sit amet,
<a href="#">consectetur adipisicing elit</a>.
Et, maiores itaque! Culpa natus officiis eos,
delectus libero impedit autem quae expedita tempora
ipsa excepturi numquam fugit facilis amet quibusdam
laudantium.
</p>
</section>
<div class="palette" role="presentation">
<h3 data-colors="invert">prime</h3>
<div style="--show: var(--prime-palette)"></div>
</div>
<div class="palette" role="presentation">
<h3 data-colors="invert">accent</h3>
<div style="--show: var(--accent-palette)"></div>
</div>
<hr>
<div class="palette" data-theme="complement" data-colors role="presentation">
<h3 data-colors="invert">complement</h3>
<div style="--show: var(--accent-palette)"></div>
</div>
<div class="palette" data-theme="triad" data-colors role="presentation">
<h3 data-colors="invert">triad</h3>
<div style="--show: var(--accent-palette)"></div>
</div>
<div class="palette" data-theme="adjacent" data-colors role="presentation">
<h3 data-colors="invert">adjacent</h3>
<div style="--show: var(--accent-palette)"></div>
</div>
</main>
// primary hue
[data-theme] {
// optionally set `--h-user-*` values with JS
--h-prime: var(--h-user-prime, 330);
--h-accent: var(--h-user-accent, 195);
// initial lightness and saturation values
--l-user-p: calc(var(--l-user) * 1%);
--l: var(--l-user-p, 50%);
--s-user-p: calc(var(--s-user) * 1%);
--s: var(--s-user-p, 80%);
// offset lightness and saturation by various amounts
--offset-count: 3;
--user-contrast-p: calc(var(--user-contrast) * 1%);
--contrast: var(--user-contrast-p, 45%);
--offset-step: calc(var(--contrast) / var(--offset-count));
--off-1: calc(var(--offset-step));
--off-2: calc(var(--offset-step) * 2);
--off-3: var(--contrast);
// generate faded values…
--fade: 0.75;
}
// presets
[data-theme="default-pink"] {
--h-prime: 330;
--h-accent: 195;
--custom-hue: none;
}
[data-theme="default-blue"] {
--h-prime: 195;
--h-accent: 330;
--custom-hue: none;
}
[data-theme="black"] {
--s: 0%;
--l: 50%;
--contrast: calc(100% * var(--offset-count));
--custom-hue: none;
--custom-sat: none;
--custom-light: none;
--custom-contrast: none;
}
// user customizable
[data-theme="monochrome"] {
--h-accent: var(--h-prime);
}
[data-theme="complement"] {
--h-accent: var(--h-complement);
}
[data-theme="triad"] {
--h-accent: var(--h-triad);
}
[data-theme="adjacent"] {
--h-accent: var(--h-adjacent);
}
// Light & Dark Modes
// ------------------
[data-colors] {
@media (prefers-color-scheme: light) {
--os-mode: 1;
}
@media (prefers-color-scheme: dark) {
--os-mode: -1;
}
--given-mode: var(--user-mode, var(--html-mode, var(--os-mode, 1)));
--mode: var(--given-mode);
--invert: calc(-1 * var(--mode));
}
[data-colors="invert"] {
--mode: calc(-1 * var(--given-mode));
}
[data-colors="light"] {
--html-mode: 1;
}
[data-colors="dark"] {
--html-mode: -1;
}
// Re-Calculate when mode changes
// ------------------------------
[data-colors] {
/* options for setting different color palettes */
--h-complement: calc(var(--h-prime) + (180 * var(--mode)));
--h-triad: calc(var(--h-prime) + (120 * var(--mode)));
--h-adjacent: calc(var(--h-prime) + (60 * var(--mode)));
/* full contrast colors, foreground and background */
--bg-full: hsl(0, 0%, calc(100% * var(--mode)));
--fg-full: hsl(0, 0%, calc(100% * var(--invert)));
/* generate lightness values */
--l-fg-1: calc(var(--l) - var(--off-1) * var(--mode));
--l-fg-2: calc(var(--l) - var(--off-2) * var(--mode));
--l-fg-3: calc(var(--l) - var(--off-3) * var(--mode));
--l-bg-1: calc(var(--l) + var(--off-1) * var(--mode));
--l-bg-2: calc(var(--l) + var(--off-2) * var(--mode));
--l-bg-3: calc(var(--l) + var(--off-3) * var(--mode));
/* generate saturation values */
--s1: calc(var(--s) - var(--off-1));
--s2: calc(var(--s) - var(--off-2));
--s3: calc(var(--s) - var(--off-3));
/* generate the prime color palette */
--prime: hsl(var(--h-prime), var(--s), var(--l));
--prime-fade: hsla(var(--h-prime), var(--s3), var(--l), var(--fade));
--prime-fg-1: hsl(var(--h-prime), var(--s1), var(--l-fg-1));
--prime-fg-1-fade: hsla(var(--h-prime), var(--s3), var(--l-fg-1), var(--fade));
--prime-fg-2: hsl(var(--h-prime), var(--s2), var(--l-fg-2));
--prime-fg-2-fade: hsla(var(--h-prime), var(--s3), var(--l-fg-2), var(--fade));
--prime-fg-3: hsl(var(--h-prime), var(--s3), var(--l-fg-3));
--prime-fg-3-fade: hsla(var(--h-prime), var(--s3), var(--l-fg-3), var(--fade));
--prime-bg-1: hsl(var(--h-prime), var(--s1), var(--l-bg-1));
--prime-bg-1-fade: hsla(var(--h-prime), var(--s3), var(--l-bg-1), var(--fade));
--prime-bg-2: hsl(var(--h-prime), var(--s2), var(--l-bg-2));
--prime-bg-2-fade: hsla(var(--h-prime), var(--s3), var(--l-bg-2), var(--fade));
--prime-bg-3: hsl(var(--h-prime), var(--s3), var(--l-bg-3));
--prime-bg-3-fade: hsla(var(--h-prime), var(--s3), var(--l-bg-3), var(--fade));
/* generate the accent color palette */
--accent: hsl(var(--h-accent), var(--s), var(--l));
--accent-fade: hsla(var(--h-accent), var(--s3), var(--l), var(--fade));
--accent-fg-1: hsl(var(--h-accent), var(--s1), var(--l-fg-1));
--accent-fg-1-fade: hsla(var(--h-accent), var(--s3), var(--l-fg-1), var(--fade));
--accent-fg-2: hsl(var(--h-accent), var(--s2), var(--l-fg-2));
--accent-fg-2-fade: hsla(var(--h-accent), var(--s3), var(--l-fg-2), var(--fade));
--accent-fg-3: hsl(var(--h-accent), var(--s3), var(--l-fg-3));
--accent-fg-3-fade: hsla(var(--h-accent), var(--s3), var(--l-fg-3), var(--fade));
--accent-bg-1: hsl(var(--h-accent), var(--s1), var(--l-bg-1));
--accent-bg-1-fade: hsla(var(--h-accent), var(--s3), var(--l-bg-1), var(--fade));
--accent-bg-2: hsl(var(--h-accent), var(--s2), var(--l-bg-2));
--accent-bg-2-fade: hsla(var(--h-accent), var(--s3), var(--l-bg-2), var(--fade));
--accent-bg-3: hsl(var(--h-accent), var(--s3), var(--l-bg-3));
--accent-bg-3-fade: hsla(var(--h-accent), var(--s3), var(--l-bg-3), var(--fade));
/* flip the shadow based on the mode */
--shadow-offset: calc(1px * var(--mode));
/* color props set directly by the theme */
background: var(--accent-bg-3);
color: var(--prime-fg-3);
text-shadow: var(--shadow-offset) var(--shadow-offset) 0 var(--bg-full);
}
/* some UI elements… */
a {
--link-color: var(--prime-fg-2);
--link-underline: var(--prime-fg-2);
color: var(--link-color);
text-decoration-color: var(--link-underline);
text-decoration-skip-ink: auto;
&:hover,
&:focus,
&:active {
--link-color: var(--prime-fg-3);
--link-underline: var(--prime);
}
}
button {
--button-background: var(--accent-fg-2);
--button-color: var(--accent-bg-3);
--button-border: var(--accent-fg-3-fade);
background: var(--button-background);
border: 2px solid var(--button-border);
color: var(--button-color);
cursor: pointer;
padding: 0.25em 0.5em;
&:hover,
&:focus {
--button-background: var(--accent-fg-3);
--button-border: var(--accent-fg-3);
}
}
button:active {
--button-background: var(--prime-bg-2);
--button-color: var(--prime-fg-2);
}
/* show the colors */
.palette {
background: linear-gradient(
45deg,
var(--accent-fg-3) 25%,
transparent 25% 75%,
var(--accent-fg-3) 75%
),
linear-gradient(
45deg,
var(--accent-fg-3) 25%,
transparent 25% 75%,
var(--accent-fg-3) 75%
)
var(--accent-bg-3);
background-size: 1em 1em;
background-position: 0 0, 0.5em 0.5em;
border: 2px solid var(--fg-full);
margin: 1em 0;
h3 {
font-size: 0.75em;
padding: 0.25em 1em;
}
}
[style*="--show"] {
--stop: calc(100% / (var(--offset-count) * 2 + 1));
--accent-palette-main: linear-gradient(to right,
var(--accent-bg-3) calc(var(--stop) * 1),
var(--accent-bg-2) calc(var(--stop) * 1) calc(var(--stop) * 2),
var(--accent-bg-1) calc(var(--stop) * 2) calc(var(--stop) * 3),
var(--accent) calc(var(--stop) * 3) calc(var(--stop) * 4),
var(--accent-fg-1) calc(var(--stop) * 4) calc(var(--stop) * 5),
var(--accent-fg-2) calc(var(--stop) * 5) calc(var(--stop) * 6),
var(--accent-fg-3) calc(var(--stop) * 6)
);
--accent-palette-fade: linear-gradient(to right,
var(--accent-bg-3-fade) calc(var(--stop) * 1),
var(--accent-bg-2-fade) calc(var(--stop) * 1) calc(var(--stop) * 2),
var(--accent-bg-1-fade) calc(var(--stop) * 2) calc(var(--stop) * 3),
var(--accent-fade) calc(var(--stop) * 3) calc(var(--stop) * 4),
var(--accent-fg-1-fade) calc(var(--stop) * 4) calc(var(--stop) * 5),
var(--accent-fg-2-fade) calc(var(--stop) * 5) calc(var(--stop) * 6),
var(--accent-fg-3-fade) calc(var(--stop) * 6)
);
--prime-palette-main: linear-gradient(to right,
var(--prime-bg-3) calc(var(--stop) * 1),
var(--prime-bg-2) calc(var(--stop) * 1) calc(var(--stop) * 2),
var(--prime-bg-1) calc(var(--stop) * 2) calc(var(--stop) * 3),
var(--prime) calc(var(--stop) * 3) calc(var(--stop) * 4),
var(--prime-fg-1) calc(var(--stop) * 4) calc(var(--stop) * 5),
var(--prime-fg-2) calc(var(--stop) * 5) calc(var(--stop) * 6),
var(--prime-fg-3) calc(var(--stop) * 6)
);
--prime-palette-fade: linear-gradient(to right,
var(--prime-bg-3-fade) calc(var(--stop) * 1),
var(--prime-bg-2-fade) calc(var(--stop) * 1) calc(var(--stop) * 2),
var(--prime-bg-1-fade) calc(var(--stop) * 2) calc(var(--stop) * 3),
var(--prime-fade) calc(var(--stop) * 3) calc(var(--stop) * 4),
var(--prime-fg-1-fade) calc(var(--stop) * 4) calc(var(--stop) * 5),
var(--prime-fg-2-fade) calc(var(--stop) * 5) calc(var(--stop) * 6),
var(--prime-fg-3-fade) calc(var(--stop) * 6)
);
--prime-palette: var(--prime-palette-main), var(--prime-palette-fade);
--accent-palette: var(--accent-palette-main), var(--accent-palette-fade);
background-image: var(--show);
background-size: 100% 50%, 100% 100%;
background-repeat: no-repeat;
height: 3em;
}
/* layout */
body {
display: grid;
min-height: 100vh;
font-size: calc(1em + 0.5vmin);
}
main {
--growth: 3vw;
padding: 0.5em calc(0.25em + var(--growth));
transition: padding 100ms ease-out;
@media (min-width: 45em) {
--growth: 10vw;
}
@media (min-width: 80em) {
--growth: 15vw;
}
}
section {
padding: 0.5em 1em;
}
menu {
align-items: center;
display: grid;
grid-auto-rows: 1fr;
grid-template-columns: repeat(4, 1fr);
grid-gap: 0.5em 1.5em;
font-size: smaller;
margin-bottom: 2em;
padding: 0;
@media (min-width: 45em) {
--min: 18em;
}
@media (min-width: 70em) {
--min: 20em;
}
}
[data-toggle] {
justify-self: end;
grid-column: span 1 / -1;
}
[for*='select'] {
grid-column: span 2;
}
[for='theme-select'] {
grid-column: span 3;
}
.label {
display: block;
}
[for='hue-select'] {
display: var(--custom-hue, block);
}
[for='light-select'] {
display: var(--custom-light, block);
}
[for='sat-select'] {
display: var(--custom-sat, block);
}
[for='contrast-select'] {
display: var(--custom-contrast, block);
}
@supports (background-clip: text) {
h1 {
background: linear-gradient(to bottom right, var(--prime-fg-1), var(--accent-fg-1));
background-clip: text;
box-decoration-break: clone;
color: transparent;
text-shadow: none;
}
}
h2 {
font-size: 1em;
}
h3, h2 {
margin: 0;
}
::selection {
background: var(--prime-fg-2);
color: var(--prime-bg-3);
background-clip: initial;
text-shadow: none;
}
/* range input */
@mixin range-track {
&::-webkit-slider-runnable-track { @content; }
&::-moz-range-track { @content; }
&::-ms-track { @content; }
}
@mixin range-thumb {
&::-webkit-slider-thumb { @content; }
&::-moz-range-thumb { @content; }
&::-ms-thumb { @content; }
}
$rainbow: ();
@for $hue from 1 through 36 {
$rainbow: append($rainbow, hsl($hue * 10, 80%, 40%), comma);
}
[type='range'] {
-webkit-appearance: none;
background: transparent;
height: 0.75em;
width: 100%;
@include range-track {
height: 1em;
background: var(--accent-fg-3-fade);
border-radius: 1em;
border: 1px solid var(--accent-fg-2-fade);
cursor: pointer;
}
@include range-thumb {
border: 2px solid var(--accent-fg-3);
height: 1em;
width: 0.5em;
border-radius: 0.25em;
background: var(--prime);
cursor: pointer;
}
}
[id='hue-select'] {
@include range-track {
background: linear-gradient(
to right,
$rainbow
);
}
}
[id='light-select'] {
@include range-track {
background: linear-gradient(
to right,
hsl(0, 0%, 25%),
hsl(0, 0%, 75%)
);
}
}
[id='sat-select'] {
@include range-track {
background: linear-gradient(
to right,
hsl(var(--h-prime), 0%, var(--l)),
hsl(var(--h-prime), 100%, var(--l))
);
}
}
[id='contrast-select'] {
@include range-track {
background: linear-gradient(
to right,
hsl(0, 0%, 50%),
hsl(0, 0%, 100%)
),
linear-gradient(
to right,
hsl(0, 0%, 50%),
hsl(0, 0%, 0%)
);
background-size: 100% 50%, 100% 100%;
background-position: top left, bottom left;
background-repeat: no-repeat;
}
}
const themeAttr = 'data-theme';
const root = document.querySelector(`[${themeAttr}]`);
const modeToggle = document.querySelector('[data-toggle]');
const hueProp = '--h-user-prime';
const satProp = '--s-user';
const lightProp = '--l-user';
const contrastProp = '--user-contrast';
const modeProp = '--user-mode';
const themeSelect = document.querySelector('#theme-select');
const hueSelect = document.querySelector('#hue-select');
const satSelect = document.querySelector('#sat-select');
const lightSelect = document.querySelector('#light-select');
const contrastSelect = document.querySelector('#contrast-select');
const changeMode = () => {
const is = getComputedStyle(root)
.getPropertyValue('--mode')
.trim();
changeProp(modeProp, Number(is) * -1);
}
const changePreset = () => {
const to = themeSelect.value;
root.setAttribute(themeAttr, to);
}
const changeProp = (prop, val) => {
root.style.setProperty(prop, val);
}
document.onload = changePreset();
themeSelect.addEventListener('input', () => changePreset());
modeToggle.addEventListener('click', () => changeMode());
hueSelect.addEventListener('input', () => changeProp(hueProp, hueSelect.value));
satSelect.addEventListener('input', () => changeProp(satProp, satSelect.value));
lightSelect.addEventListener('input', () => changeProp(lightProp, lightSelect.value));
contrastSelect.addEventListener('input', () => changeProp(contrastProp, contrastSelect.value));
Also see: Tab Triggers