If you only have to support modern browsers, you can get away with:
You only need to convert the number to a class
, e.g. class='stars-score-50'
.
First a demo of "rendered" markup:
body { font-size: 18px; }_x000D_
_x000D_
.stars-container {_x000D_
position: relative;_x000D_
display: inline-block;_x000D_
color: transparent;_x000D_
}_x000D_
_x000D_
.stars-container:before {_x000D_
position: absolute;_x000D_
top: 0;_x000D_
left: 0;_x000D_
content: '?????';_x000D_
color: lightgray;_x000D_
}_x000D_
_x000D_
.stars-container:after {_x000D_
position: absolute;_x000D_
top: 0;_x000D_
left: 0;_x000D_
content: '?????';_x000D_
color: gold;_x000D_
overflow: hidden;_x000D_
}_x000D_
_x000D_
.stars-0:after { width: 0%; }_x000D_
.stars-10:after { width: 10%; }_x000D_
.stars-20:after { width: 20%; }_x000D_
.stars-30:after { width: 30%; }_x000D_
.stars-40:after { width: 40%; }_x000D_
.stars-50:after { width: 50%; }_x000D_
.stars-60:after { width: 60%; }_x000D_
.stars-70:after { width: 70%; }_x000D_
.stars-80:after { width: 80%; }_x000D_
.stars-90:after { width: 90%; }_x000D_
.stars-100:after { width: 100; }
_x000D_
Within block level elements:_x000D_
_x000D_
<div><span class="stars-container stars-0">?????</span></div>_x000D_
<div><span class="stars-container stars-10">?????</span></div>_x000D_
<div><span class="stars-container stars-20">?????</span></div>_x000D_
<div><span class="stars-container stars-30">?????</span></div>_x000D_
<div><span class="stars-container stars-40">?????</span></div>_x000D_
<div><span class="stars-container stars-50">?????</span></div>_x000D_
<div><span class="stars-container stars-60">?????</span></div>_x000D_
<div><span class="stars-container stars-70">?????</span></div>_x000D_
<div><span class="stars-container stars-80">?????</span></div>_x000D_
<div><span class="stars-container stars-90">?????</span></div>_x000D_
<div><span class="stars-container stars-100">?????</span></div>_x000D_
_x000D_
<p>Or use it in a sentence: <span class="stars-container stars-70">?????</span> (cool, huh?).</p>
_x000D_
Then a demo that uses a wee bit of code:
$(function() {_x000D_
function addScore(score, $domElement) {_x000D_
$("<span class='stars-container'>")_x000D_
.addClass("stars-" + score.toString())_x000D_
.text("?????")_x000D_
.appendTo($domElement);_x000D_
}_x000D_
_x000D_
addScore(70, $("#fixture"));_x000D_
});
_x000D_
body { font-size: 18px; }_x000D_
_x000D_
.stars-container {_x000D_
position: relative;_x000D_
display: inline-block;_x000D_
color: transparent;_x000D_
}_x000D_
_x000D_
.stars-container:before {_x000D_
position: absolute;_x000D_
top: 0;_x000D_
left: 0;_x000D_
content: '?????';_x000D_
color: lightgray;_x000D_
}_x000D_
_x000D_
.stars-container:after {_x000D_
position: absolute;_x000D_
top: 0;_x000D_
left: 0;_x000D_
content: '?????';_x000D_
color: gold;_x000D_
overflow: hidden;_x000D_
}_x000D_
_x000D_
.stars-0:after { width: 0%; }_x000D_
.stars-10:after { width: 10%; }_x000D_
.stars-20:after { width: 20%; }_x000D_
.stars-30:after { width: 30%; }_x000D_
.stars-40:after { width: 40%; }_x000D_
.stars-50:after { width: 50%; }_x000D_
.stars-60:after { width: 60%; }_x000D_
.stars-70:after { width: 70%; }_x000D_
.stars-80:after { width: 80%; }_x000D_
.stars-90:after { width: 90%; }_x000D_
.stars-100:after { width: 100; }
_x000D_
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>_x000D_
_x000D_
Generated: <div id="fixture"></div>
_x000D_
The biggest downsides of this solution are:
width
on a pseudo-element).To fix this the solution above can be easily tweaked. The :before
and :after
bits need to become actual elements in the DOM (so we need some JS for that).
The latter is left as an excercise for the reader.