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 CSS

These stylesheets will be added in this order and before the code you write in the CSS editor. You can also add another Pen here, and it will pull the CSS from it. Try typing "font" or "ribbon" below.

Quick-add: + add another resource

Add External JavaScript

These scripts will run in this order and before the code in the JavaScript editor. You can also link to another Pen here, and it will run the JavaScript from it. Also try typing the name of any popular library.

Quick-add: + add another resource

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.

            
              <h2>Changing CSS Variables in Keyframes</h2>
<div id="css"></div>
<h2>Changing CSS Variables in JavaScript</h2>
<div id="raf"></div>
<!--h2>Interacting with CSS Variables (press white keys)</h2>
<div id="interact"></div-->
            
          
!
            
              div, div::before, div::after {
  position: relative;
  box-sizing: border-box;
}

div {
  --color1: hsl(196, 50%, 50%);
  --color1alt: hsl(196, 50%, 33%);
  --color1alta: hsl(196, 50%, 73%);
  --white: #eaeaef;
  --white-between: hsla(0,0%,0%,.2);
  --black: var(--color1alt);
  
  
  --the-black-keys: 
    linear-gradient(
      to right,
      transparent 3.5%,
      var(--black1, var(--black)) 3.5%,
      var(--black1, var(--black)) 6.5%,
      transparent 6.5%,
      transparent 8.5%,
      var(--black2, var(--black)) 8.5%,
      var(--black2, var(--black)) 11.5%,
      transparent 11.5%,
      transparent 13.5%,
      var(--black3, var(--black)) 13.5%,
      var(--black3, var(--black)) 16.5%,
      transparent 16.5%,
      
      transparent 23.5%,
      var(--black4, var(--black)) 23.5%,
      var(--black4, var(--black)) 26.5%,
      transparent 26.5%,
      transparent 28.5%,
      var(--black5, var(--black)) 28.5%,
      var(--black5, var(--black)) 31.5%,
      transparent 31.5%,
      
      transparent 38.5%,
      var(--black6, var(--black)) 38.5%,
      var(--black6, var(--black)) 41.5%,
      transparent 41.5%,
      transparent 43.5%,
      var(--black7, var(--black)) 43.5%,
      var(--black7, var(--black)) 46.5%,
      transparent 46.5%,
      transparent 48.5%,
      var(--black8, var(--black)) 48.5%,
      var(--black8, var(--black)) 51.5%,
      transparent 51.5%,
      
      transparent 58.5%,
      var(--black9, var(--black)) 58.5%,
      var(--black9, var(--black)) 61.5%,
      transparent 61.5%,
      transparent 63.5%,
      var(--black10, var(--black)) 63.5%,
      var(--black10, var(--black)) 66.5%,
      transparent 66.5%,
      
      
      transparent 73.5%,
      var(--black11, var(--black)) 73.5%,
      var(--black11, var(--black)) 76.5%,
      transparent 76.5%,
      transparent 78.5%,
      var(--black12, var(--black)) 78.5%,
      var(--black12, var(--black)) 81.5%,
      transparent 81.5%,
      transparent 83.5%,
      var(--black13, var(--black)) 83.5%,
      var(--black13, var(--black)) 86.5%,
      transparent 86.5%,
      
      transparent 93.5%,
      var(--black14, var(--black)) 93.5%,
      var(--black14, var(--black)) 96.5%,
      transparent 96.5%
    );
  
  /* If not animating, the keys can be done with a repeating linear gradient, breaking it out explicity (a few lines down) allows us more animation options 
  --keyboard: repeating-linear-gradient(to right, 
      var(--white-between) 0%,
      var(--white-between) 0.4%,
      var(--white) 0.4%,
      var(--white) 5%);
  */
  
  --keyboard: linear-gradient(to right, 
      var(--white-between) 0%,
      var(--white-between) 0.4%,
      var(--white1, var(--white)) 0.4%,
      var(--white1, var(--white)) 5%, 
      var(--white-between) 5%,
      var(--white-between) 5.4%,
      var(--white2, var(--white)) 5.4%,
      var(--white2, var(--white)) 10%, 
      var(--white-between) 10%,
      var(--white-between) 10.4%,
      var(--white3, var(--white)) 10.4%,
      var(--white3, var(--white)) 15%, 
      var(--white-between) 15%,
      var(--white-between) 15.4%,
      var(--white4, var(--white)) 15.4%,
      var(--white4, var(--white)) 20%, 
      var(--white-between) 20%,
      var(--white-between) 20.4%,
      var(--white5, var(--white)) 20.4%,
      var(--white5, var(--white)) 25%, 
      var(--white-between) 25%,
      var(--white-between) 25.4%,
      var(--white6, var(--white)) 25.4%,
      var(--white6, var(--white)) 30%, 
      var(--white-between) 30%,
      var(--white-between) 30.4%,
      var(--white7, var(--white)) 30.4%,
      var(--white7, var(--white)) 35%, 
      var(--white-between) 35%,
      var(--white-between) 35.4%,
      var(--white8, var(--white)) 35.4%,
      var(--white8, var(--white)) 40%, 
      var(--white-between) 40%,
      var(--white-between) 40.4%,
      var(--white9, var(--white)) 40.4%,
      var(--white9, var(--white)) 45%, 
      var(--white-between) 45%,
      var(--white-between) 45.4%,
      var(--white10, var(--white)) 45.4%,
      var(--white10, var(--white)) 50%, 
      var(--white-between) 50%,
      var(--white-between) 50.4%,
      var(--white11, var(--white)) 50.4%,
      var(--white11, var(--white)) 55%, 
      var(--white-between) 55%,
      var(--white-between) 55.4%,
      var(--white12, var(--white)) 55.4%,
      var(--white12, var(--white)) 60%, 
      var(--white-between) 60%,
      var(--white-between) 60.4%,
      var(--white13, var(--white)) 60.4%,
      var(--white13, var(--white)) var(--white13-pos,65%), 
      var(--white-between) var(--white13-pos,65%),
      var(--white-between) var(--white13-pos-offset,65.4%),
      var(--white14, var(--white)) var(--white13-pos-offset,65.4%),
      var(--white14, var(--white)) var(--white14-pos,70%), 
      var(--white-between) var(--white14-pos,70%),
      var(--white-between) var(--white14-pos-offset,70.4%),
      var(--white15, var(--white)) var(--white14-pos-offset,70.4%),
      var(--white15, var(--white)) 75%, 
      var(--white-between) 75%,
      var(--white-between) 75.4%,
      var(--white16, var(--white)) 75.4%,
      var(--white16, var(--white)) 80%, 
      var(--white-between) 80%,
      var(--white-between) 80.4%,
      var(--white17, var(--white)) 80.4%,
      var(--white17, var(--white)) 85%, 
      var(--white-between) 85%,
      var(--white-between) 85.4%,
      var(--white18, var(--white)) 85.4%,
      var(--white18, var(--white)) 90%, 
      var(--white-between) 90%,
      var(--white-between) 90.4%,
      var(--white19, var(--white)) 90.4%,
      var(--white19, var(--white)) 95%, 
      var(--white-between) 95%,
      var(--white-between) 95.4%,
      var(--white20, var(--white)) 95.4%,
      var(--white20, var(--white)) 100%);
  
  --black-keys-position: center 0%;
  --black-keys-size: 100% 66%;
  --keyboard-position: 0% 20%;
  --keyboard-size: 100% 90%;
}

/*keyboard*/
div {
  width: 100vmin;
  height: 22vmin;
 
  background:
    var(--the-black-keys),
    var(--keyboard);
  background-repeat: no-repeat;
  
  background-position:
    var(--black-keys-position),
    var(--keyboard-position);
  
  background-size:
    var(--black-keys-size),
    var(--keyboard-size);
  
  user-select: none;
}

#css {
  animation: press-keys 1000ms infinite alternate linear both;
}
#raf {
  transition: background 200ms linear;
}

@keyframes press-keys {
  50% {
    --white14: hsl(343, 80%, 80%);
    --white13-pos-offset: 65.6%;
    --white14-pos: 69.9%;
  }
  100% {
    --black8: hsl(343, 80%, 50%);
  }
}

body {
  display: flex;
  justify-content: center;
  align-items: center;
  flex-direction: column;
  height: 100vh;
  overflow: hidden;
  background: hsl(233, 12%, 59%);
}
h2 {
  margin: calc(2vmin + .2rem);
  text-align: center;
  font-family: system-ui, -apple-system, 'Segoe UI', sans-serif;
}
            
          
!
            
              var raf = document.getElementById('raf').style;
var interact = document.getElementById('interact');

function pressKeys() {
  var now = Date.now();
  var blackKeyDown = now % 2000 > 1000;
  var whiteKeyDown = now % 1000 > 500;
  if (blackKeyDown) {
    raf.setProperty('--black8', 'hsl(343, 80%, 50%)');
  } else {
    raf.setProperty('--black8', 'var(--black)');
  }
  if (whiteKeyDown) {
    raf.setProperty('--white14', 'hsl(343, 80%, 80%)');
    raf.setProperty('--white13-pos-offset', '65.6%');
    raf.setProperty('--white14-pos', '69.9%');
  } else {
    raf.setProperty('--white14', 'var(--white)');
    raf.setProperty('--white13-pos-offset', '65.4%');
    raf.setProperty('--white14-pos', '70%');
  }
  requestAnimationFrame(pressKeys);
}
pressKeys();


/*
var bounds;
interact.addEventListener('pointerdown', interactWithWhiteKeys);

interact.addEventListener('pointerup', interactWithWhiteKeys)

//It should be noted this is entirely unnecessary. The Pointer Events you get with Elements are going to be way more advanced and useful.  But it goes to show you, you can do a lot inside of a div.  This could be taken further to tie into cancel and move and turn keys off. 

function interactWithWhiteKeys(e) {
  bounds = interact.getBoundingClientRect();
  var offset = e.clientX - bounds.left;
  var percent = offset / bounds.width;
  var idx = Math.ceil(percent * 20);
  var isPointerDown = e.type === 'pointerdown';
  
  interact.style.setProperty('--white' + idx, isPointerDown ? 'hsl(343, 80%, 80%)' : 'var(--white)');
}

*/
            
          
!
999px
Loading ..................

Console