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

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

              
                <link rel="stylesheet" href="https://unpkg.com/spectre.css/dist/spectre.min.css">
<section class="container">
  <div class="pad-1"></div>
  <div class="columns col-10 col-mx-auto">
    <div class="column col-xs-6 col-4 mb-3">
      <div class="color bg-white pad-1">
        <div>white</div>
      </div>
    </div>
    <div class="column col-xs-6 col-4 mb-3">
      <div class="color bg-grey pad-1">
        <div>grey</div>
      </div>
    </div>
    <div class="column col-xs-6 col-4 mb-3">
      <div class="color bg-black pad-1">
        <div>black</div>
      </div>
    </div>
    <div class="column col-xs-6 col-4 mb-3">
      <div class="color bg-blue pad-1">
        <div>blue</div>
      </div>
    </div>
    <div class="column col-xs-6 col-4 mb-3">
      <div class="color bg-indigo pad-1">
        <div>indigo</div>
      </div>
    </div>
    <div class="column col-xs-6 col-4 mb-3">
      <div class="color bg-purple pad-1">
        <div>purple</div>
      </div>
    </div>
    <div class="column col-xs-6 col-4 mb-3">
      <div class="color bg-green pad-1">
        <div>green</div>
      </div>
    </div>
    <div class="column col-xs-6 col-4 mb-3">
      <div class="color bg-red pad-1">
        <div>red</div>
      </div>
    </div>
    <div class="column col-xs-6 col-4 mb-3">
      <div class="color bg-orange pad-1">
        <div>orange</div>
      </div>
    </div>
    <div class="column col-xs-6 col-4 mb-3">
      <div class="color bg-yellow pad-1">
        <div>yellow</div>
      </div>
    </div>
  </div>
</section>
              
            
!

CSS

              
                // Some fun on-brand colours
$blue:           #259AC4;
$indigo:         #244260;
$green:          #59B27D;
$white:          #f5f8fa;
$grey:           #939DA8;
$black:          #2B3745;
$purple:         #8058a5;
$red:            #ec2c4c;
$orange:         #eb6a39;
$yellow:         #ebcd39;

// helpful background mixin (thanks, Spectre!)
@mixin bg-color-variant($name: ".bg-primary", $color: $primary-color) {
  #{$name} {
    background: $color;
    .label {
      background-color: #555;
      color: #eeeeee;
    }

    @if (lightness($color) < 60) {
      color: $white;
      .label {
        background-color: #eeeeee;
        color: #555;
      }
    }
  }
}

// define backgrounds
@include bg-color-variant(".bg-white", $white);
@include bg-color-variant(".bg-grey", $grey);
@include bg-color-variant(".bg-black", $black);
@include bg-color-variant(".bg-blue", $blue);
@include bg-color-variant(".bg-indigo", $indigo);
@include bg-color-variant(".bg-purple", $purple);
@include bg-color-variant(".bg-green", $green);
@include bg-color-variant(".bg-red", $red);
@include bg-color-variant(".bg-orange", $orange);
@include bg-color-variant(".bg-yellow", $yellow);

// spacing css
.mb-3 { margin-bottom: 1rem; }
.pad-1 { padding: 1rem; }
              
            
!

JS

              
                // get the style of an element
function getStyle(el, styleProp) {
  var value, defaultView = (el.ownerDocument || document).defaultView;
  // W3C standard way:
  if (defaultView && defaultView.getComputedStyle) {
    // sanitize property name to css notation
    // (hypen separated words eg. font-Size)
    styleProp = styleProp.replace(/([A-Z])/g, "-$1").toLowerCase();
    return defaultView.getComputedStyle(el, null).getPropertyValue(styleProp);
  } else if (el.currentStyle) { // IE
    // sanitize property name to camelCase
    styleProp = styleProp.replace(/\-(\w)/g, function(str, letter) {
      return letter.toUpperCase();
    });
    value = el.currentStyle[styleProp];
    // convert other units to pixels on IE
    if (/^\d+(em|pt|%|ex)?$/i.test(value)) {
      return (function(value) {
        var oldLeft = el.style.left, oldRsLeft = el.runtimeStyle.left;
        el.runtimeStyle.left = el.currentStyle.left;
        el.style.left = value || 0;
        value = el.style.pixelLeft + "px";
        el.style.left = oldLeft;
        el.runtimeStyle.left = oldRsLeft;
        return value;
      })(value);
    }
    return value;
  }
}

// convert rgba to hex
function hexc(colorval) {
    var parts = colorval.match(/^rgb\((\d+),\s*(\d+),\s*(\d+)\)$/);
    delete(parts[0]);
    for (var i = 1; i <= 3; ++i) {
        parts[i] = parseInt(parts[i]).toString(16);
        if (parts[i].length == 1) parts[i] = '0' + parts[i];
    }
    color = '#' + parts.join('');
}

document.addEventListener("DOMContentLoaded", function(event) {
  
  let els = document.querySelectorAll('.color');
  els.forEach(function(el, i) {
    console.log(el)
    var backgroundColorRGBA = getStyle(el, "background-color");
    
    // hack to convert rgba to hex
    var ctx = document.createElement('canvas').getContext('2d');
ctx.strokeStyle = backgroundColorRGBA;
var hexVal = ctx.strokeStyle;
    
    el.innerHTML += '<div class="mt-2 label"><small>' + hexVal + '</small></div>';
  })
  
});
              
            
!
999px

Console