Presenting my unique solution. To enhance your HTML, you simply need to insert two nested divs styled using the provided stylesheet:
<div class="stars"><div class="percent"></div></div>
You can adjust the star rating in percentage (20% for each complete star; 10% for a half star) by setting the width
attribute of the .percent
element in CSS. The percent value can be easily modified later via JavaScript.
.stars {
position: relative;
float: left;
font-size: 50pt;
height: 1em;
line-height: 1em;
}
.stars:before {
content: "\2606\2606\2606\2606\2606";
float: left;
z-index: 1;
}
.stars .percent {
position: absolute;
left: 0;
float: left;
overflow: hidden;
z-index: -1;
}
.stars .percent:after {
content: "\2605\2605\2605\2605\2605";
color: rgb(255, 200, 0);
}
li {
display: table;
}
<ul>
<li><div class="stars"><div class="percent" style="width: 0%;"></div></div></li>
<li><div class="stars"><div class="percent" style="width: 10%;"></div></div></li>
<li><div class="stars"><div class="percent" style="width: 20%;"></div></div></li>
<li><div class="stars"><div class="percent" style="width: 30%;"></div></div></li>
<li><div class="stars"><div class="percent" style="width: 40%;"></div></div></li>
<li><div class="stars"><div class="percent" style="width: 50%;"></div></div></li>
<li><div class="stars"><div class="percent" style="width: 60%;"></div></div></li>
<li><div class="stars"><div class="percent" style="width: 70%;"></div></div></li>
<li><div class="stars"><div class="percent" style="width: 80%;"></div></div></li>
<li><div class="stars"><div class="percent" style="width: 90%;"></div></div></li>
<li><div class="stars"><div class="percent" style="width: 100%;"></div></div></li>
</ul>
The snippet above demonstrates ratings from 0% to 100% in increments of 10%. Please note that finer adjustments may not distribute evenly within the stars due to space considerations, but 10% intervals function well.