20
loading...
This website collects cookies to deliver better user experience
<input type="radio">
-controls. Go read that for some great accessibility-insights.:hover
.<input type="range">
controls two clip-path
's.<input type="range">
?value
(and valueAsNumber
in JavaScript), great for both visual browsers and screen-readers.<input type="range">
for a rating-control. We'll make one, where you can easily add more stars, use half or even quarter-star rating, customize the star-colors etcetera.<label class="rating-label">
<strong>Rating</strong>
<input
class="rating"
max="5"
oninput="this.style.setProperty('--value', this.value)"
step="0.5"
type="range"
value="1">
</label>
max
is used for ”how many stars”. The step
is 1
by default, but in this case, it's been set to 0.5
, allowing “half stars”. The oninput
can be moved to an eventListener
, if you want. It returns the current value
and sets it as a “CSS Custom Property”: --value
.--star: url('data:image/svg+xml,<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 24 24"><path d="M12 17.25l-6.188 3.75 1.641-7.031-5.438-4.734 7.172-0.609 2.813-6.609 2.813 6.609 7.172 0.609-5.438 4.734 1.641 7.031z"/></svg>');
url()
, so we can use it as a mask
in mutiple places.fill
of the stars and the default background-fill (when a star is not selected) are set as properties too:--fill: gold;
--fillbg: rgba(100, 100, 100, 0.15);
--dir: right;
--stars: 5;
--starsize: 3rem;
--symbol: var(--star);
--value: 1;
--x: calc(100% * (var(--value) / var(--stars)));
--x
variable is essential, as this indicates the “cutting point” in the gradient, we'll use in the “track” of the range-slider:.rating::-webkit-slider-runnable-track {
background: linear-gradient(to var(--dir), var(--fill) 0 var(--x), var(--fillbg) 0 var(--x));
block-size: 100%;
mask: repeat left center/var(--starsize) var(--symbol);
-webkit-mask: repeat left center/var(--starsize) var(--symbol);
}
linear-gradient
is “filling up” the stars with the --fill
-color, while the mask
is used to mask it as stars. --dir
-property in the linear-gradient
?linear-gradient(to inline-end, ...)
--dir
-property:[dir="rtl"] .rating {
--dir: left;
}
rtl
, the gradient will be “to left”.UPDATE: People have requested a non-JS version, although the JS is only 45 bytes. Chrome does not support range-progress
(like Firefox), but a hack using box-shadow
can be used. The example above has been updated to include both types. You can also set it to readonly
, if you want to show an “average review rating” like the last of the examples above.