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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="wrapper">
  <div class="pickers">
    <div id="primary" acp-color="#2f30d8" acp-palette="black|white"></div>
    <div class="app">
      <div class="app-hero">
        <div class="app-menu">
          <div class="menu-top"></div>
          <div class="menu-bottom"></div>
        </div>
        <div class="section-font">
    <div id="text-test" class="text-test"></div>

  </div>
      </div>
      <div class="result">
        <div class="result-top">
          <div>Level: <span id="level"></span></div>
          <div>ratio: <span id="ratio" style="color: green">21</span></div>          
        </div>
        <div class="result-center">brightness: <span id="brightness">Black </span>text is not legible</div>
      </div>
      <div class="footer">
    <div class="info-trigger">
    <i class="fas fa-info-circle"></i>
    <div class="info">Level AA requires a contrast ratio of at least 4.5:1 for normal text and 3:1 for large text, and a contrast ratio of at least 3:1 for graphics and user interface components (such as form input borders). Level AAA requires a contrast ratio of at least 7:1 for normal text and 4.5:1 for large text.
<br><br>
Large text is defined as 14 point (typically 18.66px) and bold or larger, or 18 point (typically 24px) or larger.</div> 
  </div>
    <select id="select-font">
      <option value="18px">18px</option>
      <option value="24px">24px</option>
    </select>
        <p class="select-label">Change Font Size</p>
  </div>
    </div>
    <div id="font" acp-color="#fff" acp-palette="black|white"></div>
  </div>
</div>


            
          
!
            
              :root {
  --background-color: #2f30d8;
  --font-color: #fff;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body, html {
  background: linear-gradient(#eee, #ddd);
  font-family: 'Montserrat', sans-serif;
  height: 100%;
}

.wrapper {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: space-evenly;
  position: relative;
  overflow: hidden;
  height: 100%;
  padding: 10px;
}

.section-font {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
}

.text-test {
  margin-right: 10px;
  color: var(--font-color);
}

.result {
  font-size: 14px;
  text-transform: uppercase;
  font-weight: 600;
  padding: 20px;
}

.result-top {
  display: flex;
  justify-content: space-between;
}

.result-center {
  margin-top: 50px;
  text-align: center;
}

.pickers {
  display: flex;
  width: 100%;
  height: 90%;
  justify-content: space-evenly;
}

.a-color-picker {
  position: relative;
}

#primary .a-color-picker:before {
  position: absolute;
  content:'Background';
  bottom: 10px;
  right: 10px;
}

#font .a-color-picker:before {
  position: absolute;
  content:'Foreground';
  bottom: 10px;
  right: 10px;
}

.a-color-picker-palette-color {
  border: 1px solid #ccc;
}

.info-trigger {
  position: relative;
  cursor: pointer;
  margin-right: 20px;
}

.info {
  position: absolute;
  bottom: 110%;
  left: 0;
  width: 300px;
  padding: 20px;
  background-color: white;
  border-radius: 4px;
  border: 1px solid #ccc;
  color: black;
  display: none;
  z-index: 1000;
}

.info.active {
  display: block;
}


.app {
  background-color: #fff;
  height: 500px;
  width: 260px;
  border-radius: 20px;
  box-shadow: 0 0 3px #8e8e8e;
  position: relative;
}

.app-hero {
  height: 50%;
  width: 100%;
  background-color: var(--background-color);
  border-radius: 20px;
  border-bottom-right-radius: 0;
  border-bottom-left-radius: 50px;
  display: flex;
}

.app-menu {
  position: absolute;
  width: 30px;
  height: 30px;
  margin: 20px;
}

.menu-top {
  width: 100%;
  height: 4px;
  background-color: var(--font-color);
  border-radius: 100px;
}

.menu-bottom {
  width: 70%;
  margin-top: 5px;
  height: 4px;
  background-color: var(--font-color);
  border-radius: 100px;
}

.footer {
  width: 100%;
  height: 50px;
  padding: 20px;
  position: absolute;
  bottom: 0;
  display: flex;
  justify-content: flex-start;
  align-items: center;
}

.select-label {
  padding-left: 10px;
  font-size: 10px;
}
            
          
!
            
              var primary = document.getElementById('primary');
var font = document.getElementById('font');
var text = document.getElementById('text-test');
var select = document.getElementById('select-font');
var level = document.getElementById('level');
var infoTrigger = document.querySelector('.info-trigger');
var info = document.querySelector('.info');
var topMenu = document.querySelector('.menu-top');
var bottomMenu = document.querySelector('.menu-bottom');
var colorFont = '#fff';
var colorPrimary = '#2f30d8';
var fontSize = select.options[select.selectedIndex].value;
var resultRatio = 21;

// Set Defaults
level.textContent = "AAA";
level.style.color = 'green';
text.textContent = "Happy Bazinga!"

infoTrigger.addEventListener('mouseover', function(){
  if(!info.classList.contains('active')) {
    info.classList.add('active');
  }
});

infoTrigger.addEventListener('mouseleave', function(){
  if(info.classList.contains('active')) {
    info.classList.remove('active');
  }
});

select.addEventListener('input', function(){
  fontSize = select.options[select.selectedIndex].value;
  text.style.fontSize = fontSize;
  levelWCAG(resultRatio);
})

AColorPicker.from('#primary').on('change', (picker, color) => {
  var ratio = document.getElementById('ratio');
  var brightness = document.getElementById('brightness');
  colorPrimary = picker.rgbhex;
  document.documentElement.style.setProperty('--background-color', color);
  
  resultRatio = getContrast(colorPrimary, colorFont);
  var resultBrightness = getBrightness(colorPrimary);

  ratio.textContent = resultRatio;

  levelWCAG(resultRatio);
  
  if(resultBrightness < 125) {
       brightness.textContent = "Black ";
       topMenu.style.background = "#fff";
       bottomMenu.style.background = "#fff";
     } else {
       brightness.textContent = "White ";
       topMenu.style.background = "#000";
       bottomMenu.style.background = "#000";
     }
  return colorPrimary;
  return resultRatio;
});

AColorPicker.from('#font').on('change', (picker, color) => {
  var ratio = document.getElementById('ratio');
  colorFont = picker.rgbhex;

  resultRatio = getContrast(colorPrimary, colorFont);

  ratio.textContent = resultRatio;
  document.documentElement.style.setProperty('--font-color', color);

  levelWCAG(resultRatio);
  
  return colorFont;
  return resultRatio;
});

var getContrast = function(background, foreground) {

  // Convert hex to rgb
  // Credit to Denis http://stackoverflow.com/a/36253499/4939630
  var backgroundRGB = 'rgb(' + (background = background.replace('#', '')).match(new RegExp('(.{' + background.length/3 + '})', 'g')).map(function(l) { return parseInt(background.length%2 ? l+l : l, 16); }).join(',') + ')';

  var foregroundRGB = 'rgb(' + (foreground = foreground.replace('#', '')).match(new RegExp('(.{' + foreground.length/3 + '})', 'g')).map(function(l) { return parseInt(foreground.length%2 ? l+l : l, 16); }).join(',') + ')';

  // Get array of RGB values
  var backgroundRGBArray = backgroundRGB.replace(/[^\d,]/g, '').split(',');
  var foregroundRGBArray = foregroundRGB.replace(/[^\d,]/g, '').split(',');

  function luminance(r, g, b) {
      var a = [r, g, b].map(function (v) {
          v /= 255;
          return v <= 0.03928 ? v / 12.92 : Math.pow( (v + 0.055) / 1.055, 2.4 );
      });
      return a[0] * 0.2126 + a[1] * 0.7152 + a[2] * 0.0722;
  }

  function floor(number, decimals) {
    decimals = +decimals || 0;

    var multiplier = Math.pow(10, decimals);

    return Math.floor(number * multiplier) / multiplier;
}

  function contrast(el1, el2) {
    l1 = (luminance(el1[0], el1[1], el1[2]) + 0.05);
    l2 =(luminance(el2[0], el2[1], el2[2]) + 0.05);
    ratio = l1/l2;

    if (l2 > l1) {
      ratio = 1 / ratio;
    } 
    
    return floor(ratio, 2);
  }
  
  var contrast = contrast(backgroundRGBArray, foregroundRGBArray);
  return contrast;
}

var getBrightness = function(hex) {

    // Convert hex to rgb
    // Credit to Denis http://stackoverflow.com/a/36253499/4939630
    var rgb = 'rgb(' + (hex = hex.replace('#', '')).match(new RegExp('(.{' + hex.length/3 + '})', 'g')).map(function(l) { return parseInt(hex.length%2 ? l+l : l, 16); }).join(',') + ')';

    // Get array of RGB values
    rgb = rgb.replace(/[^\d,]/g, '').split(',');

    var r = rgb[0], g = rgb[1], b = rgb[2];

    var brightness = Math.floor((r * 299 + g * 587 + b * 114) / 1000);
    
    return brightness;
}

var levelWCAG = function(resultRatio){
   var ratio = document.getElementById('ratio');
   if(resultRatio >= 4.5 && fontSize === "18px" && resultRatio < 7) {
      ratio.style.color = 'green';
      level.textContent = "AA";
      text.textContent = "Bazinga!";
     } else if(resultRatio >= 3 && fontSize === "24px" && resultRatio < 4.5) {
      ratio.style.color = 'green';
      level.textContent = "AA";
      text.textContent = "Bazinga!";
     } else if(resultRatio >= 7 && fontSize === "18px") {
      ratio.style.color = 'green';
      level.textContent = "AAA";
      text.textContent = "Happy Bazinga!";
     } else if(resultRatio >= 4.5 && fontSize === "24px") {
      ratio.style.color = 'green';
      level.textContent = "AAA";
      text.textContent = "Happy Bazinga!";
     } else {
      ratio.style.color = 'red';
      level.textContent = "Nope";
      text.textContent = "Sad Bazinga...";
     }
  
     if(level.textContent !== "Nope") {
       level.style.color = 'green';
     } else {
       level.style.color = 'black';
     }
}
            
          
!
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.

Console