cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

            
              <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>  
            
          
!
            
              /* 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 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
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console