Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URLs 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 its URL and the proper URL extension.

+ 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

Auto Save

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

              
                <h1><span>Interesting CSS Color Contrast Technique</span></h1>

<svg xmlns="http://www.w3.org/2000/svg" version="1.1" height="0">
    <defs>
        <filter id="bwFilter" color-interpolation-filters="sRGB">
            <!-- Convert to grayscale based on luminance -->
            <feColorMatrix type="matrix"
                values="0.2126 0.7152 0.0722 0 0
                        0.2126 0.7152 0.0722 0 0
                        0.2126 0.7152 0.0722 0 0
                        0 0 0 1 0"/>
            <!-- Expand edges slightly to clean up any fringing -->
            <feMorphology operator="dilate" radius="2"/>
            <!-- Apply the threshold to determine if the color should be black or white -->
            <feComponentTransfer>
                <feFuncR type="linear" slope="-255" intercept="128"/>
                <feFuncG type="linear" slope="-255" intercept="128"/>
                <feFuncB type="linear" slope="-255" intercept="128"/>
            </feComponentTransfer>
            <!-- Composite step to clean up the result -->
            <feComposite operator="in" in2="SourceGraphic"/>
        </filter>
    </defs>
</svg>




<form hidden>  

  <ground-control set-prop="--h">
    <label for="h">Hue</label>
    <input id="h" type="range" min="0" max="360" value="0">
    <output for="h"></output>
  </ground-control>

  <ground-control set-prop="--s">
    <label for="s">Saturation</label>
    <input id="s" type="range" min="0" max="100" value="50">
    <output for="s"></output>
  </ground-control>

  <ground-control set-prop="--l">
    <label for="l">Lightness</label>
    <input id="l" type="range" min="0" max="100" value="50">
    <output for="l"></output>
  </ground-control>  
</form>

<footer>
  <p>
    A technique by <a href="https://meow.social/@miunau/111911373727499267">miunau</a>. This one uses SVG filters.
    Forked from <a href="https://codepen.io/miriamsuzanne/pen/yLwRpGQ?editors=1100">mia's codepen of the pure CSS filter version</a>.
  </p>
</footer>
              
            
!

CSS

              
                html {
  background: var(--bgColor);
}

body {
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

h1 {
  color: var(--bgColor);
  filter: url(#bwFilter);

}

@layer setup {

  html {
    --bgColor: hsl(
      calc(var(--h) * 1deg)
      calc(var(--s) * 1%) 
      calc(var(--l) * 1%) 
    );
  }
  
  body {
    display: grid;
    place-content: center;
    font-size: clamp(1.25rem, 1.163rem + 0.4348vi, 1.5rem);
    margin: 0;
    padding: 1em;
  }

  form, footer {
    background: Canvas;
    border-radius: 6px;
    color-scheme: dark light;
    color: CanvasText;  
    max-width: 70ch;
    opacity: 0.95;
    padding: 1em;
  }

  form {
    display: grid;
    gap: 0.5lh;
  }

  footer {
    margin-block: 1em;
  }

  ground-control {
    display: grid;
    gap: 0.25lh 1em;
    grid-template: 'l l' auto 'r o' auto / 1fr 4ch;
  }

  label {
    grid-area: l;
  }
  
  output {
    text-align: right;
    font-family: monospace;
  }

  hr {
    margin-inline: 0;
  }

  [hidden] {
    display: none !important;
  }
}
              
            
!

JS

              
                document.querySelector('form').removeAttribute('hidden');

class groundControl extends HTMLElement {
  static observedAttributes = [
    'control-for',
    'set-attr',
    'set-prop',
  ];

  static attrToPropMap = {
    'control-for': 'targetSelect',
    'set-attr': 'setAttr',
    'set-prop': 'setProp',
  };

  static controls = [
    'input',
    'select'
  ];

  inputEl;
  resetValue;
  targetEls;
  displayEls;
  setProp;
  setAttr;

  constructor() {
    super();

    // Add direct event listeners when component is created
    this.addEventListener('reset', (e) => this.resetControl());
  }

  // attribute changes
  attributeChangedCallback(name, oldValue, newValue) {
    this[groundControl.attrToPropMap[name]] = newValue;
    if (this.inputEl) { this.setUp(); }
  }

  // first connection
  connectedCallback() {
    this.inputEl = this.querySelector(groundControl.controls.join(', '));

    if (this.inputEl) {
      this.setUp();
      this.broadCast();

      this.inputEl.addEventListener('input', (e) => this.broadCast());
      this.resetBtn?.addEventListener('click', (e) => this.resetControl());
    } else {
      console.error("Your circuit's dead, there's something wrong (no input/select found)");
    }
  }

  disconnectedCallback() {
    this.inputEl.removeEventListener('input', (e) => this.broadCast());
    this.resetBtn?.removeEventListener('click', (e) => this.resetControl());
  }

  // methods
  setUp = () => {
    this.resetValue = this.inputEl.value;

    const selectors = {
      target: this.targetSelect || ':root',
      output: `output[for=${this.inputEl.id}]`,
      reset: `button[reset-for=${this.inputEl.id}]`,
    };

    this.targetEls = document.querySelectorAll(selectors.target);
    this.displayEls = document.querySelectorAll(selectors.output);
    this.resetBtn = this.querySelector(selectors.reset);
  };

  // change target attrs/props, and update output displayEls
  broadCast = () => {
    this.targetEls.forEach((el) => {
      if (this.setProp) {
        el.style.setProperty(this.setProp, this.inputEl.value);
      }
      if (this.setAttr) {
        el.setAttribute(this.setAttr, this.inputEl.value);
      }
    });

    this.displayEls.forEach((el) => {
      const units = el.getAttribute('with-units') || '';
      el.value = `${this.inputEl.value}${units}`;
    });
  };

  // reset the value of the control
  resetControl = (value) => {
    this.inputEl.value = value || this.resetValue;
    this.broadCast();
  };
}

// register the ground-control element
customElements.define("ground-control", groundControl);

              
            
!
999px

Console