Skip to content

Rating Examples

The examples below assume that you have imported the component and set up the theme.

Basic

Let users set a star rating by click, drag, or keyboard. value holds the rating, step sets how far a value snaps and an arrow key moves, and a <label for> names the slider.

--active
Display
Value
5
0.5
State
<div class="rating-basic">
<label for="rating-basic">Rate the product</label>
<sv-rating id="rating-basic" max="5" step="0.5" value="3.5"></sv-rating>
</div>
<style>
.rating-basic {
display: grid;
gap: 0.5em;
justify-items: center;
label {
font-weight: 600;
}
}
</style>

Labels

Announce words like "Good" instead of "3 of 5" to assistive technology. labels maps values to aria-valuetext, and a valuechange listener shows the label of each new value on the page.

Good

<div class="rating-labels">
<sv-rating
aria-label="Rate your experience"
labels="0:Not rated, 1:Poor, 2:Fair, 3:Good, 4:Very good, 5:Excellent"
step="1"
value="3"
></sv-rating>
<p>Good</p>
</div>
<script type="application/x-typescript" module>
const rating = document.querySelector<Rating>(".rating-labels sv-rating")!;
const output = document.querySelector(".rating-labels p")!;
rating.addEventListener("valuechange", () => {
output.textContent = rating.labels[rating.value];
});
</script>
<style>
.rating-labels {
display: grid;
gap: 0.4em;
justify-items: center;
p {
margin: 0;
font-size: 0.9em;
color: var(--sv-gray-3);
}
}
</style>

Custom Shapes

Replace the default star with your own SVG shape, like a heart. A slotted <svg> is cloned for every shape, and --sv-rating-fill-clr colors the fill.

<div class="rating-shapes">
<sv-rating class="hearts" aria-label="Rate with hearts" value="3.5">
<svg viewBox="0 -960 960 960">
<path
d="M480-120l-58-52q-101-91-167-157T150-447.5Q111-500 95.5-544T80-634q0-94 63-157t157-63q52 0 99 22t81 62q34-40 81-62t99-22q94 0 157 63t63 157q0 46-15.5 90T810-447.5Q771-395 705-329T538-172l-58 52Z"
/>
</svg>
</sv-rating>
<sv-rating class="bolts" aria-label="Rate with bolts" step="1" value="4">
<svg viewBox="140 55 232 400">
<path
d="M343.229 213.746H240.824l29.879-141.408c1.187-5.62-6.537-8.419-9.226-3.344l-108 203.849c-6.107 11.527 2.249 25.41 15.293 25.41h102.405l-29.879 141.408c-1.187 5.62 6.537 8.419 9.226 3.344l108-203.849c6.108-11.527-2.248-25.41-15.293-25.41"
/>
</svg>
</sv-rating>
</div>
<style>
.rating-shapes {
display: grid;
gap: 1.5em;
justify-items: center;
.hearts {
--sv-rating-fill-clr: #ef4444;
}
.bolts {
--sv-rating-fill-clr: #ffd15b;
}
}
</style>

Styling

Resize and recolor the shapes, and space them out. --sv-rating-shape-sz sets the shape size, --sv-rating-fill-clr, --sv-rating-bg, and --sv-rating-bdr-clr set the colors, and ::part(container) sets the gap between shapes.

--sv-rating-shape-sz
color tokens
::part(container) gap
<div class="rating-styling">
<div class="variant">
<sv-rating class="large" aria-label="Larger stars" value="3.5"></sv-rating>
<span>--sv-rating-shape-sz</span>
</div>
<div class="variant">
<sv-rating class="tinted" aria-label="Amber stars" value="4"></sv-rating>
<span>color tokens</span>
</div>
<div class="variant">
<sv-rating class="spread" aria-label="Spread out stars" value="2.5"></sv-rating>
<span>::part(container) gap</span>
</div>
</div>
<style>
.rating-styling {
display: grid;
gap: 1.5em;
justify-items: center;
.variant {
display: grid;
gap: 0.4em;
justify-items: center;
span {
font-size: 0.8em;
color: var(--sv-gray-3);
}
}
.large {
--sv-rating-shape-sz: 2.6em;
}
.tinted {
--sv-rating-fill-clr: #f59e0b;
--sv-rating-bg: color-mix(in srgb, #f59e0b 15%, transparent);
--sv-rating-bdr-clr: color-mix(in srgb, #f59e0b 40%, transparent);
}
.spread::part(container) {
gap: 1em;
}
}
</style>