Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8223903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T14:52:34+00:00 2026-06-07T14:52:34+00:00

Say I have the following png In my HTML, coming from Ruby I get

  • 0

Say I have the following png

Star Map

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T14:52:38+00:00Added an answer on June 7, 2026 at 2:52 pm

    I would use the HTML5 data-* attribute to set the specif rating value from Ruby like (the rendered html):

    <span class='rating' data-rating='3.5'></span>
    

    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 .rating selector to use the sprite image. If you want to use the big stars from your sprite, you could assign an ID to the parent container, e.g.

    #rateBig .rating  {
     background-image: url('images/rating.png'); 
     background-repeat: no-repeat;
     width: 84px; 
     height: 16px;
     display: block; 
    }
    

    Notice the width: 84px; and height: 16px; values, which correspond to the size of a big stars stripe.

    then suppose you have this html to match our css

    <ul id="rateBig">
     <li>This line is rated 3.5 stars<br /><span class='rating' data-rating='3.5'></span></li>
    </ul>
    

    The javascript:

    <script>
    var dataRating, bkPos;
    // function to set the backgroundPosition based on the value of the data-* attribute
    // for the BIG stars
    function getRateImageBig(dataRating){
     switch(dataRating){
      case 1:   bkPos =  "0 -19px";  return bkPos; break;
      case 1.5: bkPos =  "0 -38px";  return bkPos; break;
      case 2:   bkPos =  "0 -57px";  return bkPos; break;
      case 2.5: bkPos =  "0 -76px";  return bkPos; break;
      case 3:   bkPos =  "0 -95px";  return bkPos; break;
      case 3.5: bkPos =  "0 -114px"; return bkPos; break;
      case 4:   bkPos =  "0 -133px"; return bkPos; break;
      case 4.5: bkPos =  "0 -152px"; return bkPos; break;
      case 5:   bkPos =  "0 -171px"; return bkPos; break;
      default:  bkPos =  "0 0"; return bkPos;
     }
    }
    // go through each selector and get the value of the data-rating attribute
    // call the getRateImageBig function to set the proper background position
    $(document).ready(function() {
     $("#rateBig .rating").each(function(i){
      var selector = $(this);
      dataRating = selector.data("rating"); 
      getRateImageBig(dataRating);
      selector.css({"backgroundPosition": bkPos});
     }); // each
    }); //  ready
    </script>
    

    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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Let's say I have a web page ( /index.html ) that contains the following
Let's say I have an image URL http://website.com/content/image.png I want to grab that image
Let's say I have following table like this <!DOCTYPE html PUBLIC -//W3C//DTD XHTML 1.0
Say I have the following: <ul> <li><img src='test.png'> <div id=parentDiv> <div><img='click-me-icon.png'></div> </div> </li> </ul>
Say we have a div named 'slide_down_div' that contains two ul(lists). And I want
Lets say we have following models. class User(db.Model): username=db.StringProperty() avatar=db.ReferenceProperty() class User(db.Model): username=db.StringProperty() avatar=db.StringProperty()
Let's say we have following this: <p class=first>This is paragraph 1.</p> <p class=second>This is
Let's say we have following code: struct A{ virtual ~A(){} void f(){ p =
say I have the following <h3>This is my heading</h3> <p>This is my headingAnd this
Say I have the following chef role: name test description role for test run_list

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.