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. rogie said 1570 days ago

    ssin,

    I’m guessing it has to do with having the “#” in the href. By putting the “#” symbol, it is telling the browser to goto that anchor. I’d try removing it and see what it does.

    If you lose the behavior after removing it, try forcing a javascript void function on the href. I would change all of the links on load by doing something like this:

    linkItem.setAttribute(“href”,”javascript:void(0);”);

    This may work for you, depending on the situation.

    Rogie

  2. ssin said 1570 days ago

    Great script! I cant get one thing fixed though. Everytime i click on the star the browser jumps to the top of the page. (just click on an anchor tag).

    In examples people have given on this page, it happens on some and not on other.

    On this page the page jumps after clicking the stars;
    http://www.burningsoulsforum.com/forum/statistics_winamp_member.asp?member=-1&view=1

    But on this page it doesnt;
    http://www.guidetobuy.info/product6-beamers.html

    I cant figure out whats the difference between them! Anyone have any suggestions on how to fix this?

  3. aaron said 1583 days ago

    wow. I can only laugh at how SICK this is..

    you are the f-in bomb.

  4. Andy said 1588 days ago

    Just to say thanks for the css code and graphic as I have managed to get this rating system working using asp (classic) for mobile phones on my site. The way I got it working wasn’t too difficult but I know nothing about javascript / ajax and don’t like relying on javascript so I simply submit to the datbase each time the user clicks on the star and update the page based on the new values.

    Thanks again!

  5. ratingo.com said 1588 days ago

    Speaking about ratings…
    As Internet user you, probably, have already tried to find ratings on some things which you consider to buy, use or get more information on. It may be services (hosting, design or movie rentals), public figures, consumer goods, articles or books, news, movies, beer, hotels, websites and much more.

    You have, perhaps, seen thousands of fragmented websites, discussion forums, which force you to dig for the information even more.

    With Ratingo you got one-stop shop, where you can find what people think (and why) about all you have been searching for before.

    ratingo.com

  6. Smarking said 1588 days ago

    CSS Star Rating Part Deux

  7. JonathanAquino said 1610 days ago

    Note that for small stars (e.g. 14px) you need to set the line-height, otherwise the stars will be strangely offset in IE (line-height:14px)

  8. JonathanAquino said 1610 days ago

    Jang – If you need free PHP hosting, look no further than Ning.com.

  9. The DX said 1617 days ago

    Es un muy buen sistema de Rating para foros y para servicios. solo que la cuestion de las calificaciones medias tiene algunos problemas, para algunos usuarios.-
    Esta muy util.

  10. Jang said 1618 days ago

    Well, since Blogger doesn’t allow hosting up files, is there a way in which the actual files are hosted on some free server and these files are accessed to save and retrieve these ratings?

    Thanks.

  11. marco said 1621 days ago

    hi!
    I like this SO much, but cannot use the suggestion phil gave here
    http://komodomedia.com/blog/index.php/2006/01/09/css-star-rating-part-deux/#comment-3922

    to make the rating permanent.
    anyone got it to work?
    thanks

  12. n3on said 1635 days ago

    I’m wondering if this code could be somehow extended, to have a rating like this:
    0 1 2 3 4 5, where 0 would be thumbs down and the others thumbs up and the thumbs down wouldn’t be selected if I select positive ratings ?
    In other words the rating should be divided in two halves one to the right and the other to the left.

  13. Billy said 1652 days ago

    thx rogie. I was trying to get the first example going which i believe is a 5-horizontal setup but you have just given me the 3-vertical img.

    Anyway, I got everything hokied up I do believe (must be this allergic reaction to chocolate I am having right now lol) but as my domain /stars.html shows right now, it opens with the 5-Horizontal img and after clicking any of them, it changes to 3-Vertical lol (but it still doesn’t appear in my WP blog at all).

    I think it started when I didn’t have the img library link up front but I’m gonna walk away and regroup. Congrats on the mag writeup.

  14. Rogie said 1652 days ago

    Billy, you’ll need to also upload the image to your server. Here is a link to it. Put it in the same directory as your CSS file.

    http://komodomedia.com/blog/samples/star_rating/star_rating.gif

  15. billy said 1652 days ago

    No joy on a brand new static page, what the hell am I doing wrong?

    I did notice that no pics were being referenced (# instead) so I added some links that I am using in another test site (my domain/fadeout.html) but still no stars here: my domain/stars.html

    Any ideas? TIA

  16. Billy said 1652 days ago

    I’m sorry bud, it must be that my CSS spacing is conflicting with the star stylesheet. I still validate fine for both XHTML & CSS but the stars are not viewable in FF or IE, except for the gap it leaves over my Comment box when clicking on any of my post titles here: http://billy-girlardo.com/WP/

    I’ll try a brand new static page.

  17. Billy said 1652 days ago

    thx Rogie, guess I’ll give it another download although I’m using FF… hopefully it will work for me this time

  18. Rogie said 1652 days ago

    As per billy and neville’s latest noticing, I have changed the example file to include fixes for IE. The big fixes were adding overflow: hidden to the rater as well as adding exact top:0px; and left:0px; declaration to absolutely positioned elements.

  19. Billy said 1653 days ago

    Gotta admit, you the man Neville, thx for all your help!

  20. Billy said 1655 days ago

    I just noticed everything had single quotes, I changed to double since I’m shooting for xHTML but still no joy. Also, that 1-5 last comment shows me that the stars will be vertical when I’m 99% sure I picked the first example (I still have the window open anyway) which is horizontal.

    Anyway, I can see your html for that first example on your site, can you just post the style since they appear to be different? Or have I been staring at this too long? lol

    Sorry to bogart your comments.

  21. Billy said 1655 days ago

    yea, I have that, View Source that link above… here’s the HTML though

    Currently 3.5/5 Stars.

    1
    2
    3
    4
    5

  22. Neville Franks said 1655 days ago

    Me again. Apologies but my html of course got gobbled up. The greater than char for the open li tag should be after style=

  23. Billy said 1655 days ago

    Thx Neville, but that still isn’t working for me as (not) shown here:

    http://billy-girlardo.com/WP/2006/02/20/27k_a_year_in_nyc_really_is_solitary/

    The CSS is unchanged as per your first example. Any ideas? TIA.

  24. Neville Franks said 1655 days ago

    Rogie, really, really nice work. I’m just working on adding this into my Windows app, Surfulater so users can set ratings for Articles and Bookmarks and I know it is going to be cool and hopefully well received.

    There is a wee small problem with the current-rating HTML in all three examples:
    style=’width:105px;’ Currently 3.5/5 Stars.

    should be:
    Currently 3.5/5 Stars.

    ie. The close > in the open li tag is in the wrong place.

  25. Billy said 1655 days ago

    I see now only 2 minutes later after not looking far enuff down my site lol that what is getting displayed is the style info as grabbed from the 1st example:

    style=’width:105px;’ Currently 3.5/5 Stars.

    guess I need to tweak this

  26. Billy said 1655 days ago

    Looks great bud, as usual, the easiest implementations get me stuck:
    I humbly grabbed your first example’s code and added it to my stylesheet and WP single.php file but the stars do not appear. VIEW SOURCE shows the HTML but no joy. I have no other problems on my site and am currently validating fine.

    Any thoughts on what I may have missed? I’m dying to plug this blog!

    TIA.

  27. phil said 1657 days ago

    hey, great work! thanks for your tutorial. something to consider… once the user has hovered over their appropriate star-rating, they should be able to click to (ex) change a form item for submission. But once they move away from the stars ul, their rating reverts back to the standard default setting. there are many ways around this problem. i like two in particular:
    (1) use unobtrusive domscripting to change the styles for that selected star… pros: will stay selected even as user moves about page and clicks elsewhere. cons: causes issues for re-selecting (changing rating with) stars.
    (2) use the :active and :focus attributes in a way similar to stu nichols with all his :active photogalleries…
    ul.star-rating a.three-stars:active, ul.star-rating a.three-stars:focus {
    width: 60px;
    left: 0px;
    z-index: 1;
    background: url(../images/star_rating.gif) left bottom;
    }

    now the user can see their rating remain (until they click elsewhere, which is usually to submit or leave the page).

    cool! thanks again,

    phil

  28. petr krebs said 1659 days ago

    cutter bok:
    just follow the discussion above, you should be successful finding what you’re looking for.
    rogie: this stuff is really great, thanks!

  29. Cutter Bok said 1660 days ago

    This is really cool… but…. the CSS is just for looking cute… how does the actual ratings system happen? I mean… now what? This needs to go into a database or something right? how do you keep track of people? IS there any help out there for this second (and far more complicated) part?

  30. J Mann said 1663 days ago

    Rogie. Thanks for the great info!!!

    Has anyone been able to make the star rater print? I have looked at many of the examples people have used based on this tutorial and I don’t see any that actually print the stars. Anyone had any luck? Thx

  31. Maarten Janssens said 1665 days ago

    Thanks a lot Rogie for this really understandable tutorial! I’m a newbe in cyberspace and just started a blog. Blogger does not support a database (as far as I could investigate, I’m no programmer), but I happily used your starrating-technology as the default ‘back-to-top’-button. And, as an artist myself, I couldn’t resist to alter it’s appearance a little bit :o)

    Really, if I can implement the starrater, anyone can.

  32. Scott said 1666 days ago

    Hello rogie.

    What are the chances of you adding one more “star” to this wonderful code? Perhaps, not even a star..lol, but more of a “delete rating” image, with one of those circles with the line down the middle. My radio station uses this script to rate songs, and, currently, the users can only rate songs, and not remove them upon choosing to.

    Thanks,

    Scott

  33. dayve said 1673 days ago

    rogie,

    Actually, my implementation is quite similar to kisco’s except I am using AJAX (noted earlier):

    http://www.burningsoulsforum.com/forum/statistics_winamp_member.asp?member=-1&view=1

    I am finding as I implement this in other projects with different star dimensions that the layout doesn’t always seems perfect. I also noticed with your examples above with the larger stars that the alignment is a bit off as well. Nothing major, but I really like it when things are perfect :)

    I realized I didn’t need the pre-formatted tags. I think I am just having a weird anomaly, especially with smaller stars.

    Thanks.

    Dayve

  34. Donna said 1673 days ago

    Congrats! You are the FIRST blog deemed worthy enough to bookmark. This is wonderful!

    But for all us non-programmers out there – how would I get this onto my website? I can insert html, but have no clue if I could insert this or even how. I would give you and anyone who helps me a nice big kudos and link. :)

    Donna
    http://www.donnajodesigns.com

  35. Dylan said 1673 days ago

    Beautiful ! Thanks for this elegant solution !!

  36. Code0 Networks said 1673 days ago

    Even more – woohoo!

    Must be in one of those good moods today…

    phpFunctions
    Learn Python in 10 minutes
    CSS Star Rating Part Duex
    AJAX Tutorial with Prototype
    Cheat Sheet Roundup

  37. Rogie said 1673 days ago

    Actually dayve, the pre tags were just for display. I noticed in the samples, they were showing as mandatory tags for the XHTML of the star rater. However, you do not need the PRE tags at all. Just take a look at what kisco just did and you shall see.

  38. Kisco said 1673 days ago

    Thanks a lot for this really nice tutorial !

    Here is my implementation : http://www.charlottetoujours.ch/chansons

  39. Snaps! Gallery » Blog Archive » More Dev Updates said 1673 days ago

    [...] s of a rating system in place (images will be replaced eventually) thanks in large part to Rogie King’s CSS Star Rating. In addition to those main things, [...]

  40. dayve said 1673 days ago

    sorry for the recurring posts, but my post keeps getting filtered. I am referring to the pre-formatted text html tag that encapsulates the unordered lists.

  41. dayve said 1673 days ago

    my previous post had an important part filtered… when I spoke of tags, I was referring to the tags.

  42. dayve said 1673 days ago

    the version that dexus has shown is pretty interestng, though it is missing the class for current rating.

    on a side note, is there a trick to using rogie’s version without the need for the tags? when I was playing around in different environments, I noticed that things did not align properly without using those tags and when you do use it, it adds additional padding around the stars.

  43. Rogie said 1674 days ago

    Nice one dexus. I have to give credit where it is due. This is a nice and very clever solution. I actually came up with a similar solution in my first go at the star rater, but I sacrifice bulkier “classy” CSS to have the list in the right order. Maybe, just maybe there is a way for us to have our cake and eat it too…hmmm

  44. dexus said 1674 days ago

    Nice tutorial.

    The only thing I don’t like is all those classes.
    So I tried to make a version that doesn’t need so much classes and here it is: http://veijden.nl/dexus/bin/star%20menu/

    Only problem is that te sourcecode is ‘upsidedown’ ( starts with 5 and ends with 1 )

  45. Notes pour (beaucoup) plus tard » Des effets CSS said 1677 days ago

    [...] ne image.

  46. Nodge said 1678 days ago

    This is maybee an answer to your question…

    http://sonspring.com/journal/removing-dotted-links

  47. Jake said 1681 days ago

    Cool. One question. When I click one the stars (in mac firefox, maybe others), I see a dotted rectangle that appears around the star that I clicked and extends to the left edge of the screen. Can this be removed?

  48. Rogie said 1682 days ago

    sukesh,
    I’d look into using some of the great Ajax stuff that has been done here and then outfitting it to communicate via Ajax to your .NET applications. Also, you may want to try rewriting the PHP functions in .NET that were written over at SLIM:

    http://slim.climaxdesigns.com/

    Have fun. :)

  49. Sukesh Menon said 1682 days ago

    Hi all,
    Its really kool one. I was looking for this kinda one. But I need a star rating function using Dot net Ajax.
    Please help me out…

  50. links for 2006-01-13 - Brokekid.net said 1683 days ago

    [...] ly deleting files. Really. (tags: Norton AntiVirus Virus Symantec Rootkit Computers) CSS Star Rating System, Part 2 Creation of a 5-star rating system us [...]

Sorry, the comment form is closed at this time.