Pen Settings



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


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


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.


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.


                <aside class="sliders">
  <input type="range" id="red" min="0" max="255" value="200" step="1"> 
  <input type="range" id="green" min="0" max="255" value="60" step="1"> 
  <input type="range" id="blue" min="0" max="255" value="255" step="1">

<main class="buttons">  
  <h2>sRBG Luma method</h2>
  <button class="btn">Primary Color</button> 
  <button class="btn btn--secondary">Secondary Color</button> 
  <h2>W3C Luma method</h2>
  <button class="btn btn--w3c">Primary Color</button> 
  <button class="btn btn--w3c btn--secondary">Secondary Color</button>


The challenge: 
1) Set text to either black or white depending on the element background perceived lightness (luma)
2) Set a border as a darker variation of the base color to improve button visibility, ONLY if background luma is really high
3) Automatically generate a secondary, 60º rotated hue color

:root {
  /* theme color variables to use in RGB declarations */
  --red: 200;
  --green: 60;
  --blue: 255;
  /*the threshold at which colors are considered "light". 
Range: decimals from 0 to 1,
recommended 0.5 - 0.6*/
  --threshold: 0.5;
  /*the threshold at which a darker border will be applied.
Range: decimals from 0 to 1,
recommended 0.8+*/
  --border-threshold: 0.8;

.btn {
/*sets the background for the base class*/
	background: rgb(var(--red), var(--green), var(--blue));

Calcs perceived brightness using the 
sRGB Luma method
lightness = (red * 0.2126 + green * 0.7152 + blue * 0.0722) / 255
  --r: calc(var(--red) * 0.2126);
  --g: calc(var(--green) * 0.7152);
  --b: calc(var(--blue) * 0.0722);
  --sum: calc(var(--r) + var(--g) + var(--b));
  --perceived-lightness: calc(var(--sum) / 255);
1) Any lightness value above the threshold will be considered "light", therefore apply a black text color. Any bellow will be considered dark, and use white color. 
This results from appying either a sub-zero (negative) or a higher-than-100 lightness value, which are capped to 0 and 100 respectively, to a HSL declaration
  color: hsl(0, 0%, calc((var(--perceived-lightness) - var(--threshold)) * -10000000%));
2) sets the border as a 50% darker shade of the base color, ONLY if background color luma is higher than the border threshold.
To achieve this I use the same sub-zero or higher-than-max technique, only this time using the Alpha value from an RGBA declaration. 
This results in a border that's either fully transparent or fully opaque
 --border-alpha: calc((var(--perceived-lightness) - var(--border-threshold)) * 100);
  border-width: .2em;
  border-style: solid;
  border-color: rgba(calc(var(--red) - 50), calc(var(--green) - 50), calc(var(--blue) - 50), var(--border-alpha));

/* Alternative calc using the 
W3C luma method
lightness = (red * 0.299 + green * 0.587 + blue * 0.114) / 255
  --r: calc(var(--red) * 0.299);
  --g: calc(var(--green) * 0.587);
  --b: calc(var(--blue) * 0.114);

  /* 3) sets the background color as a 60º rotated hue */

/* just basic styles*/
  background: honeydew;
  max-width: 600px;
  margin: auto;

  grid-template-columns: repeat(auto-fit, minmax(150px,1fr));

  grid-template-columns: repeat(auto-fit, minmax(200px,1fr));
  grid-gap: 1em;

.buttons h2{
  grid-column: 1 / -1;

  padding: 1em;
  font-size: 1.5rem; 
  border-radius: 0.2em;
  box-sizing: border-box;

  color: black;
  margin: 10px;

  content: attr(id);
  text-transform: capitalize;

  counter-reset: red var(--red);
  content: counter(red);

  counter-reset: green var(--green);
  content: counter(green);

  counter-reset: blue var(--blue);
  content: counter(blue);


  JS is used only to set the CSS custom properties used in the base color
  Color changes are calculated in CSS 

const root = document.documentElement;
const inputs = []'input'));

inputs.forEach(input => input.addEventListener('change', handleUpdate));
inputs.forEach(input => input.addEventListener('mousemove', handleUpdate));

function handleUpdate(e) {
  if ( === 'red')'--red', this.value);
  if ( === 'green')'--green', this.value);
  if ( === 'blue')'--blue', this.value);