123

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.

            
              <h1>CSS level 4 color contrast adjuster just with custom properties and calc()</h1>

<form>
  <label><strong>Background lightness</strong></label><input type="range" name="--bg-l" value="35"/><code></code>
  <label>Background hue</label><input type="range" name="--bg-h" value="180" min="0" max="360"/><code></code>
  <label>Background saturation</label><input type="range" name="--bg-s" value="100"/><code></code>
  <label><strong>Contrast ratio</strong></label><input type="range" name="--contrast-ratio" value="30"/><code></code>
</form>
            
          
!
            
              /*
  The CSS level 4 contrast adjuster mimicked with just custom properties and calc().
  While it's pretty straightforward to mimick the hue, saturation and
  lightness adjusters, color contrast requires some "serious" math.
  Results are not perfect, but very much usable IMHO.
  Syntax of course is even more bloated than for the simple adjusters mentioned above.
*/
body {
  /* Actual values set from JS */
  --bg-h: 0; --bg-s: 0; --bg-l: 0; /* Background hue, saturation, lightness. */
  /* The ratio for the contrast() adjuster,
     see https://www.w3.org/TR/css-color-4/#modifying-colors */
  --contrast-ratio: 0%;
}
h1 {
  /* If you see a red foreground,
     you might have a syntax error somwehere below this. */
  color: hsl(0, 100%, 50%);

  /* This is how colors need to be specified.
     Conversion of saturation and lightness to percentage done here
     to enable prior calculations. */
  background-color: hsl(
    var(--bg-h),
    calc(var(--bg-s) * 1%),
    calc(var(--bg-l) * 1%)
  );

  /* The foreground color with contrast adjuster. */
  color: hsl(
    var(--bg-h),
    calc(var(--bg-s) * 1%),
    /* Calculate the lightness:
       1. Take the 1/x function
       2. Turn it upside down: -1/x
       2. Adjust it to the x and y axis, but avoid -Infinity being hit,
          since it tends to act funny when used in further calculations
          (make sure the input lightness value is rounded to at most 2 decimal points):
           -1/(x-49.999)+x
       3. Make it less curved, so the y-axis makes sense between 0 and 100:
          -2500/(x-49.999)+x
       4. Adjust the curve with the contrast ratio,
          but make sure we're never multiplying by 0: (-2500*(c/100+1))/(x-49.999)+x
    */
    calc((-2500 * (var(--contrast-ratio) * 1% / 100 + 1%)) / (var(--bg-l) - 49.999) + var(--bg-l) * 1%)
  );
}

/* A few demo styles. */
body {
  font-family: system, -apple-system, ".SFNSText-Regular", "San Francisco", "Roboto", "Segoe UI", "Helvetica Neue", "Lucida Grande", sans-serif;
}
form {
  display: grid;
  grid-template-columns: minmax(min-content, max-content) max-content max-content;
  grid-gap: .5em;
  align-items: center;
}
code { text-align: right }
h1 {
  font-weight: bold;
  font-size: 26px;
  padding: 1em;
}
            
          
!
            
              const updateStyle = element => document.body.style.setProperty(element.name, element.value) || (element.nextElementSibling.innerText = element.value)
Array.from(document.querySelectorAll('input')).forEach(input => updateStyle(input))
document.addEventListener('input', ({ target }) => requestAnimationFrame(() => updateStyle(target)))
            
          
!
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