An updated and more cross-browser compatible version of the CSS star rater can be found here.

My last CSS tutorial, Creating a Star Rater Using CSS covered the creation of a 5-star rating system using only CSS and a list of links. The result acheived can be seen below:

Rate me!
Here is the original CSS star rater in all of its goodness. However, I have mixed it up a little to show that this system is decently flexible. In this case, I have changed the background images for the star graphics and made them a little larger.

Now, this rater works great in practice, but it is missing one essential thing: state. This star rating system does not have a initial state exept for a zero star rating. This is not good enough. We need to be able to start off with a specific rating, say 4 stars or 3 stars. Better yet would be a rating of 3.5 stars. As you know, when you average a rating from many users, you will most likely not get an exact integer number. Most likely you will come up with a rating such as 3.76 stars. How do you do this with CSS? That is the question I aim to answer.

If you don’t understand how the CSS works for the basic star rater that you see above, be sure to do your homework and head on over to the original article for the CSS star rating before you continue.

Now, I hate over complicating things, so I am going to keep this as straight forward as possible. First I will introduce you to my solution to this issue, I will then explain my approach, then I will present you with the CSS and a brief explanation of how it works.

Step 1: Check it in action

Everybody wants to see the result before they do the work, so lets do that. Here she is, in her full shiny glory:

Here’s a few more:

2.5 stars

0 stars

4 stars

Step 2: The XHTML

<ul class='star-rating'>
<li class='current-rating'>Currently 3.5/5 Stars.</li>
<li><a href='#' title='1 star out of 5' class='one-star'>1</a></li>
<li><a href='#' title='2 stars out of 5' class='two-stars'>2</a></li>
<li><a href='#' title='3 stars out of 5' class='three-stars'>3</a></li>
<li><a href='#' title='4 stars out of 5' class='four-stars'>4</a></li>
<li><a href='#' title='5 stars out of 5' class='five-stars'>5</a></li>
</ul>

Basically, the star rater is setup exactly the same way as previously shown. The only difference you will see is in bold. All that we have done here is add one more list element. This element has a class of “current-rating” and has some descriptive text for those browsers that aren’t too cool with CSS. Also, this text is great for screen readers. Don’t worry about this text showing up because we are going to push it off the screen and show the graphical equivalent of 3.5 stars in its place. Pretty simple huh?

Step 3: The Star Image

Don’t ask me why, but we are going to create the star image just a little differently. We are going to make a 3-state image. State 1 will be for the empty, non-rated star. State 2 will be for the current rating. State 3 will be for the user’s rating. Basically the user will hover the star rater and see a different state for when they are choosing their rating. Nifty.

Why a different state image and not just reset the image when a user hovers, using the already showing state? Good question and I am very glad that you asked it. When I first created this thing, I had that same idea. Without boring you with the headache that I had, let me say that it involved me making the list element that housed the current rating a link, then when that link was hovered, I made the width of the link set to 0. Well, as you may have thought, the resulting outcome in IE and Firefox was really really jacked up. It would flicker like heck and do a crazy song and dance and was really unfriendly. Then, I thought to myself, self what should we do? The troubles came from the current state being a link and the links being unfriendly on top of eachother, so I decided to make it a list element with the hover event on the list element like so: li:hover. If any of you know what comes next is pretty dang frustrating to all CSS developers. You got it. Some evil nerd with a lot of money made a proprietary browser that does NOT support :hover pseudo events on list elements. Basically, to make this solution work in IE, you have to get all crazy up in your code and include a csshover.htc file to make the browser realize how to make hover events work. Only problem. You HAVE to have JavaScript turned on. Screw that.

It came to me in a dream. Have a 3-state star rater. This will fix the problem. Hence we find ourselves with this 3-state star image that is o-so-beautiful:

Star Rater Dimensions

Step 4: The CSS, the Magic

  1. .star-rating li.current-rating{
  2. background: url(star_rating.gif) left bottom;
  3. position: absolute;
  4. height: 30px;
  5. display: block;
  6. text-indent: -9000px;
  7. z-index: 1;
  8. }

This time around, we are building on the previous CSS from the tutorial creating a star rater using CSS so the CSS should be pretty small.

The first CSS code that you will notice is the background tag. This list element will have…you guessed it, the same background as all of the other elements. In this case the background has three states: unrated, rated, and currently rating. Keep in mind that some CSS has changed from the original article to make the new background image work for this three state rater. Back to the rater. This list element will contain the current rating for whatever object/article you are using it for.

The next line of CSS is the position:absolute. Because the containing UL of this rater is positioned relatively, that means that this list will be relatively absolute. So if this list element is put at coordinates left:0px, top:0px, it will be located at those coordinates relative to the parent unordered list, NOT to the main window.

The next CSS rules are pretty simple. height:30px does what you think it does – it forces the height of the li element to be as high as all of the other stars. The display:block rule is in place because in the previous tutorial the style .star-rating li declares the list element to be a left floating list element. Now, we will be able to adjust the width of the current-rating list element to how many stars is the average rating. The text-indent rule just moves the text off of the screen so that the user doesn’t see it anymore (an image replacement method). Last, but definitely not least is the z-index: 1 rule. This will cause the current rating to be at a z level of 1, basically the bottom of the stack. We have set the z-index of the a:hover state for the rating links to be at z-index:2. This causes the links to hover over the current rating image and that is exactly what we want. We want our rating to be visible over the current rating so that we can see what we are rating. Yeah, that makes sense.

Thats pretty much all of the CSS for the new current rating addition. We just have a few things left to cover. I mentioned that the image is a three state image so we had to change some of the CSS in the previous tutorial. Basically, and CSS rules declared that use the background now have to be changed. First, we have to change the CSS delclaration for the main star-rating unordered list:

  1. .star-rating{
  2. background: url(star_rating.gif) top left repeat-x;
  3. }

Here, we just merely changed the background position on the y-axis to be at the top. That way, initially you will see the unrated star. Perfect. On to the next declaration. The next declaration we have to change is the CSS declaration for the hover state for the rating links:

  1. .star-rating li a:hover{
  2. background: url(star_rating.gif) left center;
  3. }

Once again this will be an easy change. Here, we will change the active rating state to the center image. So when the user hovers the rater, they will see the vertically centered image as hover image. Neato.

The very last thing I want to mention is how to change the current rating. I showed it in the examples above, but here is the final word. Check out this code snippet to see how I did it:

  1. <ul class=’star-rating’>
  2. <li class=’current-rating’ style=’width:105px;’>
    Currently 3.5/5 Stars.
    </li>
  3. <li>
    <a href=’#’ title=’1 star out of 5′ class=’one-star’>
    1 star
    </a>
    </li>
  4. <li>
    <a href=’#’ title=’2 stars out of 5′ class=’two-stars’>
    2 stars
    </a>
    </li>
  5. <li>
    <a href=’#’ title=’3 stars out of 5′ class=’three-stars’>
    3 stars
    </a>
    </li>
  6. <li>
    <a href=’#’ title=’4 stars out of 5′ class=’four-stars’>
    4 stars
    </a>
    </li>
  7. <li>

    <a href=’#’ title=’5 stars out of 5′ class=’five-stars’>
    5 stars
    </a>
    </li>

  8. </ul>

The bold line is the only line you will have to change to make the rater show what current rating it has. I decided to include it as an in-tag style because its easier to accomplish with server-side scripting. The other solution that I had was to id every current-rating list element and then in an inline stylesheet I declared all the values there. It’s your call.

The only thing you’ll need here is a little math. Remember, each star is 30 pixels wide. So, I’m sure that you will have a server-side script running to determine the average rating for the current item/article, so lets just pretend that the script figured that the average rating was 3.5 stars. Here’s the solution:

Average Rating: 3.5
Each Star Width: 30px;
Set width to: 3.5 * 30 = 105px

So, in this case you will set the list element with a class of current-rating to a width of 105px. Thats it!

Here are some samples of two different rating systems I set up, each including the CSS and the XHTML code to accomplish it:

Want to know how to implement this system with PHP and a Database? Over at SLIM, there is a great tutorial written on just this article.

Comments

217 Comments

Pages:
  1. youtubesitesi.com said over 3 years ago

    Like that,

    thanks u much

  2. Drew said over 3 years ago

    Hello,

    There seems to be some errors in the HTML and CSS you give in your links at the bottom of the post.
    This one in particular: http://komodomedia.com/blog/samples/star_rating/example1.htm

    The first tag is closed off early. And then the CSS supplied in the textarea is several lines longer than the CSS used in the example on that page.

    I was racking my brain for a while (because I couldn’t get it to work!) and finally decided to look at the CSS used on your demo page (star_rating.css) and when I used that it worked just fine.

    (Also, you might want to look into getting a spam filter or deleting spam comments)

  3. Magic said over 3 years ago

    Hi! I really like it!…….
    But dont’t know how to use it with beta blogger.
    There is any way to use this fantastic rating system with blogger?

  4. ödev sitesi said over 3 years ago

    i like it,
    thanks ..

  5. Greg said over 3 years ago

    Can someone tell me why this rater isn’t appearing correctly in Firefox?

    http://www.hearthegear.com/a/5-SM57_on_Snare_Shure_SM57_API_3124.php

    I’m using a variation of this tutorial:
    http://www.robarov.be/rate2/

  6. Hostimal said over 3 years ago

    Absolutely elegant, without JS pure CSS, great work.

  7. shaon said over 3 years ago

    Thanks, its really nice thing.

  8. cathy said over 3 years ago

    I’d like to try it; but I need a tutorial for a rating system using ASP first – does anyone know where I can find one? Been looking online without much luck.

    Thanks

  9. Jim Morris said over 3 years ago

    Works great thanks.
    I wrote a ruby rails wrapper for this and blogged about it here.

    http://blog.wolfman.com/articles/2007/06/23/developing-a-social-networking-site-part-2-rating-stars

  10. Simon said over 3 years ago

    Absolutely genius mate I’ve dabled a wee bit in csss nothing great though but it definetly has alot more power than people realise. This is a fine example of what you can do. Anywho great post!!

  11. brand bontril said over 3 years ago

    brand bontril .

  12. Mazur said over 3 years ago

    ITS LOVELY TUTORIAL ! :D
    I can use in my cms this rate system ? Because I do the mysql and php rate system and I need this one system. I can? :)

  13. loko said over 3 years ago

    Great tutorial

  14. Greg said over 3 years ago

    Hello

    On my site http://www.wierszyki24.pl/ works like a charm!!! Thank you for your great script!!!

  15. tschela said over 3 years ago

    Very cool article, thanks for it :-)

  16. wirus said over 3 years ago

    i just wanted 2 test it

  17. scott said over 3 years ago

    Good to see this update on the awesome star rating tutorial. Have to test this method now.

  18. Green said over 3 years ago

    Hi Sam! Photos i send on e-mail.
    Green

  19. michelle said over 3 years ago

    Only the best layout at http://www.myspaclayouts.com/

    Myspace Layouts, MySpace Backgrounds, MySpace Graphics, MySpace MySpaceTotal.com provides the HOTTEST Layouts and Graphics, with numerous Ge
    nerators, Codes, and More! You’ll also find SEXY and CUTE Layouts only we have!

    Skinny/Thin/Tiny Myspace Layouts, Layouts For Myspace, Hot Myspace Skinny/Thin/Tiny Myspace Layouts, Layouts For Myspace, Hot Myspace Layouts, Cute Myspace Layouts, Retro Myspace Layouts, Scenic Myspace Layouts, Emo Myspace

    Myspace layouts Myspace Codes MySpace Backgrounds Xanga The Largest selection of Myspace layouts, MySpace Codes, Xanga, Piczo Backgrounds. Myspace Graphics, Myspace Generators.

    myspacelayouts slipknot myspacelayouts skin ind myspacelayouts naruto myspacelayouts emo myspacelayouts myspacelayouts gloria trevi marijuana myspacelayouts myspacelayouts poems naruot myspacelayouts tlc myspacelayouts small plain myspacelayouts skullhead myspacelayouts jesus myspacelayouts naked women myspacelayouts bettyboop

  20. miquelcamps said over 3 years ago

    thx!!!! it’s amazing

    Greetings from Spain

  21. LtPitt said over 3 years ago

    Just…
    Great!
    Can I put it on blogger?
    Can I be able to track the scores on my posts?
    I’d like to understand that my readers like and what not using this star system… Is it possible?

  22. SvT said over 3 years ago

    thanks.. i’ll use it ;)

  23. Jitendra said over 3 years ago

    Hi,

    Its very useful to me, I am learning CSS now a days looking forward for star rating.
    Its too good for me.
    Thanks a lot.

  24. ozel ders said over 3 years ago

    Thank you very much it’s very good.

  25. ed pills said over 3 years ago

    ed pills .

  26. resimler said over 3 years ago

    thanks,great script!

  27. Avsh said over 3 years ago

    I have used this execelent script on my site http://www.zyczenia24.info.pl -its just excelent!

  28. Yannick said over 3 years ago

    Very nice article! You helped me a lot! Thanks!

  29. PohEe.com said over 3 years ago

    Nice article.

  30. Rogie said over 3 years ago

    @S.L.H: You might want to check out the newest article @ http://komodomedia.com/blog/index.php/2007/01/20/css-star-rating-redux/. Also, in that article, it links to a few of the very generous folks that have implemented a database solution w/ Ajax for exactly what you are talking about :)

  31. S.L.H. said over 3 years ago

    How can you make it remember the ratings people give it? Could it automatically calculate the average rating users give it? Stuff like that…

  32. ZB said over 3 years ago

    Thanks Mate. This thing is killer. One thing, I dunno how styles work but this always comes on a new line. I want this to come in the same line with where ever i place. How do I do this?

    Eg:
    Now
    Rate this:
    * * * * *

    I want this like this:
    Rate this: * * * * *

    Thanks again.

  33. Awaybox said over 3 years ago

    Very usefull!

  34. Etender4e said over 3 years ago

    hello, good idea…

  35. Lucian Sabo said over 3 years ago

    I’ve made some enhancements:

    The chosen rating remains visible after click:

    I’ve added a star container div, that contains the grayed stars. Add this is the style declaration:
    .star_container{
    background: url(‘images/star_rating.gif’) left top;
    background-repeat:repeat-x;
    width:150px;
    position:relative;
    height: 30px;
    display: block;
    text-indent: -9000px;
    z-index: 1;
    line-break:none;
    }

    The rating system declaration will look like this:

    1
    2
    3
    4
    5

    Note that I’ve changed the class names in romanian language. There is a hidden field to store the rating for further use.

    And here is the script that stores the rating:

    function RateIt(id,rate)
    {
    document.getElementById(id).style.backgroundPosition=’left bottom’;
    document.getElementById(id).style.width=30*rate+”px”;
    document.getElementById(eval(“‘”+id+’_rate’+”‘”)).value=rate;
    }

    This enhancements makes the star rating system suitable for use in a feedback form.

  36. Confluence: SOCIALNEWS said over 3 years ago

    get_history_voted_by_friends($userID,$from,$limit,$logged_userID=NULL)…

    mixed gethistoryvotedbyfriends($userID,$from,$limit,$loggeduserID=NULL) Funkcja zwraca list? historii, na które g?osowali przyjaciele aktualnego u?ytkownika definiowanego przez userID. Zwrca numer b??du w przypadku pora?ki…….

  37. Justin said over 3 years ago

    Thanks so much you totall rock!!!

  38. ohetothks666 said over 3 years ago

    ohetothks666 http://ohetothks666.nu/

  39. bjhess said over 3 years ago

    Still searching for an IE fix. Anyone?

  40. kaustic said over 3 years ago

    Has anyone noticed display problems in IE 6? The overlayed, filled-in rating stars are not overlaying over top of the blank stars. The overlay is starting about 2.5 stars right of where it should. At least for me. Works beautifully in FF.

    Noticed the same thing. Anyone have suggestions? As said, works perfect in Mozilla and every Mac/Linux browser I’ve tested. IE is the only one giving problems.

  41. B.Jade Hess said over 3 years ago

    Has anyone noticed display problems in IE 6? The overlayed, filled-in rating stars are not overlaying over top of the blank stars. The overlay is starting about 2.5 stars right of where it should. At least for me. Works beautifully in FF.

  42. ewr said over 3 years ago

    TESjdah

  43. Michael Szczepanski said over 3 years ago

    You sir, have the most stunning website I’ve seen yet. Great tutorial too!

  44. Get Tagged NOW said over 3 years ago

    FYI:
    If you want to stick the rating after clicking on a star use the following JS code

    function ri(r) // rateit
    {
    document.getElementById(‘myUL’).style.width = 30*r + ‘px’;
    // submit form or use AJAX to save the rating in DB
    }

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10

  45. Get Tagged NOW said over 3 years ago

    Great article. thnx

  46. Q said over 3 years ago

    It worked with outline:none;

    Say, another question, how can I use png with this code ? In IE 6 the transparancy is not right.. Anyone solved this matter ? My designer prefers PNGs over the GIFs.

    Thanks!

  47. Rogie said over 3 years ago

    @Q: Add this rule to remove the stretching dotted line:

    .star-rating li a{
    ...
    overflow:hidden;
    }

  48. Q said over 3 years ago

    Great and simple work.

    The only think that I find a bit strange is the way the dotted line extends for most left side of the screen( in my test example).

    Do I have to wrap it with a div ?

  49. auto said over 3 years ago

    Appreciate it men!

Sorry, the comment form is closed at this time.