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 521771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T08:17:05+00:00 2026-05-13T08:17:05+00:00

I have been looking at jquery plugin and was wondering how to adapt that

  • 0

I have been looking at jquery plugin and was wondering how to adapt that plugin to turn a number (like 4.8618164) into a 4.8618164 stars filled out of 5. Basically interpreting a number <5 into stars filled in a 5-star rating system using jQuery/JS/CSS.

Note that this would only display/show the stars rating from an already available number and not accept new ratings submissions.

  • 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-05-13T08:17:05+00:00Added an answer on May 13, 2026 at 8:17 am

    Here’s a solution for you, using only one very tiny and simple image and one automatically generated span element:

    CSS

    span.stars, span.stars span {
        display: block;
        background: url(stars.png) 0 -16px repeat-x;
        width: 80px;
        height: 16px;
    }
    
    span.stars span {
        background-position: 0 0;
    }
    

    Image

    alt text
    (source: ulmanen.fi)

    Note: do NOT hotlink to the above image! Copy the file to your own server and use it from there.

    jQuery

    $.fn.stars = function() {
        return $(this).each(function() {
            // Get the value
            var val = parseFloat($(this).html());
            // Make sure that the value is in 0 - 5 range, multiply to get width
            var size = Math.max(0, (Math.min(5, val))) * 16;
            // Create stars holder
            var $span = $('<span />').width(size);
            // Replace the numerical value with stars
            $(this).html($span);
        });
    }
    

    If you want to restrict the stars to only half or quarter star sizes, add one of these rows before the var size row:

    val = Math.round(val * 4) / 4; /* To round to nearest quarter */
    val = Math.round(val * 2) / 2; /* To round to nearest half */
    

    HTML

    <span class="stars">4.8618164</span>
    <span class="stars">2.6545344</span>
    <span class="stars">0.5355</span>
    <span class="stars">8</span>
    

    Usage

    $(function() {
        $('span.stars').stars();
    });
    

    Output

    Image from fugue icon set (www.pinvoke.com)
    (source: ulmanen.fi)

    Demo

    http://www.ulmanen.fi/stuff/stars.php

    This will probably suit your needs. With this method you don’t have to calculate any three quarter or whatnot star widths, just give it a float and it’ll give you your stars.


    A small explanation on how the stars are presented might be in order.

    The script creates two block level span elements. Both of the spans initally get a size of 80px * 16px and a background image stars.png. The spans are nested, so that the structure of the spans looks like this:

    <span class="stars">
        <span></span>
    </span>
    

    The outer span gets a background-position of 0 -16px. That makes the gray stars in the outer span visible. As the outer span has height of 16px and repeat-x, it will only show 5 gray stars.

    The inner span on the other hand has a background-position of 0 0 which makes only the yellow stars visible.

    This would of course work with two separate imagefiles, star_yellow.png and star_gray.png. But as the stars have a fixed height, we can easily combine them into one image. This utilizes the CSS sprite technique.

    Now, as the spans are nested, they are automatically overlayed over each other. In the default case, when the width of both spans is 80px, the yellow stars completely obscure the grey stars.

    But when we adjust the width of the inner span, the width of the yellow stars decreases, revealing the gray stars.

    Accessibility-wise, it would have been wiser to leave the float number inside the inner span and hide it with text-indent: -9999px, so that people with CSS turned off would at least see the floating point number instead of the stars.

    Hopefully that made some sense.


    Updated 2010/10/22

    Now even more compact and harder to understand! Can also be squeezed down to a one liner:

    $.fn.stars = function() {
        return $(this).each(function() {
            $(this).html($('<span />').width(Math.max(0, (Math.min(5, parseFloat($(this).html())))) * 16));
        });
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this jQuery plugin called Easy Slider which I'm implementing on this site
For awhile now I have been looking for an overall data/markup strategy to settle
I'm wondering if it is at all possible to create a Jquery animation with
I have a webpage that should be relatively lightweight, the problem is that i've
I have some select options in a form, but I'd like to make them
I am working on a emailer system that I have to plug in a
I am using jQuery; I have a select box the options of which are
I am currently looking for a way to search though a MembershipUserCollection. At the
Is it possible to block a page when a user clicks on a link
i am trying to get the value of the raido boxes when you click

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.