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. You can use the CSS from another Pen by using it's URL and the proper URL extention.

+ 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

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

CSS

              
                @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

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

Console