css-var-bind

Bind HTML inputs to CSS custom properties

View on GitHub

Interactive Demo

This entire page is styled using CSS custom properties. Use the control panel in the top-right corner to adjust the design in real-time. Every change you make demonstrates css-var-bind in action.

Features

A lightweight web component that binds HTML input elements to CSS custom properties (variables).

Installation

npm install css-var-bind

Then import in your JavaScript:

import 'css-var-bind';

Basic Usage

Wrap any input element with <css-var-bind> and specify the CSS variable name.

<css-var-bind variable="--scale" target=":root" strategy="global">
  <input type="range" value="1" min="0.5" max="2" step="0.1">
</css-var-bind>

The component automatically updates the CSS variable when inputs change, syncs values between multiple inputs, and applies units to values.

API Reference

Attributes

Attribute Required Default Description
variable Yes - CSS custom property name (must start with --)
unit No "" Unit to append (e.g., px, rem, %, deg)
target No - CSS selector for the element to set the variable on
strategy No "closest" How to resolve the target: "closest", "global", or "self"

Strategy Options

Examples

Color Picker

<css-var-bind target="body" variable="--bg-color">
  <input type="color" value="#b4d455" />
</css-var-bind>

Scoped to Ancestor

<div class="card">
  <css-var-bind variable="--card-padding" unit="rem" target=".card">
    <input type="number" min="0" max="5" value="2" />
  </css-var-bind>
</div>

Self-Binding

<css-var-bind variable="--opacity" strategy="self">
  <input type="range" min="0" max="1" step="0.1" value="1" />
</css-var-bind>
Controls text color and primary elements
Page background color
Button, link, and highlight color
All border colors throughout the page
Base font size that scales all text
Vertical spacing between lines of text
Space between characters
Roundness of buttons, panels, and inputs
Multiplier for all padding and margins
Maximum width of content