Say I have the following png

In my HTML, coming from Ruby I get a particular value for a rating (0,1,2,3,4,5). Depending on that value I want to show a particular row of the png.
I am thinking on, depending on the value, assign a particular class, and then define that class to focus on a particular part of that png image (using some position). However, I fail to see how to do that and if that is the best way to approach this.
So in other words, if I have the following:
<span id='rating'> 3 </span>
I was thinking on doing something like:
<span id='rating' class='rating_3'> <img ..></img></span>
Where rating_3 class would be defined to just show the row where 3 stars are bright.
How can I do that?
Thanks
I would use the HTML5
data-*attribute to set the specif rating value from Ruby like (the rendered html):Then I would use a bit of javascript/jQuery to dynamically set the proper image background position (as previously suggested) of the png sprite file.
First set the css properties to the
.ratingselector to use the sprite image. If you want to use the big stars from your sprite, you could assign anIDto the parent container, e.g.Notice the
width: 84px;andheight: 16px;values, which correspond to the size of a big stars stripe.then suppose you have this html to match our css
The javascript:
Basically you could do the same for the small stars.
BTW, I used the spritecow online tool to get the correct background position of each element of the sprite.
See DEMO HERE