# Range slider with histogram

> Bars above the slider show how results are spread, and the ones outside your range fade out as you drag.

- Category: [Filters](https://library.aimran.es/components/filters)
- Built with: HTML + CSS
- Live editor: https://library.aimran.es/blocks/uv-f-range-histogram
- Library: AIMRAN Library (https://library.aimran.es), free, no sign-up, by Aimran Oukham

## Where to use it

Showing the spread helps people choose a sensible range. On a used car marketplace, for example, the bars reveal where most cars sit by mileage or price, so buyers don't set a range with nothing in it. Rental sites and flight search use the same idea. It is often called a histogram range slider.

The bars only help if they come from your real data; made-up shapes mislead. Around twenty to thirty bars reads well. On narrow phones, reduce the histogram height a little so the handles stay easy to grab.

## Code

### HTML

```html
<div class="uv-frh"><div class="hd"><span>Distribution</span><b class="out">20 – 80</b></div><div class="bars"><span style="height:20%"></span><span style="height:22%"></span><span style="height:24%"></span><span style="height:27%"></span><span style="height:32%"></span><span style="height:38%"></span><span style="height:44%"></span><span style="height:53%"></span><span style="height:61%"></span><span style="height:70%"></span><span style="height:78%"></span><span style="height:84%"></span><span style="height:89%"></span><span style="height:90%"></span><span style="height:89%"></span><span style="height:84%"></span><span style="height:78%"></span><span style="height:70%"></span><span style="height:61%"></span><span style="height:53%"></span><span style="height:44%"></span><span style="height:38%"></span><span style="height:32%"></span><span style="height:27%"></span><span style="height:24%"></span><span style="height:22%"></span><span style="height:20%"></span><span style="height:19%"></span></div><div class="tk"><span class="fill"></span><i class="h a" data-h="a"></i><i class="h b" data-h="b"></i></div></div>
```

### CSS

```css
.uv-frh {
  --c1: #0071e3;
  --c2: #8ec5ff;
  --s: 16px;
  /* tamaño */
  --k: 1;
  /* velocidad */
  --fg: #1d1d1f;
  /* texto */
  --bg: #f5f5f7;
  /* fondo */
}

.uv-frh {
  font-family: -apple-system,BlinkMacSystemFont,"SF Pro Text","Inter",system-ui,sans-serif;
  font-size: var(--s);
  color: var(--fg);
  -webkit-tap-highlight-color: transparent;
  width: 19em;
  display: flex;
  flex-direction: column;
  gap: .7em;
}

.uv-frh .hd {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: calc(var(--s) * .9);
}

.uv-frh .hd span {
  opacity: .6;
}

.uv-frh .hd b {
  color: var(--c1);
  font-weight: 700;
}

.uv-frh .bars {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  height: 4.5em;
}

.uv-frh .bars span {
  flex: 1;
  border-radius: 3px 3px 0 0;
  background: color-mix(in srgb,var(--fg) 13%,transparent);
  transition: background calc(0.25s / var(--k)) cubic-bezier(.25,.1,.25,1),scale calc(0.3s / var(--k)) cubic-bezier(.34,1.56,.64,1);
  transform-origin: bottom;
}

.uv-frh .bars span.on {
  background: var(--c1);
}

.uv-frh .tk {
  position: relative;
  height: 5px;
  border-radius: 999px;
  background: color-mix(in srgb,var(--fg) 12%,transparent);
}

.uv-frh .fill {
  position: absolute;
  top: 0;
  height: 100%;
  border-radius: 999px;
  background: var(--c1);
}

.uv-frh .h {
  position: absolute;
  top: 50%;
  width: 1.3em;
  height: 1.3em;
  margin: -.65em 0 0 -.65em;
  border-radius: 50%;
  background: var(--bg);
  box-shadow: 0 2px 8px rgba(0,0,0,.22),inset 0 0 0 2px var(--c1);
  cursor: grab;
  touch-action: none;
  transition: transform calc(0.3s / var(--k)) cubic-bezier(.34,1.56,.64,1);
}

.uv-frh .h.drag,.uv-frh .h:hover {
  transform: scale(1.2);
}
```

### JavaScript

```js
document.querySelectorAll(".uv-frh").forEach(el => {
  const tk=el.querySelector(".tk"),f=el.querySelector(".fill"),out=el.querySelector(".out"),A=el.querySelector(".h.a"),Bh=el.querySelector(".h.b"),bs=[...el.querySelectorAll(".bars span")];let v={a:20,b:80};const draw=()=>{const lo=Math.min(v.a,v.b),hi=Math.max(v.a,v.b);f.style.left=lo+"%";f.style.width=(hi-lo)+"%";A.style.left=v.a+"%";Bh.style.left=v.b+"%";out.textContent=Math.round(lo)+" – "+Math.round(hi);bs.forEach((b,i)=>{const p=(i+.5)/bs.length*100;b.classList.toggle("on",p>=lo&&p<=hi)})};[A,Bh].forEach(h=>{h.addEventListener("pointerdown",e=>{h.setPointerCapture(e.pointerId);h.classList.add("drag")});h.addEventListener("pointermove",e=>{if(!h.classList.contains("drag"))return;const r=tk.getBoundingClientRect();v[h.dataset.h]=Math.max(0,Math.min(100,(e.clientX-r.left)/r.width*100));draw()});h.addEventListener("pointerup",()=>h.classList.remove("drag"))});draw()
});
```

### React

```tsx
// @ts-nocheck: setup() es JavaScript del DOM sin tipos, el mismo de la pestaña JS.
import { useEffect, useRef, type CSSProperties } from "react";

const css = `
.uv-frh {
  font-family: -apple-system,BlinkMacSystemFont,"SF Pro Text","Inter",system-ui,sans-serif;
  font-size: var(--s);
  color: var(--fg);
  -webkit-tap-highlight-color: transparent;
  width: 19em;
  display: flex;
  flex-direction: column;
  gap: .7em;
}

.uv-frh .hd {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: calc(var(--s) * .9);
}

.uv-frh .hd span {
  opacity: .6;
}

.uv-frh .hd b {
  color: var(--c1);
  font-weight: 700;
}

.uv-frh .bars {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  height: 4.5em;
}

.uv-frh .bars span {
  flex: 1;
  border-radius: 3px 3px 0 0;
  background: color-mix(in srgb,var(--fg) 13%,transparent);
  transition: background calc(0.25s / var(--k)) cubic-bezier(.25,.1,.25,1),scale calc(0.3s / var(--k)) cubic-bezier(.34,1.56,.64,1);
  transform-origin: bottom;
}

.uv-frh .bars span.on {
  background: var(--c1);
}

.uv-frh .tk {
  position: relative;
  height: 5px;
  border-radius: 999px;
  background: color-mix(in srgb,var(--fg) 12%,transparent);
}

.uv-frh .fill {
  position: absolute;
  top: 0;
  height: 100%;
  border-radius: 999px;
  background: var(--c1);
}

.uv-frh .h {
  position: absolute;
  top: 50%;
  width: 1.3em;
  height: 1.3em;
  margin: -.65em 0 0 -.65em;
  border-radius: 50%;
  background: var(--bg);
  box-shadow: 0 2px 8px rgba(0,0,0,.22),inset 0 0 0 2px var(--c1);
  cursor: grab;
  touch-action: none;
  transition: transform calc(0.3s / var(--k)) cubic-bezier(.34,1.56,.64,1);
}

.uv-frh .h.drag,.uv-frh .h:hover {
  transform: scale(1.2);
}
`;

// Interacción en JavaScript del DOM: la misma que en la pestaña JS.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function setup(el: any) {
  const tk=el.querySelector(".tk"),f=el.querySelector(".fill"),out=el.querySelector(".out"),A=el.querySelector(".h.a"),Bh=el.querySelector(".h.b"),bs=[...el.querySelectorAll(".bars span")];let v={a:20,b:80};const draw=()=>{const lo=Math.min(v.a,v.b),hi=Math.max(v.a,v.b);f.style.left=lo+"%";f.style.width=(hi-lo)+"%";A.style.left=v.a+"%";Bh.style.left=v.b+"%";out.textContent=Math.round(lo)+" – "+Math.round(hi);bs.forEach((b,i)=>{const p=(i+.5)/bs.length*100;b.classList.toggle("on",p>=lo&&p<=hi)})};[A,Bh].forEach(h=>{h.addEventListener("pointerdown",e=>{h.setPointerCapture(e.pointerId);h.classList.add("drag")});h.addEventListener("pointermove",e=>{if(!h.classList.contains("drag"))return;const r=tk.getBoundingClientRect();v[h.dataset.h]=Math.max(0,Math.min(100,(e.clientX-r.left)/r.width*100));draw()});h.addEventListener("pointerup",()=>h.classList.remove("drag"))});draw()
}

export default function FRangeHistogram() {
  const ref = useRef<HTMLDivElement>(null);

  useEffect(() => {
    const el = ref.current?.firstElementChild;
    return el ? setup(el) : undefined;
  }, []);

  return (
    <>
      <style>{css}</style>
      <div
        ref={ref} style={{
          "--c1": "#0071e3",
          "--c2": "#8ec5ff",
          "--s": "16px",
          "--k": "1",
          "--fg": "#1d1d1f",
          "--bg": "#f5f5f7",
        } as CSSProperties}
      >
        <div className="uv-frh"><div className="hd"><span>Distribution</span><b className="out">20 – 80</b></div><div className="bars"><span style={{ height: "20%" }}></span><span style={{ height: "22%" }}></span><span style={{ height: "24%" }}></span><span style={{ height: "27%" }}></span><span style={{ height: "32%" }}></span><span style={{ height: "38%" }}></span><span style={{ height: "44%" }}></span><span style={{ height: "53%" }}></span><span style={{ height: "61%" }}></span><span style={{ height: "70%" }}></span><span style={{ height: "78%" }}></span><span style={{ height: "84%" }}></span><span style={{ height: "89%" }}></span><span style={{ height: "90%" }}></span><span style={{ height: "89%" }}></span><span style={{ height: "84%" }}></span><span style={{ height: "78%" }}></span><span style={{ height: "70%" }}></span><span style={{ height: "61%" }}></span><span style={{ height: "53%" }}></span><span style={{ height: "44%" }}></span><span style={{ height: "38%" }}></span><span style={{ height: "32%" }}></span><span style={{ height: "27%" }}></span><span style={{ height: "24%" }}></span><span style={{ height: "22%" }}></span><span style={{ height: "20%" }}></span><span style={{ height: "19%" }}></span></div><div className="tk"><span className="fill"></span><i className="h a" data-h="a"></i><i className="h b" data-h="b"></i></div></div>
      </div>
    </>
  );
}
```

## Prompt for AI assistants

Paste this into ChatGPT, Claude, Cursor, v0 or Lovable to adapt the block to your stack (Tailwind, Vue, Svelte…):

````text
I want to add the “Range slider with histogram” block (Filters) from AIMRAN Library to my project.

## What it is
Bars above the slider show how results are spread, and the ones outside your range fade out as you drag.

## Chosen settings
- Main color: #0071e3
- Secondary color: #8ec5ff
- Size: 16px (from 12 to 24px)
- Speed: 1x (from 0.5 to 2x)

## What I need
1. Add it to my project with the same look and animation as the reference code: same sizes, colours, radii, shadows, timings and easing.
2. It is built with plain HTML and CSS (the reference wraps it in a React component with its CSS). If my project uses something else (Vue, Svelte, Astro, plain HTML…), translate it without losing any behaviour. If I use Tailwind, turn the styles into classes; if not, keep the CSS in its own file.
3. Expose the settings above as props (or CSS variables) with those values as defaults.
4. Make it work well on phones: nothing that depends only on hover, touch targets of at least 44 px and no overflow on 320 px screens.
5. Make it look right in light and dark mode.
6. Keep it accessible: semantic HTML, roles and aria-* where needed, visible focus and keyboard support.
7. Respect prefers-reduced-motion: with reduced motion, no big animations but the same end result.

## Reference code
```tsx
// @ts-nocheck: setup() es JavaScript del DOM sin tipos, el mismo de la pestaña JS.
import { useEffect, useRef, type CSSProperties } from "react";

const css = `
.uv-frh {
  font-family: -apple-system,BlinkMacSystemFont,"SF Pro Text","Inter",system-ui,sans-serif;
  font-size: var(--s);
  color: var(--fg);
  -webkit-tap-highlight-color: transparent;
  width: 19em;
  display: flex;
  flex-direction: column;
  gap: .7em;
}

.uv-frh .hd {
  display: flex;
  justify-content: space-between;
  align-items: baseline;
  font-size: calc(var(--s) * .9);
}

.uv-frh .hd span {
  opacity: .6;
}

.uv-frh .hd b {
  color: var(--c1);
  font-weight: 700;
}

.uv-frh .bars {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  height: 4.5em;
}

.uv-frh .bars span {
  flex: 1;
  border-radius: 3px 3px 0 0;
  background: color-mix(in srgb,var(--fg) 13%,transparent);
  transition: background calc(0.25s / var(--k)) cubic-bezier(.25,.1,.25,1),scale calc(0.3s / var(--k)) cubic-bezier(.34,1.56,.64,1);
  transform-origin: bottom;
}

.uv-frh .bars span.on {
  background: var(--c1);
}

.uv-frh .tk {
  position: relative;
  height: 5px;
  border-radius: 999px;
  background: color-mix(in srgb,var(--fg) 12%,transparent);
}

.uv-frh .fill {
  position: absolute;
  top: 0;
  height: 100%;
  border-radius: 999px;
  background: var(--c1);
}

.uv-frh .h {
  position: absolute;
  top: 50%;
  width: 1.3em;
  height: 1.3em;
  margin: -.65em 0 0 -.65em;
  border-radius: 50%;
  background: var(--bg);
  box-shadow: 0 2px 8px rgba(0,0,0,.22),inset 0 0 0 2px var(--c1);
  cursor: grab;
  touch-action: none;
  transition: transform calc(0.3s / var(--k)) cubic-bezier(.34,1.56,.64,1);
}

.uv-frh .h.drag,.uv-frh .h:hover {
  transform: scale(1.2);
}
`;

// Interacción en JavaScript del DOM: la misma que en la pestaña JS.
// eslint-disable-next-line @typescript-eslint/no-explicit-any
function setup(el: any) {
  const tk=el.querySelector(".tk"),f=el.querySelector(".fill"),out=el.querySelector(".out"),A=el.querySelector(".h.a"),Bh=el.querySelector(".h.b"),bs=[...el.querySelectorAll(".bars span")];let v={a:20,b:80};const draw=()=>{const lo=Math.min(v.a,v.b),hi=Math.max(v.a,v.b);f.style.left=lo+"%";f.style.width=(hi-lo)+"%";A.style.left=v.a+"%";Bh.style.left=v.b+"%";out.textContent=Math.round(lo)+" – "+Math.round(hi);bs.forEach((b,i)=>{const p=(i+.5)/bs.length*100;b.classList.toggle("on",p>=lo&&p<=hi)})};[A,Bh].forEach(h=>{h.addEventListener("pointerdown",e=>{h.setPointerCapture(e.pointerId);h.classList.add("drag")});h.addEventListener("pointermove",e=>{if(!h.classList.contains("drag"))return;const r=tk.getBoundingClientRect();v[h.dataset.h]=Math.max(0,Math.min(100,(e.clientX-r.left)/r.width*100));draw()});h.addEventListener("pointerup",()=>h.classList.remove("drag"))});draw()
}

export default function FRangeHistogram() {
  const ref = useRef<HTMLDivElement>(null);

  useEffect(() => {
    const el = ref.current?.firstElementChild;
    return el ? setup(el) : undefined;
  }, []);

  return (
    <>
      <style>{css}</style>
      <div
        ref={ref} style={{
          "--c1": "#0071e3",
          "--c2": "#8ec5ff",
          "--s": "16px",
          "--k": "1",
          "--fg": "#1d1d1f",
          "--bg": "#f5f5f7",
        } as CSSProperties}
      >
        <div className="uv-frh"><div className="hd"><span>Distribution</span><b className="out">20 – 80</b></div><div className="bars"><span style={{ height: "20%" }}></span><span style={{ height: "22%" }}></span><span style={{ height: "24%" }}></span><span style={{ height: "27%" }}></span><span style={{ height: "32%" }}></span><span style={{ height: "38%" }}></span><span style={{ height: "44%" }}></span><span style={{ height: "53%" }}></span><span style={{ height: "61%" }}></span><span style={{ height: "70%" }}></span><span style={{ height: "78%" }}></span><span style={{ height: "84%" }}></span><span style={{ height: "89%" }}></span><span style={{ height: "90%" }}></span><span style={{ height: "89%" }}></span><span style={{ height: "84%" }}></span><span style={{ height: "78%" }}></span><span style={{ height: "70%" }}></span><span style={{ height: "61%" }}></span><span style={{ height: "53%" }}></span><span style={{ height: "44%" }}></span><span style={{ height: "38%" }}></span><span style={{ height: "32%" }}></span><span style={{ height: "27%" }}></span><span style={{ height: "24%" }}></span><span style={{ height: "22%" }}></span><span style={{ height: "20%" }}></span><span style={{ height: "19%" }}></span></div><div className="tk"><span className="fill"></span><i className="h a" data-h="a"></i><i className="h b" data-h="b"></i></div></div>
      </div>
    </>
  );
}
```

Give me the complete final code, ready to paste, and tell me in one line where to put it in my project.
````
