Pen Settings

HTML

CSS

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

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

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.

HTML

              
                <aside class="sliders">
  <input type="range" id="hue" min="0" max="360" value="220" step="1"> 
  <input type="range" id="saturation" min="0" max="100" value="100" step="1"> 
  <input type="range" id="lightness" min="0" max="100" value="50" step="1">
</aside>
<main class="buttons">  
  <button class="btn">Primary Color</button> 
  <button class="btn btn--secondary">Secondary Color</button> 
</main>  
              
            
!

CSS

              
                /* The challenge: 
1) Set text to either black or white depending on the element background lightness
2) Set a border as a darker variation of the base color to improve button visibility, ONLY IF background lightness is higher than the border lightness threshold
3) Automatically generate a secondary color
*/

:root {
  /* theme color variables to use in HSL declarations */
  --hue: 220;
  --sat: 100;
  --light: 50;
  /*the threshold at which colors are considered "light". 
Range: integers from 0 to 100,
recommended 50 - 70 */
  --threshold: 65;
  /*the threshold at which a darker border will be applied.
Range: integers from 0 to 100,
recommended 80+ */
  --border-threshold: 80;
}

.btn {
/*auxiliary variable for hue manipulation*/
  --h: var(--hue);

  /*sets the background for the base class*/
	background: hsl(var(--h), calc(var(--sat) * 1%), calc(var(--light) * 1%));
  
  /* 1) Any lightness value bellow the threshold will result in white, any above will result in black*/
  --switch: calc((var(--light) - var(--threshold)) * -100%);
  color: hsl(0, 0%, var(--switch));
  
/* 2) border as 30% darker only if brightness is above the border threshold */
  --border-light: calc(var(--light) * 0.7%);
  --border-alpha:calc((var(--light) - var(--border-threshold)) * 10);
  
  border:.2em solid hsla(var(--h), calc(var(--sat) * 1%), var(--border-light), var(--border-alpha));
}

.btn--secondary{
  /* 3) sets the background color as a 60º rotated hue */
  --h: calc(var(--hue) + 60);
}

/* just aesthetic styles*/

body{
  background: honeydew;
  padding:1rem;
  max-width: 600px;
  margin: auto;
}

.sliders{
  width: 100%;
  display:flex;
}

.sliders input{
  flex:1;
}

.buttons{
  display:flex;
}

.btn{
  padding: 1em; margin: 0.5em;
  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;
}

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

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

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

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

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 === 'hue') root.style.setProperty('--hue', this.value);
   if (this.id === 'saturation') root.style.setProperty('--sat', this.value);
  if (this.id === 'lightness') root.style.setProperty('--light', this.value);
}
              
            
!
999px

Console