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. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ 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

              
                <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">
</aside>

<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>
</main>  
              
            
!

CSS

              
                /*
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));
}

.btn--w3c{
/* 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);
}

.btn--secondary{
  /* 3) sets the background color as a 60º rotated hue */
  filter:hue-rotate(60deg);
}

/* just basic styles*/
body{
  background: honeydew;
  padding:1rem;
  max-width: 600px;
  margin: auto;
}

.sliders{
  display:grid;
  grid-template-columns: repeat(auto-fit, minmax(150px,1fr));
  grid-gap:1em;
}

.buttons{
  display:grid;
  grid-template-columns: repeat(auto-fit, minmax(200px,1fr));
  grid-gap: 1em;
}

.buttons h2{
  grid-column: 1 / -1;
  margin-bottom:0;
}

.btn{
  padding: 1em;
  font-size: 1.5rem; 
  border-radius: 0.2em;
  box-sizing: border-box;
  flex:1;
}

input[type=range]{
  display:flex;
  flex-direction:column;
  color: black;
  text-align:center;
  margin: 10px;
  font-weight:600;
}

input::before{
  content: attr(id);
  text-transform: capitalize;
}

input[id=red]::after{
  counter-reset: red var(--red);
  content: counter(red);
}

input[id=green]::after{
  counter-reset: green var(--green);
  content: counter(green);
}

input[id=blue]::after{
  counter-reset: blue var(--blue);
  content: counter(blue);
}
              
            
!

JS

              
                /*
  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 = [].slice.call(document.querySelectorAll('input'));

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

function handleUpdate(e) {
  if (this.id === 'red') root.style.setProperty('--red', this.value);
  if (this.id === 'green') root.style.setProperty('--green', this.value);
  if (this.id === 'blue') root.style.setProperty('--blue', this.value);
}
              
            
!
999px

Console