You can do this using a pseudo element ( .rating:after
) that sits on top of div.rating
. .rating
has linear-gradient
, whose background-size
determines which part of the circle is filled, and in .rating:after
there is radial-gradient
, which creates five circles that act like masks).
I used animation to show how the circle fills as the background-size
increases. You can set the required background-size
using JS (or any internal code that generates a rating element), and then add it using the built-in styles in div.rating
.
Using this approach, even between ratings (or ratings of any value, for example 3.65, 2.25, 1.85, etc.), you can easily cope by simply calculating the required background-size
. I added some examples in the demo.
.rating { position: relative; height: 50px; width: 250px; background: linear-gradient(to right, black, black); background-repeat: no-repeat; background-position: 0px 0px; background-size: 0px 100%; } .rating.auto-anim { animation: fill-circle 5s ease infinite; } .rating:after { position: absolute; content: ''; height: 100%; width: 100%; background: radial-gradient(20px at center, transparent 7.5px, beige 9px); background-position: 0px 0px; background-size: 50px 100%; border: 1px solid; } @keyframes fill-circle { to { background-size: 100% 100%; } }
<div class='rating auto-anim'></div> <div class='rating' style="background-size: 50px 100%;"></div> <div class='rating' style="background-size: 75px 100%;"></div> <div class='rating' style="background-size: 121.25px 100%;"></div> <div class='rating' style="background-size: 228.75px 100%;"></div> <div class='rating' style="background-size: 177.25px 100%;"></div> <div class='rating' style="background-size: 80.25px 100%;"></div>
Harry source share