Balance
import { useState } from 'react';
const css = `
.slider-balance { display: grid; justify-items: center; gap: 12px; width: 220px; }
.slider-balance__scale { position: relative; width: 200px; height: 108px; }
.slider-balance__post { position: absolute; left: 50%; bottom: 0; width: 4px; height: 70px; margin-left: -2px; background: light-dark(#6d7179, #7d8087); }
.slider-balance__post::after { content: ''; position: absolute; left: -16px; bottom: 0; width: 36px; height: 5px; border-radius: 3px; background: light-dark(#6d7179, #7d8087); }
/* Tilt follows the split: the heavier side goes down, a little past where it settles. */
.slider-balance__beam { position: absolute; left: 20px; right: 20px; top: 34px; height: 4px; border-radius: 2px; background: light-dark(#50535b, #c6c8cd); transform: rotate(calc((0.5 - var(--slider-balance-v)) * 22deg)); transition: transform 0.7s cubic-bezier(0.3, 1.6, 0.5, 1); }
.slider-balance__beam::before { content: ''; position: absolute; left: 50%; top: -4px; width: 12px; height: 12px; margin-left: -6px; border-radius: 50%; background: light-dark(#42454c, #e5e6e9); }
.slider-balance__pan { position: absolute; top: 2px; width: 54px; height: 50px; margin-left: -27px; transform-origin: 50% 0; transform: rotate(calc((0.5 - var(--slider-balance-v)) * -22deg)); transition: transform 0.7s cubic-bezier(0.3, 1.6, 0.5, 1); }
.slider-balance__pan--l { left: 0; }
.slider-balance__pan--r { left: 100%; }
.slider-balance__pan::before { content: ''; position: absolute; left: 26px; top: 0; width: 1px; height: 30px; background: light-dark(#6d7179, #7d8087); }
.slider-balance__dish { position: absolute; left: 0; right: 0; top: 30px; height: 6px; border-radius: 0 0 27px 27px; background: light-dark(#5f626a, #a7abb2); }
.slider-balance__load { position: absolute; left: 50%; bottom: 20px; width: 22px; margin-left: -11px; border-radius: 3px; background: light-dark(#42454c, #e5e6e9); transition: height 0.4s cubic-bezier(0.3, 1.4, 0.5, 1); }
.slider-balance__pan--l .slider-balance__load { height: calc(4px + var(--slider-balance-v) * 22px); }
.slider-balance__pan--r .slider-balance__load { height: calc(4px + (1 - var(--slider-balance-v)) * 22px); background: light-dark(#6d7179, #7d8087); }
.slider-balance__tag { position: absolute; top: 40px; width: 60px; margin-left: -30px; color: light-dark(#5f626a, #a7abb2); font-size: 10.5px; text-align: center; }
@media (prefers-reduced-motion: reduce) {
.slider-balance * { transition: none !important; }
}
.slider-balance__read { color: light-dark(#50535b, #c6c8cd); font-size: 12px; font-variant-numeric: tabular-nums; }
.slider-balance__read b { color: light-dark(#373a40, #f1f2f3); font-weight: 600; }
.slider-balance__track { position: relative; width: 100%; height: 24px; }
.slider-balance__rail { position: absolute; left: 0; right: 0; top: 10px; height: 4px; border-radius: 2px; background: light-dark(#ebedf0, #232529); }
.slider-balance__fill { position: absolute; left: 0; top: 10px; height: 4px; border-radius: 2px; width: calc(10px + var(--slider-balance-v) * (100% - 20px)); background: light-dark(#50535b, #c6c8cd); }
.slider-balance__thumb { position: absolute; top: 2px; left: calc(var(--slider-balance-v) * (100% - 20px)); width: 20px; height: 20px; border-radius: 50%; background: light-dark(#373a40, #f1f2f3); box-shadow: 0 2px 6px light-dark(rgba(0, 0, 0, 0.12), rgba(0, 0, 0, 0.4)); }
.slider-balance:has(.slider-balance__input:focus-visible) .slider-balance__thumb { outline: 2px solid light-dark(#50535b, #c6c8cd); outline-offset: 3px; }
.slider-balance__input { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: pointer; -webkit-appearance: none; appearance: none; background: none; }
.slider-balance__input::-webkit-slider-thumb { -webkit-appearance: none; width: 20px; height: 20px; }
.slider-balance__input::-moz-range-thumb { width: 20px; height: 20px; border: 0; }
`;
export function BalanceSlider() {
const [v, setV] = useState(30);
const t = (v - 0) / (100 - 0);
return (
<>
<style>
{css}
</style>
<div className="slider-balance" style={{ '--slider-balance-v': t } as React.CSSProperties}>
<div className="slider-balance__scale" aria-hidden="true">
<span className="slider-balance__post" />
<span className="slider-balance__beam">
<span className="slider-balance__pan slider-balance__pan--l">
<span className="slider-balance__load" />
<span className="slider-balance__dish" />
<span className="slider-balance__tag" style={{ left: 27 }}>Save</span>
</span>
<span className="slider-balance__pan slider-balance__pan--r">
<span className="slider-balance__load" />
<span className="slider-balance__dish" />
<span className="slider-balance__tag" style={{ left: 27 }}>Spend</span>
</span>
</span>
</div>
<span className="slider-balance__read" aria-hidden="true">
Save <b>{v}%</b> · Spend <b>{100 - v}%</b>
</span>
<div className="slider-balance__track">
<span className="slider-balance__rail" />
<span className="slider-balance__fill" />
<span className="slider-balance__thumb" />
<input
className="slider-balance__input"
type="range"
min={0}
max={100} step={5}
value={v}
onChange={(e) => setV(+e.target.value)}
aria-label="Save"
aria-valuetext={`Save ${v} percent, spend ${100 - v} percent`}
/>
</div>
</div>
</>
);
}Markup where the component goes, CSS in your stylesheet, script before </body>. In Vue, Svelte or Angular, call initSliderBalance(el) on the mounted element instead.
<div class="slider-balance" style="--slider-balance-v:0.3">
<div class="slider-balance__scale" aria-hidden="true">
<span class="slider-balance__post"></span>
<span class="slider-balance__beam">
<span class="slider-balance__pan slider-balance__pan--l">
<span class="slider-balance__load"></span>
<span class="slider-balance__dish"></span>
<span class="slider-balance__tag" style="left:27px">Save</span>
</span>
<span class="slider-balance__pan slider-balance__pan--r">
<span class="slider-balance__load"></span>
<span class="slider-balance__dish"></span>
<span class="slider-balance__tag" style="left:27px">Spend</span>
</span>
</span>
</div>
<span class="slider-balance__read" aria-hidden="true">Save <b>30%</b> · Spend <b>70%</b></span>
<div class="slider-balance__track">
<span class="slider-balance__rail"></span>
<span class="slider-balance__fill"></span>
<span class="slider-balance__thumb"></span>
<input class="slider-balance__input" type="range" min="0" max="100" step="5" aria-label="Save" aria-valuetext="Save 30 percent, spend 70 percent" value="30"/>
</div>
</div>.slider-balance { display: grid; justify-items: center; gap: 12px; width: 220px; }
.slider-balance__scale { position: relative; width: 200px; height: 108px; }
.slider-balance__post { position: absolute; left: 50%; bottom: 0; width: 4px; height: 70px; margin-left: -2px; background: light-dark(#6d7179, #7d8087); }
.slider-balance__post::after { content: ''; position: absolute; left: -16px; bottom: 0; width: 36px; height: 5px; border-radius: 3px; background: light-dark(#6d7179, #7d8087); }
/* Tilt follows the split: the heavier side goes down, a little past where it settles. */
.slider-balance__beam { position: absolute; left: 20px; right: 20px; top: 34px; height: 4px; border-radius: 2px; background: light-dark(#50535b, #c6c8cd); transform: rotate(calc((0.5 - var(--slider-balance-v)) * 22deg)); transition: transform 0.7s cubic-bezier(0.3, 1.6, 0.5, 1); }
.slider-balance__beam::before { content: ''; position: absolute; left: 50%; top: -4px; width: 12px; height: 12px; margin-left: -6px; border-radius: 50%; background: light-dark(#42454c, #e5e6e9); }
.slider-balance__pan { position: absolute; top: 2px; width: 54px; height: 50px; margin-left: -27px; transform-origin: 50% 0; transform: rotate(calc((0.5 - var(--slider-balance-v)) * -22deg)); transition: transform 0.7s cubic-bezier(0.3, 1.6, 0.5, 1); }
.slider-balance__pan--l { left: 0; }
.slider-balance__pan--r { left: 100%; }
.slider-balance__pan::before { content: ''; position: absolute; left: 26px; top: 0; width: 1px; height: 30px; background: light-dark(#6d7179, #7d8087); }
.slider-balance__dish { position: absolute; left: 0; right: 0; top: 30px; height: 6px; border-radius: 0 0 27px 27px; background: light-dark(#5f626a, #a7abb2); }
.slider-balance__load { position: absolute; left: 50%; bottom: 20px; width: 22px; margin-left: -11px; border-radius: 3px; background: light-dark(#42454c, #e5e6e9); transition: height 0.4s cubic-bezier(0.3, 1.4, 0.5, 1); }
.slider-balance__pan--l .slider-balance__load { height: calc(4px + var(--slider-balance-v) * 22px); }
.slider-balance__pan--r .slider-balance__load { height: calc(4px + (1 - var(--slider-balance-v)) * 22px); background: light-dark(#6d7179, #7d8087); }
.slider-balance__tag { position: absolute; top: 40px; width: 60px; margin-left: -30px; color: light-dark(#5f626a, #a7abb2); font-size: 10.5px; text-align: center; }
@media (prefers-reduced-motion: reduce) {
.slider-balance * { transition: none !important; }
}
.slider-balance__read { color: light-dark(#50535b, #c6c8cd); font-size: 12px; font-variant-numeric: tabular-nums; }
.slider-balance__read b { color: light-dark(#373a40, #f1f2f3); font-weight: 600; }
.slider-balance__track { position: relative; width: 100%; height: 24px; }
.slider-balance__rail { position: absolute; left: 0; right: 0; top: 10px; height: 4px; border-radius: 2px; background: light-dark(#ebedf0, #232529); }
.slider-balance__fill { position: absolute; left: 0; top: 10px; height: 4px; border-radius: 2px; width: calc(10px + var(--slider-balance-v) * (100% - 20px)); background: light-dark(#50535b, #c6c8cd); }
.slider-balance__thumb { position: absolute; top: 2px; left: calc(var(--slider-balance-v) * (100% - 20px)); width: 20px; height: 20px; border-radius: 50%; background: light-dark(#373a40, #f1f2f3); box-shadow: 0 2px 6px light-dark(rgba(0, 0, 0, 0.12), rgba(0, 0, 0, 0.4)); }
.slider-balance:has(.slider-balance__input:focus-visible) .slider-balance__thumb { outline: 2px solid light-dark(#50535b, #c6c8cd); outline-offset: 3px; }
.slider-balance__input { position: absolute; inset: 0; width: 100%; height: 100%; margin: 0; opacity: 0; cursor: pointer; -webkit-appearance: none; appearance: none; background: none; }
.slider-balance__input::-webkit-slider-thumb { -webkit-appearance: none; width: 20px; height: 20px; }
.slider-balance__input::-moz-range-thumb { width: 20px; height: 20px; border: 0; }function initSliderBalance(root) {
var input = root.querySelector('.slider-balance__input');
var read = root.querySelector('.slider-balance__read');
function update() {
var v = +input.value;
var t = (v - 0) / (100 - 0);
root.style.setProperty('--slider-balance-v', t);
read.innerHTML = 'Save <b>' + v + '%</b> · Spend <b>' + (100 - v) + '%</b>';
input.setAttribute('aria-valuetext', 'Save ' + v + ' percent, spend ' + (100 - v) + ' percent');
}
input.addEventListener('input', update);
update();
return function () { input.removeEventListener('input', update); };
}
document.querySelectorAll('.slider-balance').forEach(initSliderBalance);Goal: Create a "Balance" slider.
Visual behavior: A save and spend split drawn as a balance scale: the heavier pan tips down, overshooting a little before it settles, and the reading shows both shares.
Technique:
- A real <input type="range"> lies over the drawing, transparent (opacity 0, appearance none), with its thumb hit area sized to the drawn thumb, so dragging, clicking the track and the arrow, Page Up/Down, Home and End keys all work natively. The drawing is aria-hidden.
- The value is written to one CSS custom property (0 to 1) on the root; the beam is rotated by (0.5 - value) * 22deg on an overshooting transition, and each pan counter-rotates by the same angle, so it always hangs straight down.
- The load in each pan grows with its own share.
- :has(input:focus-visible) outlines the drawn thumb, so keyboard focus is visible though the input itself is not.
Accessibility: A native range input with an aria-label and an aria-valuetext in words, so a screen reader announces the reading rather than a bare number; keyboard focus outlines the drawn thumb. Under prefers-reduced-motion the beam tilts without easing.
My stack: {{USER_STACK}}
My styling: {{USER_STYLING}}
My constraints: {{USER_CONSTRAINTS}}
My colors: neutral only. Black, white and grays, no hues. Use the color values above as written, or my project's closest neutral tokens.
Return a single self-contained component. No dependencies.Goal: Create a "Balance" slider.
Visual behavior: A save and spend split drawn as a balance scale: the heavier pan tips down, overshooting a little before it settles, and the reading shows both shares.
Technique:
- A real <input type="range"> lies over the drawing, transparent (opacity 0, appearance none), with its thumb hit area sized to the drawn thumb, so dragging, clicking the track and the arrow, Page Up/Down, Home and End keys all work natively. The drawing is aria-hidden.
- The value is written to one CSS custom property (0 to 1) on the root; the beam is rotated by (0.5 - value) * 22deg on an overshooting transition, and each pan counter-rotates by the same angle, so it always hangs straight down.
- The load in each pan grows with its own share.
- :has(input:focus-visible) outlines the drawn thumb, so keyboard focus is visible though the input itself is not.
Accessibility: A native range input with an aria-label and an aria-valuetext in words, so a screen reader announces the reading rather than a bare number; keyboard focus outlines the drawn thumb. Under prefers-reduced-motion the beam tilts without easing.
My stack: {{USER_STACK}}
My styling: {{USER_STYLING}}
My constraints: {{USER_CONSTRAINTS}}
My colors: use my project's existing color tokens. The color values above are grayscale placeholders. Map each one by its role (surface, text, border, accent, glow) to my theme, and keep their relative lightness so contrast and the effect still read.
Return a single self-contained component. No dependencies.Paste into Claude, ChatGPT or Cursor after filling in {{USER_STACK}}, {{USER_STYLING}} and {{USER_CONSTRAINTS}}. Neutral keeps the greys you see here; Match my project asks the model to map them onto your own colour tokens.
Free to use in any project, commercial included. MIT-0, no attribution required. Licence
About this component
A save and spend split drawn as a balance scale: the heavier pan tips down, overshooting a little before it settles, and the reading shows both shares.
- Dependencies
- None
- Difficulty
- Beginner
- Triggers
- state change, spring
- Techniques
- css variables, css transition, transform only
- Reduced motion
- Respected
- Theme
- Light and dark (follows your page's color-scheme)
A native range input with an aria-label and an aria-valuetext in words, so a screen reader announces the reading rather than a bare number; keyboard focus outlines the drawn thumb. Under prefers-reduced-motion the beam tilts without easing.