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.

            
              <p>Study Notes from <a href="https://www.youtube.com/watch?v=kZOJCVvyF-4">Lea Verou: CSS Variables: var(--subtitle) - CSSConf.Asia 2016</a></p>

<div class="block">
  <p class="css-var-text">hello world</p>
</div>

<div class="block">
  <p class="css-var-text" style="--color:blue;">inline style</p>
</div>

<div class="block">
  <div class="css-var-text">
    parent element with CSS var ->
    <span>child element inherit by default</span>
  </div>
</div>

<div class="block">
  background img url test
  <div class="css-var-url"></div>
</div>

<div class="block foo-test">
  <h1>CSS var are case sensitive</h1>
  <p class="test-foo">--foo:; // unknown prop name</p>
  <p class="test-ffoo">--foo: ; // unknown prop name</p>
  <p class="test-Foo">test-Foo</p>
  <p class="test-FOo">test-FOo</p>
  <p class="test-FOO">test-FOO</p>
</div>

<div class="block">
  <p style="color: var(--color1, orange);">var function: default value</p>
</div>

<div class="block browser-support">
  <code>
    @supports (--css: variables) {}
  </code>
</div>

<div class="block">
  <code>
    --size: 6;
    width: var(--size)em; // doesn't work
    width: calc(var(--size) * 1em); // it works!
  </code>
  <div class="css-var-unit">
  use variable for pure data, not for CSS values
  </div>
</div>

<div class="block animation">
  animation
</div>

<div class="block">
  <h1>Common Use</h1>
  
  <p class="media-demo">media-demo</p>
  
  <code>
    // default value
    --colorD: var(--color, black)
    
    // media query
    :root{ --gutter: 2px; }
    
    section{
      padding: var(--gutter);
    }
    
    @media(min-width: 1000px){
      :root{
        --gutter: 10px;
      }
    }
  </code>
  
  <p>Write onece, use anywhere.</p>
  <p>Seperate properties shortcut, redefine every property.</p>
  
  <div class="block shadow-box">box shadow</div>
</div>

<div class="block">
  Javascript Compute
  
  for example: mouseover/input range/scroll
  
  <p id="JS_Hook" class="js-demo" style="--bkg: black; background: var(--bkg);">
    hover here to change color. <br/>
  </p>
  <p>
    inline background-color is <span id="JS_DEMO_BKG"></span>, <br/>
    color from everywhere is <span id="JS_DEMO_COLOR"></span>.
  </p>
</div>

<div id="JS_BigBang" class="big-bang">
  loading...
</div>
            
          
!
            
              @use postcss-nested;
body{
  --color: green;
}
.block{
  border: 1px solid var(--color);
  width: 500px;
  padding: 10px;
  margin: 10px;
}
.css-var-text{
  --color: green;
  color: var(--color);
}
.css-var-url{
  --img: url("http://cdn.sinacloud.net/demo-static/img/avatar.JPG");
  background: var(--img) center / cover;
  --size: 100px;
  width: var(--size);
  height: var(--size);
}

.foo-test{
  --foo:;
  --ffoo:;
  --Foo: red;
  --FOo: blue;
  --FOO: green;
  .test-foo{
    color: var(--foo);
  }
  .test-ffoo{
    color: var(--ffoo);
  }
  .test-Foo{
    color: var(--Foo);
  }
  .test-FOo{
    color: var(--FOo);
  }
   .test-FOO{
    color: var(--FOO);
  }
}

.browser-support{
  background: red;  
}
@supports (--css: variables) {
  .browser-support{
    background: var(--color);
  }
}

.css-var-unit{
  background: var(--color);
  --size: 6;
  font-size: 14px;
  width: calc(var(--size) * 1em);
  height: calc(var(--size) * 1em);
}

@keyframes bg{
  from {
    background: var(--color);
  }
  to {
    background: #0ca;
  }
}
.animation{
  --color: yellow;
  height: 100px;
  animation: 1s bg infinite alternate;
  &:hover{
    --color: green;
  }
}

:root{ --gutter: 2px; }
    
@media(min-width: 1000px){
  :root{
    --gutter: 10px;
  }
}
.media-demo{
  background: red;
  padding: var(--gutter);
  display: inline-block;
}

.shadow-box{ 
  box-shadow: var(--box-shadow-x, 0)
              var(--box-shadow-y, 0)
              var(--box-shadow-blur, 20px)
              var(--box-shadow-spread, 0)
              var(--box-shadow-color, blue)
              var(--box-shadow-inset, );
  &:hover{
    --box-shadow-color: red;
  }
}

.js-demo{
  --color: red;
  color: var(--color);
}

.big-bang{
  width: 100%;
  height: 500px;
  background-image: radial-gradient(at calc(100% * var(--moveX)) calc(100% * var(--moveY)), transparent, black);
}
            
          
!
            
              // JS for CSS Variables
// getter & setter
var element = document.getElementById('JS_Hook');

var cssVarBkg = element.style.getPropertyValue('--bkg');

var cssVarColor = getComputedStyle(element).getPropertyValue('--color')

document.getElementById('JS_DEMO_BKG').innerHTML = cssVarBkg;

document.getElementById('JS_DEMO_COLOR').innerHTML = cssVarColor;

element.addEventListener('mouseenter', function(e){
  element.style.setProperty('--color', cssVarBkg);
  element.style.setProperty('--bkg', cssVarColor);
});

element.addEventListener('mouseleave', function(e){
  element.style.setProperty('--color', cssVarColor);
  element.style.setProperty('--bkg', cssVarBkg);
});

// big bang
var bigBangElement = document.getElementById('JS_BigBang');

bigBangElement.addEventListener('mousemove', function(e){
  var x = e.clientX / innerWidth;
  var y = e.clientY / 500;
  
  // console.log(x + '-' + y);
  
  bigBangElement.style.setProperty('--moveX', x);
  bigBangElement.style.setProperty('--moveY', y);
});

            
          
!
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