|$ curl https://forge-ai.dev/api/markdown?path=docs/components/rating
$cat docs/rating.md
updated Recently·10 min read·published

Rating

CSSHTMLUIInteractiveIntermediate🎯Free Tools

Rating components let users view or provide a score. These examples cover five-star ratings, half-star precision, custom icons (hearts, thumbs, flames), read-only display, interactive hover states, multiple sizes, review breakdowns with bar charts, and the CSS gradient technique that powers proportional fill without JavaScript.

Basic Star Rating

Standard five-star rating display using a CSS custom property--rating with a linear gradient. Filled stars indicate the score, empty stars show the remaining range. Pure CSS with no JavaScript required for read-only display.

basic-star-rating
Live
untitled.html
HTML
1<div class="rating-demo">
2 <div class="rating-group">
3 <span class="stars" style="--rating:5" aria-label="5 out of 5 stars">
4 ★★★★★
5 </span>
6 <span class="rating-value">
7 5.0
8 </span>
9 <span class="rating-text">
10 Excellent
11 </span>
12 </div>
13 <div class="rating-group">
14 <span class="stars" style="--rating:4" aria-label="4 out of 5 stars">
15 ★★★★★
16 </span>
17 <span class="rating-value">
18 4.0
19 </span>
20 <span class="rating-text">
21 Very Good
22 </span>
23 </div>
24 <div class="rating-group">
25 <span class="stars" style="--rating:3" aria-label="3 out of 5 stars">
26 ★★★★★
27 </span>
28 <span class="rating-value">
29 3.0
30 </span>
31 <span class="rating-text">
32 Good
33 </span>
34 </div>
35 <div class="rating-group">
36 <span class="stars" style="--rating:2.5" aria-label="2.5 out of 5 stars">
37 ★★★★★
38 </span>
39 <span class="rating-value">
40 2.5
41 </span>
42 <span class="rating-text">
43 Fair
44 </span>
45 </div>
46 <div class="rating-group">
47 <span class="stars" style="--rating:1" aria-label="1 out of 5 stars">
48 ★★★★★
49 </span>
50 <span class="rating-value">
51 1.0
52 </span>
53 <span class="rating-text">
54 Poor
55 </span>
56 </div>
57</div>
preview
Interactive Rating

Clickable star rating with hover preview. Hovering over a star highlights all stars up to that position. Clicking sets the value and displays a text label. Uses ARIA radio group pattern for accessibility.

interactive-rating
Live
untitled.html
HTML
1<div class="interactive-rating-wrap">
2 <div class="star-rating" id="star-rating" role="radiogroup"
3 aria-label="Rate this product">
4 <button class="star" data-value="1" aria-label="1 star"
5 role="radio" aria-checked="false">
6
7 </button>
8 <button class="star" data-value="2" aria-label="2 stars"
9 role="radio" aria-checked="false">
10
11 </button>
12 <button class="star" data-value="3" aria-label="3 stars"
13 role="radio" aria-checked="false">
14
15 </button>
16 <button class="star" data-value="4" aria-label="4 stars"
17 role="radio" aria-checked="false">
18
19 </button>
20 <button class="star" data-value="5" aria-label="5 stars"
21 role="radio" aria-checked="false">
22
23 </button>
24 </div>
25 <p class="rating-label" id="rating-output">
26 Click a star to rate
27 </p>
28</div>
preview
Half-Star Precision

Half-star ratings for more precise scoring. Each star splits into two clickable halves using CSS clip-path. The left half sets a .5 value, the right half sets the full integer. This gives 10 discrete rating levels.

half-star-rating
Live
untitled.html
HTML
1<div class="half-demo">
2 <div class="half-stars" id="half-rating" aria-label="Rate 3.5 out of 5">
3 <button class="half-star left" data-val="0.5" aria-label="0.5">
4
5 </button>
6 <button class="half-star right" data-val="1" aria-label="1">
7
8 </button>
9 <button class="half-star left" data-val="1.5" aria-label="1.5">
10
11 </button>
12 <button class="half-star right" data-val="2" aria-label="2">
13
14 </button>
15 <button class="half-star left active" data-val="2.5" aria-label="2.5">
16
17 </button>
18 <button class="half-star right active" data-val="3" aria-label="3">
19
20 </button>
21 <button class="half-star left active" data-val="3.5" aria-label="3.5">
22
23 </button>
24 <button class="half-star right" data-val="4" aria-label="4">
25
26 </button>
27 <button class="half-star left" data-val="4.5" aria-label="4.5">
28
29 </button>
30 <button class="half-star right" data-val="5" aria-label="5">
31
32 </button>
33 </div>
34 <p class="half-output">
35 3.5 / 5
36 </p>
37</div>
preview
Custom Icons — Hearts & Thumbs

Replace stars with any icon — hearts for favorites, thumbs for likes, or custom emoji for brand-specific scoring. The gradient clip technique works with any repeated character, making it trivially customizable.

custom-icon-rating
Live
untitled.html
HTML
1<div class="custom-rating-demo">
2 <div class="custom-group">
3 <span class="heart-stars" style="--rating:4" aria-label="4 out of 5">
4 ♥♥♥♥♥
5 </span>
6 <span class="custom-label">
7 Favorite
8 </span>
9 </div>
10 <div class="custom-group">
11 <span class="thumb-stars" style="--rating:3" aria-label="3 out of 5">
12 👍👍👍👍👍
13 </span>
14 <span class="custom-label">
15 Like
16 </span>
17 </div>
18 <div class="custom-group">
19 <span class="flame-stars" style="--rating:5" aria-label="5 out of 5">
20 🔥🔥🔥🔥🔥
21 </span>
22 <span class="custom-label">
23 Hot
24 </span>
25 </div>
26</div>
preview
Sizes

Compact, default, large, and hero rating displays. Small ratings fit inline with product names in tables; large and hero ratings work as display elements on product detail pages and landing pages.

rating-sizes
Live
untitled.html
HTML
1<div class="size-demo">
2 <div class="size-row">
3 <span class="stars sm" style="--rating:4" aria-label="4 stars">
4 ★★★★★
5 </span>
6 <span class="size-label">
7 Small (12px)
8 </span>
9 </div>
10 <div class="size-row">
11 <span class="stars md" style="--rating:4" aria-label="4 stars">
12 ★★★★★
13 </span>
14 <span class="size-label">
15 Default (18px)
16 </span>
17 </div>
18 <div class="size-row">
19 <span class="stars lg" style="--rating:4" aria-label="4 stars">
20 ★★★★★
21 </span>
22 <span class="size-label">
23 Large (28px)
24 </span>
25 </div>
26 <div class="size-row">
27 <span class="stars xl" style="--rating:4" aria-label="4 stars">
28 ★★★★★
29 </span>
30 <span class="size-label">
31 Hero (40px)
32 </span>
33 </div>
34</div>
preview
With Review Breakdown

Pairing the star rating with a review count, average score, and rating distribution bar chart gives users full context. Common in product detail pages, app store listings, and marketplace reviews.

review-breakdown
Live
untitled.html
HTML
1<div class="review-demo">
2 <div class="review-card">
3 <div class="review-header">
4 <span class="review-score">
5 4.7
6 </span>
7 <div class="review-meta">
8 <span class="stars" style="--rating:4.7" aria-label="4.7 stars">
9 ★★★★★
10 </span>
11 <span class="review-count">
12 2,847 reviews
13 </span>
14 </div>
15 </div>
16 <div class="rating-bars">
17 <div class="bar-row">
18 <span class="bar-label">
19 5
20 </span>
21 <div class="bar-track">
22 <div class="bar-fill" style="width:68%">
23 </div>
24 </div>
25 <span class="bar-count">
26 1,936
27 </span>
28 </div>
29 <div class="bar-row">
30 <span class="bar-label">
31 4
32 </span>
33 <div class="bar-track">
34 <div class="bar-fill" style="width:20%">
35 </div>
36 </div>
37 <span class="bar-count">
38 569
39 </span>
40 </div>
41 <div class="bar-row">
42 <span class="bar-label">
43 3
44 </span>
45 <div class="bar-track">
46 <div class="bar-fill" style="width:7%">
47 </div>
48 </div>
49 <span class="bar-count">
50 199
51 </span>
52 </div>
53 <div class="bar-row">
54 <span class="bar-label">
55 2
56 </span>
57 <div class="bar-track">
58 <div class="bar-fill" style="width:3%">
59 </div>
60 </div>
61 <span class="bar-count">
62 85
63 </span>
64 </div>
65 <div class="bar-row">
66 <span class="bar-label">
67 1
68 </span>
69 <div class="bar-track">
70 <div class="bar-fill" style="width:2%">
71 </div>
72 </div>
73 <span class="bar-count">
74 58
75 </span>
76 </div>
77 </div>
78 </div>
79</div>
preview
Read-Only vs Interactive

Side-by-side comparison of read-only (display only, no hover effects) and interactive (clickable, hover preview) ratings. Usepointer-events: none on read-only variants to prevent hover and click interactions.

read-only-vs-interactive
Live
untitled.html
HTML
1<div class="compare-demo">
2 <div class="compare-col">
3 <span class="compare-label">
4 Read-Only
5 </span>
6 <span class="stars readonly" style="--rating:3.5" aria-label="3.5 stars">
7 ★★★★★
8 </span>
9 <span class="compare-value">
10 3.5 — Good
11 </span>
12 </div>
13 <div class="compare-divider">
14 </div>
15 <div class="compare-col">
16 <span class="compare-label">
17 Interactive
18 </span>
19 <span class="stars interactive-stars" aria-label="Click to rate">
20 <span class="i-star on">
21
22 </span>
23 <span class="i-star on">
24
25 </span>
26 <span class="i-star on">
27
28 </span>
29 <span class="i-star hoverable">
30
31 </span>
32 <span class="i-star hoverable">
33
34 </span>
35 </span>
36 <span class="compare-value">
37 Click to rate
38 </span>
39 </div>
40</div>
preview
CSS Gradient Technique

The star rating uses a CSS custom property--rating with a linear gradient andbackground-clip: text. The gradient fills stars proportionally based on the rating value — no JavaScript needed for read-only display.

star-rating.css
CSS
1.stars {
2 --rating: 0;
3 --star-size: 18px;
4 --star-color: #333;
5 --star-fill: #EAB308;
6
7 font-size: var(--star-size);
8 letter-spacing: 2px;
9 line-height: 1;
10 color: var(--star-color);
11 background: linear-gradient(
12 90deg,
13 var(--star-fill) calc(var(--rating) / 5 * 100%),
14 var(--star-color) calc(var(--rating) / 5 * 100%)
15 );
16 -webkit-background-clip: text;
17 -webkit-text-fill-color: transparent;
18 background-clip: text;
19 display: inline-block;
20}

info

The CSS gradient technique is ideal for read-only ratings. For interactive ratings, use JavaScript to toggle classes and setaria-checkedon each star. Always provide a text fallback (e.g., "4.5 out of 5 stars") for screen readers.

best practice

When using custom icons (hearts, thumbs), ensure the empty state is visible against the background. Use #333 on dark themes and #DDD on light backgrounds. The icon character must have a consistent width for the gradient clip to align correctly across all ratings.
Best Practices
  • Always provide aria-label or visible text with the rating for accessibility.
  • Use role="radiogroup" and role="radio" with aria-checked for interactive ratings.
  • Display the numeric score alongside stars — color-blind users may not distinguish filled from empty.
  • For half-star ratings, ensure each half has a distinct click target and hover state.
  • Use pointer-events: none on read-only ratings to prevent hover effects.
  • On mobile, increase touch targets to at least 44×44px per star.
  • Consider showing a review breakdown (bar chart) alongside the average for transparency.