# Star rating with label

> Stars fill up to the one you click, pop briefly and a word below says what the rating means.

- Category: [Radio buttons](https://library.aimran.es/components/radios)
- Built with: HTML + CSS
- Live editor: https://library.aimran.es/blocks/uv-rd-stars
- Library: AIMRAN Library (https://library.aimran.es), free, no sign-up, by Aimran Oukham

## Where to use it

The familiar five-star rating, for reviews and quick feedback: rating a product after delivery, scoring an online course or the “How was your meal?” screen of a restaurant app. The label underneath changes from “Very bad” to “Excellent”, which helps people choose. A CSS star rating with a small script to fill the stars.

Translate the five labels into your own tone of voice; they matter more than they seem. If you show the average rating somewhere else on the page, use the same colour for both.

## Code

### HTML

```html
<div class="uv-rdt"><div class="rw"><button data-v="1"><svg viewBox="0 0 24 24"><path d="M12 2.6l2.9 6 6.6.9-4.8 4.6 1.2 6.5L12 17.5 6.1 20.6l1.2-6.5L2.5 9.5l6.6-.9z"/></svg></button><button data-v="2"><svg viewBox="0 0 24 24"><path d="M12 2.6l2.9 6 6.6.9-4.8 4.6 1.2 6.5L12 17.5 6.1 20.6l1.2-6.5L2.5 9.5l6.6-.9z"/></svg></button><button data-v="3"><svg viewBox="0 0 24 24"><path d="M12 2.6l2.9 6 6.6.9-4.8 4.6 1.2 6.5L12 17.5 6.1 20.6l1.2-6.5L2.5 9.5l6.6-.9z"/></svg></button><button data-v="4"><svg viewBox="0 0 24 24"><path d="M12 2.6l2.9 6 6.6.9-4.8 4.6 1.2 6.5L12 17.5 6.1 20.6l1.2-6.5L2.5 9.5l6.6-.9z"/></svg></button><button data-v="5"><svg viewBox="0 0 24 24"><path d="M12 2.6l2.9 6 6.6.9-4.8 4.6 1.2 6.5L12 17.5 6.1 20.6l1.2-6.5L2.5 9.5l6.6-.9z"/></svg></button></div><b class="lb">Not rated</b></div>
```

### CSS

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

.uv-rdt {
  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;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: .7em;
}

.uv-rdt .rw {
  display: flex;
  gap: .25em;
}

.uv-rdt button {
  padding: .15em;
  border: 0;
  background: none;
  cursor: pointer;
  line-height: 0;
  transition: transform calc(0.35s / var(--k)) cubic-bezier(.34,1.56,.64,1);
}

.uv-rdt svg {
  width: 1.9em;
  height: 1.9em;
}

.uv-rdt path {
  fill: color-mix(in srgb,var(--fg) 12%,transparent);
  stroke: color-mix(in srgb,var(--fg) 18%,transparent);
  stroke-width: 1.2;
  transition: fill calc(0.3s / var(--k)) cubic-bezier(.25,.1,.25,1),stroke calc(0.3s / var(--k)) cubic-bezier(.25,.1,.25,1);
}

.uv-rdt button:hover {
  transform: scale(1.18) rotate(-6deg);
}

.uv-rdt button.on path {
  fill: var(--c1);
  stroke: var(--c1);
}

.uv-rdt button.pop {
  animation: uv-rdt calc(0.45s / var(--k)) cubic-bezier(.34,1.56,.64,1);
}

.uv-rdt .lb {
  font-size: calc(var(--s) * .85);
  font-weight: 600;
  color: var(--c1);
}

@keyframes uv-rdt {
  0% {
    scale: 1;
  }
  40% {
    scale: 1.35;
  }
  100% {
    scale: 1;
  }
}
```

### JavaScript

```js
document.querySelectorAll(".uv-rdt").forEach(el => {
  const bs=[...el.querySelectorAll("button")],lb=el.querySelector(".lb"),ns=["Very poor","Poor","Normal","Good","Excellent"];bs.forEach((b,i)=>b.addEventListener("click",()=>{bs.forEach((x,j)=>x.classList.toggle("on",j<=i));lb.textContent=ns[i];b.classList.remove("pop");void b.offsetWidth;b.classList.add("pop")}))
});
```

### 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-rdt {
  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;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: .7em;
}

.uv-rdt .rw {
  display: flex;
  gap: .25em;
}

.uv-rdt button {
  padding: .15em;
  border: 0;
  background: none;
  cursor: pointer;
  line-height: 0;
  transition: transform calc(0.35s / var(--k)) cubic-bezier(.34,1.56,.64,1);
}

.uv-rdt svg {
  width: 1.9em;
  height: 1.9em;
}

.uv-rdt path {
  fill: color-mix(in srgb,var(--fg) 12%,transparent);
  stroke: color-mix(in srgb,var(--fg) 18%,transparent);
  stroke-width: 1.2;
  transition: fill calc(0.3s / var(--k)) cubic-bezier(.25,.1,.25,1),stroke calc(0.3s / var(--k)) cubic-bezier(.25,.1,.25,1);
}

.uv-rdt button:hover {
  transform: scale(1.18) rotate(-6deg);
}

.uv-rdt button.on path {
  fill: var(--c1);
  stroke: var(--c1);
}

.uv-rdt button.pop {
  animation: uv-rdt calc(0.45s / var(--k)) cubic-bezier(.34,1.56,.64,1);
}

.uv-rdt .lb {
  font-size: calc(var(--s) * .85);
  font-weight: 600;
  color: var(--c1);
}

@keyframes uv-rdt {
  0% {
    scale: 1;
  }
  40% {
    scale: 1.35;
  }
  100% {
    scale: 1;
  }
}
`;

// 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 bs=[...el.querySelectorAll("button")],lb=el.querySelector(".lb"),ns=["Very poor","Poor","Normal","Good","Excellent"];bs.forEach((b,i)=>b.addEventListener("click",()=>{bs.forEach((x,j)=>x.classList.toggle("on",j<=i));lb.textContent=ns[i];b.classList.remove("pop");void b.offsetWidth;b.classList.add("pop")}))
}

export default function RdStars() {
  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-rdt"><div className="rw"><button data-v="1"><svg viewBox="0 0 24 24"><path d="M12 2.6l2.9 6 6.6.9-4.8 4.6 1.2 6.5L12 17.5 6.1 20.6l1.2-6.5L2.5 9.5l6.6-.9z" /></svg></button><button data-v="2"><svg viewBox="0 0 24 24"><path d="M12 2.6l2.9 6 6.6.9-4.8 4.6 1.2 6.5L12 17.5 6.1 20.6l1.2-6.5L2.5 9.5l6.6-.9z" /></svg></button><button data-v="3"><svg viewBox="0 0 24 24"><path d="M12 2.6l2.9 6 6.6.9-4.8 4.6 1.2 6.5L12 17.5 6.1 20.6l1.2-6.5L2.5 9.5l6.6-.9z" /></svg></button><button data-v="4"><svg viewBox="0 0 24 24"><path d="M12 2.6l2.9 6 6.6.9-4.8 4.6 1.2 6.5L12 17.5 6.1 20.6l1.2-6.5L2.5 9.5l6.6-.9z" /></svg></button><button data-v="5"><svg viewBox="0 0 24 24"><path d="M12 2.6l2.9 6 6.6.9-4.8 4.6 1.2 6.5L12 17.5 6.1 20.6l1.2-6.5L2.5 9.5l6.6-.9z" /></svg></button></div><b className="lb">Not rated</b></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 “Star rating with label” block (Radio buttons) from AIMRAN Library to my project.

## What it is
Stars fill up to the one you click, pop briefly and a word below says what the rating means.

## 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-rdt {
  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;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: .7em;
}

.uv-rdt .rw {
  display: flex;
  gap: .25em;
}

.uv-rdt button {
  padding: .15em;
  border: 0;
  background: none;
  cursor: pointer;
  line-height: 0;
  transition: transform calc(0.35s / var(--k)) cubic-bezier(.34,1.56,.64,1);
}

.uv-rdt svg {
  width: 1.9em;
  height: 1.9em;
}

.uv-rdt path {
  fill: color-mix(in srgb,var(--fg) 12%,transparent);
  stroke: color-mix(in srgb,var(--fg) 18%,transparent);
  stroke-width: 1.2;
  transition: fill calc(0.3s / var(--k)) cubic-bezier(.25,.1,.25,1),stroke calc(0.3s / var(--k)) cubic-bezier(.25,.1,.25,1);
}

.uv-rdt button:hover {
  transform: scale(1.18) rotate(-6deg);
}

.uv-rdt button.on path {
  fill: var(--c1);
  stroke: var(--c1);
}

.uv-rdt button.pop {
  animation: uv-rdt calc(0.45s / var(--k)) cubic-bezier(.34,1.56,.64,1);
}

.uv-rdt .lb {
  font-size: calc(var(--s) * .85);
  font-weight: 600;
  color: var(--c1);
}

@keyframes uv-rdt {
  0% {
    scale: 1;
  }
  40% {
    scale: 1.35;
  }
  100% {
    scale: 1;
  }
}
`;

// 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 bs=[...el.querySelectorAll("button")],lb=el.querySelector(".lb"),ns=["Very poor","Poor","Normal","Good","Excellent"];bs.forEach((b,i)=>b.addEventListener("click",()=>{bs.forEach((x,j)=>x.classList.toggle("on",j<=i));lb.textContent=ns[i];b.classList.remove("pop");void b.offsetWidth;b.classList.add("pop")}))
}

export default function RdStars() {
  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-rdt"><div className="rw"><button data-v="1"><svg viewBox="0 0 24 24"><path d="M12 2.6l2.9 6 6.6.9-4.8 4.6 1.2 6.5L12 17.5 6.1 20.6l1.2-6.5L2.5 9.5l6.6-.9z" /></svg></button><button data-v="2"><svg viewBox="0 0 24 24"><path d="M12 2.6l2.9 6 6.6.9-4.8 4.6 1.2 6.5L12 17.5 6.1 20.6l1.2-6.5L2.5 9.5l6.6-.9z" /></svg></button><button data-v="3"><svg viewBox="0 0 24 24"><path d="M12 2.6l2.9 6 6.6.9-4.8 4.6 1.2 6.5L12 17.5 6.1 20.6l1.2-6.5L2.5 9.5l6.6-.9z" /></svg></button><button data-v="4"><svg viewBox="0 0 24 24"><path d="M12 2.6l2.9 6 6.6.9-4.8 4.6 1.2 6.5L12 17.5 6.1 20.6l1.2-6.5L2.5 9.5l6.6-.9z" /></svg></button><button data-v="5"><svg viewBox="0 0 24 24"><path d="M12 2.6l2.9 6 6.6.9-4.8 4.6 1.2 6.5L12 17.5 6.1 20.6l1.2-6.5L2.5 9.5l6.6-.9z" /></svg></button></div><b className="lb">Not rated</b></div>
      </div>
    </>
  );
}
```

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