Gradient-Fill Value Slider — HTML CSS JS Snippet

Gradient-Fill Value Slider · Forms · Plain HTML, CSS & JS · Live preview

What's included

Features

Thumb border and tooltip color are computed, not hardcoded — they sample the same gradient the fill bar shows
A small from-scratch linear-interpolation function reproduces the CSS gradient's exact color at any percentage
Live tooltip above the thumb shows both the numeric value and the matching gradient color as its background
Track click-to-jump is a separate, guarded interaction from thumb dragging
Full keyboard support with fine (1) and coarse (Shift+10) step sizes
Unified pointer and touch dragging with touch-action: none preventing scroll interference

About this UI Snippet

Gradient-Fill Value Slider — Sampling the Same Gradient the Fill Bar Uses

Screenshot of the Gradient-Fill Value Slider snippet rendered live

The fill bar's CSS gradient (green → amber → red) is trivial — the interesting part is that the thumb's border color and the tooltip's background *aren't* hardcoded, they're computed in JavaScript to match whatever color the CSS gradient is actually showing at that exact percentage. Move the thumb to 50% and both the border and tooltip turn amber; move it to 90% and they turn red — because a matching color-stop table is interpolated in JS on every render, not just declared once in CSS and left alone.

A tiny linear-gradient interpreter, in JavaScript

STOPS mirrors the CSS gradient's color stops as plain { pct, color } objects. colorAt(pct) finds which two stops the current percentage falls between, computes how far between them it is (t), and linearly interpolates each RGB channel independently with lerp. That's a linear gradient, reimplemented from first principles — the same math the browser's own gradient renderer does internally, just exposed so JavaScript can query "what color is showing at 63%?" and get a real answer back.

Track click-to-jump, separate from thumb drag

Clicking the track (but not the thumb itself, guarded by e.target === thumb) jumps the value directly to that position — a separate, simpler interaction from dragging the thumb, letting a user set a rough value with one click and then fine-tune it by dragging if needed.

Build with AI

Build, Understand, Optimize, and Extend It With AI

Paste this snippet's HTML, CSS, and JS into an AI coding assistant like Claude and ask it to explain exactly how colorAt() reimplements a CSS linear-gradient's color interpolation in JavaScript, and why the STOPS array and the CSS gradient declaration both have to be kept in sync by hand rather than one being derived automatically from the other. It's also worth asking the assistant to write a small helper that parses an actual CSS linear-gradient string into a STOPS-like array automatically, removing the need to keep the two declarations manually synchronized.

Prompt to recreate it

Copy this into your AI assistant of choice to build the effect from scratch, or as a jumping-off point for your own variant:

text
Build a single-handle range slider in plain HTML, CSS, and vanilla JavaScript whose track uses a multi-color CSS gradient fill, where the thumb's accent color and an attached tooltip dynamically match the exact gradient color at the thumb's current position — no library.

Requirements:
- A track with a fill bar styled with a CSS linear-gradient spanning at least three colors (e.g. green to amber to red), and a draggable circular thumb positioned according to the current value as a percentage of a configurable min/max range.
- A JavaScript array of color stop objects (each with a percentage position and an RGB color) that mirrors the exact same color stops used in the CSS gradient declaration.
- A function that, given any percentage between 0 and 100, determines which two adjacent color stops that percentage falls between and linearly interpolates each of the red, green, and blue channels independently between those two stops' colors, returning a usable CSS color string — this must correctly reproduce the same color the CSS gradient visually shows at that exact percentage, not an approximation.
- On every value change, apply the interpolated color at the current percentage to both the thumb's border/accent color and the background color of a tooltip element positioned above the thumb, so both visually match the gradient's actual appearance at that exact position, not a fixed or averaged color.
- The tooltip must also display the current numeric value as text.
- Support both dragging the thumb (via pointer events with touch fallback) and clicking directly on the track (excluding clicks on the thumb itself) to jump the value to that position.
- Keyboard support: the thumb must be focusable and respond to Left/Right or Down/Up arrow keys with a small step, and produce a larger step when a modifier key such as Shift is held.
- The thumb must carry correct ARIA slider role and value attributes.

Want to tighten it up first? Run this prompt through the AI Prompt Studio to score it across 8 quality dimensions, catch anti-patterns, and tune the wording for Claude, ChatGPT, or Gemini before you paste it in.

Source Code

<div class="gfs-wrap">
  <div class="gfs-track" id="gfsTrack">
    <div class="gfs-fill" id="gfsFill"></div>
    <div class="gfs-thumb" id="gfsThumb" tabindex="0" role="slider" aria-label="Risk level" aria-valuemin="0" aria-valuemax="100" aria-valuenow="35">
      <div class="gfs-tooltip" id="gfsTooltip">35</div>
    </div>
  </div>
  <div class="gfs-scale"><span>Low risk</span><span>High risk</span></div>
</div>

Step by step

How to Use

  1. 1
    Paste HTML, CSS, and JSA gradient slider appears at 35, with a green-tinted thumb border and tooltip.
  2. 2
    Drag the thumb rightThe border and tooltip shift from green through amber to red as the value climbs — matching the fill exactly.
  3. 3
    Click anywhere on the trackJump straight to that value without needing to grab the thumb first.
  4. 4
    Use the keyboardFocus the thumb and press Left/Right for single steps, or hold Shift for steps of 10.
  5. 5
    Change the gradientEdit the STOPS array's colors (and the CSS gradient to match) to represent any scale, not just risk.

Real-world uses

Common Use Cases

Risk, severity, or intensity settings
A slider whose own color communicates the meaning of the value — exactly the pattern this snippet models.
Confidence or probability inputs
Color-coded feedback reinforces what a raw percentage number alone doesn't communicate as quickly.
Brightness, saturation, or temperature controls
A slider that visually previews its own effect via color, not just a plain neutral bar.
Health, budget, or capacity meters
Green-to-red thresholds on an interactive control instead of a static, read-only gauge.

Got questions?

Frequently Asked Questions

Update both the CSS linear-gradient on .gfs-fill and the STOPS array's color values (as [r, g, b] arrays) to match — they must describe the same gradient for the sampled color to actually align with what's visually showing.

Yes — add more entries to STOPS with their pct and color, and update the CSS gradient with matching percentage stops; colorAt() already loops generically through however many stops exist.

There's no browser API that returns "the color of a linear-gradient at pixel X" — computed styles only expose the gradient's declaration, not a rendered sample at an arbitrary point, so interpolating the same stops manually is the practical way to get a matching color value in JavaScript.

It's already always rendered (not opacity-hidden) in this snippet — if you want it to appear only during interaction, toggle a visibility/opacity class on pointerdown and remove it on pointerup.

Yes — the thumb carries role="slider" with aria-valuemin/max/now, and is independently focusable and operable via Left/Right/Up/Down (plus Shift for larger steps) without a mouse.