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

This is my first contribution to the CSS loving community. So, many of you have seen a blog. Many of you have seen comments and reviews on sites such as Netflix and Amazon. Most of these reviews, as in Amazon’s case or Movies, as in Neflix’s case come with a handy dandy rating. I’ve even seen the star rating system used at msn.com. Many of these solutions may use JavaScript or just have an individual hover state for a single star. The question I pose is…. Can you create a star rating using only CSS. I’m talking the kind that when you hover over the 4th star, there are four stars that show up on the hover state. Well, I’m glad you asked because that is just the question I intend to answer.

But! Before we get into the action, I know I like to see a working example first, so here, check this out.

Step 1: XHTML

First, lets get to structure. A star rating simply is a list of links, correct? So, we will create a simple list of links. We will give it a class of “star-rating” and class the links and title them appropriately. Lets do this. Viola:

xhtml:

  1. <ul class=’star-rating’>
  2. <li><a href=’#’ title=’Rate this 1 star out of 5′ class=’one-star’>1</a></li>
  3. <li><a href=’#’ title=’Rate this 2 stars out of 5′ class=’two-stars’>2</a></li>
  4. <li><a href=’#’ title=’Rate this 3 stars out of 5′ class=’three-stars’>3</a></li>
  5. <li><a href=’#’ title=’Rate this 4 stars out of 5′ class=’four-stars’>4</a></li>
  6. <li><a href=’#’ title=’Rate this 5 stars out of 5′ class=’five-stars’>5</a></li>
  7. </ul>

This list that we create would potentially have href’s pointing to the correct page. Also, you could use Ajax to attach a different action to each link, but we will not cover that here. It suffices to say that we will just point them to the correct page for that rating.

Step 2: Graphics

We don’t event know what this thing is going to look like. I’d love to make this a transparent PNG, but because an evil nerd with lots of money has a proprietary browser with its own set of quirky rules, transparent PNG images don’t behave well when using CSS background manipulations, such as when using background-position. So, we will go with the all so ancient GIF. Bam! Here is the GIF image:

Star Rating Image

This image has a hover state, which is the bottom star and a normal state, which is the top star.

Step 3: CSS

Lets get into the meat of the CSS. First, I always style the parent-most element, in this case the UL (Unordered List) with a class of ’star-rating’:

  1. .star-rating{
  2. list-style: none; – turn off the default list image bullets
  3. margin: 3px; – I wan’t some space around this thing
  4. padding: 0px; – I’m anal. I’m pretty sure OL’s have a default padding of 0px, but we’ll set it to 0px just to be safe
  5. width: 100px; – This list is 5 stars, each star is 20px, therefore it should be 5 x 20px = 100px wide
  6. height: 20px; – The height of each star is 20px. Since this is a horizontal list, we will set the list height to the height of the star.
  7. position: relative; – Very important. We will be using absolute positioning later. We want to use relatively-absolute positioning.
  8. background: url(star_rating.gif) top left repeat-x; – By repeating this image horizontally, the list will appear to have five stars.
  9. }

So, in review, to any object with a class of “star-rating”, we did the following:

  • removed the bullets
  • gave it a small margin
  • removed the list indent entirely
  • made the entire list 100 pixels wide and 20 pixels high

Now for the list items, here is the css:

  1. .star-rating li{
  2. padding:0px; – no padding at all
  3. margin:0px; – no margin at all
  4. /*\*/ – Backslash hack, this causes IE5 Mac NOT to see this rule
  5. float: left; – for any other browser, we are going to float left, this makes a horizontal list
  6. /* */ – end the IE5 Backslash hack
  7. }

Basically, this css tells all list items within an object with a class of “star-rating” to be horizontal, rather than vertical. There is a CSS hack called the backslash hack to make the float: left; rule invisible to IE5 Mac.

Now for the anchor items within the list items, here is the css:

  1. .star-rating li a{
  2. display:block; – we want a block item, so that we can mess with its height and width
  3. width:20px; – easy stuff, we want the width to be the same as the star width
  4. height: 20px; – same as the width
  5. text-decoration: none; – remove the underline from the link
  6. text-indent: -9000px; – indent the text off the screen using a image-replacement technique, we dont want to see the text anymore.
  7. z-index: 20; – we’ll come back to this
  8. position: absolute; – we can now control the exact x and y coordinates of each star, relative to the parent UL
  9. padding: 0px; – once again, we don’t need any padding
  10. background-image:none; – we will not show the star
  11. }
  12. .star-rating li a:hover{
  13. background: url(star_rating.gif) left bottom; – this is where the magic is
  14. z-index: 1; – move this star to the bottom of the z-index stack
  15. left: 0px; – move this star all the way to the left, aligned with the side of the UL parent item
  16. }

In review, the CSS for the anchor items is not too complex. Basically we are making the anchor items as big as the star graphic and pushing their inner text off the screen. You will not see the actual anchor items, but rather it is like having a set of invisible hot spots.

As for the :hover state, this is where the CSS magic is. When the users hover, the specific link that they are hovering gets pushed all the way to the bottom of the z-index stack. The link then has a background image load in, but the background is shifted 20 pixels down to the bottom, which now shows us our over state image. The link gets pushed all the way to the left hand side.

Now, I know what you are thinking. You are thinking, wait, if the link shifts all the way to the left, then the :hover state will end and then the star image will dissappear! You are correct, and very smart too. But, wait! We have not finished the CSS yet. Lets see if the next CSS Rules change what is happening at all.

Also, you may think, why repeat the image horizontally if the link is still 20 pixels? Why even put the repeat-x rule there? Another good question. The goal when you hover over the link is to do this:

  • push the link all the way to the left – weve done this
  • make the over state of the image show – weve done this
  • make the link as wide as however many stars it should have – i.e. 3 stars = 3 x 20px per star = 60px – we have NOT done this yet
  • repeat the star graphic horizontally so it looks like we have that many stars – we’ve done this

So, on to the remaining CSS. Since each star has a different width based on how many stars it is, we have to have an individual rule for each star link. This is why we put a class on each different link.

  1. .star-rating a.one-star{
  2. left: 0px;
  3. }
  4. .star-rating a.one-star:hover{
  5. width:20px;
  6. }
  7. .star-rating a.two-stars{
  8. left:20px;
  9. }
  10. .star-rating a.two-stars:hover{
  11. width: 40px;
  12. }
  13. .star-rating a.three-stars{
  14. left: 40px;
  15. }
  16. .star-rating a.three-stars:hover{
  17. width: 60px;
  18. }
  19. .star-rating a.four-stars{
  20. left: 60px;
  21. }
  22. .star-rating a.four-stars:hover{
  23. width: 80px;
  24. }
  25. .star-rating a.five-stars{
  26. left: 80px;
  27. }
  28. .star-rating a.five-stars:hover{
  29. width: 100px;
  30. }

This last block of CSS is fairy easy to explain. The goal here is to make each link as wide as how many stars it is x 20px only when the link is hovered. This way a link with four stars will be 0px from the left and 80px wide. This causes the link to still be located under the mouse, so we do not lose our :hover state. So, each link on :hover needs to be as wide as its number of stars x 20px, i.e. for two stars, 2 x 20px = 40px, for three stars 3 x 20px = 60px and so on and so forth.

You’ll notice that there is also a style for each link in its normal, NON-hovered state. This is because each link is absolutely positioned. If we did not put a left coordinate on them, it would be hard to tell where each link would be horizontally positioned in each browser. This way, we can have fine-tuned control of the horizontal positioning of each link.

And thats it! For a live page that contains a working sample, go here. If you have any questions at all, please feel free to comment.

Comments

93 Comments

Pages:
  1. Star Ratings - CSS3.com said over 1 year ago

    [...] http://www.komodomedia.com/blog/2005/08/…; Posted by Sergio Jasinski Filed in [...]

  2. 网站建设专家 said over 2 years ago

    53 CSS-Techniques You Couldn’t Live Without…

    CSS is important. And it is being used more and more often. Cascading Style Sheets offer many advantages you don’t have in table-layouts – and first of all a strict separation between layout, or design of the page, and the……

  3. bonvga.net said over 3 years ago

    En vrac…

    Bonjour, bonsoir, pour commencer ce billet du week-end, voici quelques liens :

    Histoire de donner le vertige, la mosa?

  4. Smarking said over 4 years ago

    Creating a Star Rater using CSS

  5. Le blog de Bertrand said over 4 years ago

    20 trucs CSS…

    Voici s?

  6. Nick's Blog said over 4 years ago

    Ratings…

    Here is a pretty nifty way of doing star ratings. All css and xhtml friendly. http://komodomedia.com/blog/index.php/2005/08/24/creating-a-star-rater-using-css/……

  7. Railmonds Blog » Blog Archive » 20 CSS Tips and Tricks said over 4 years ago

    [...] Freitag has posted 20 CSS Tips and Tricks: Rounded Corners Rounded Corners without images Creating a Netflix style star ratings Tableless forms Styling L [...]

  8. jimmah ./write » 20 css tips and tricks said over 4 years ago

    [...] Rounded corners without images Creating Netflix style star ratings Tableless forms Styling lis [...]

  9. Bookmark Bonanza said over 4 years ago

    [...] ices 50 Best Firefox Extensions for Power Surfing Review on The Best Life Hacks of 2005 Creating a Star Rater using CSS Nifty Corners: rounded corners [...]

  10. A blog for truck’n down the highway » A List of Web Links said over 4 years ago

    [...] One True Layout CSS – the coolest idea: Creating a star-rater with CSS Best article for [...]

  11. Techniek » Star Rating said over 4 years ago

    [...] geheten Star Rating-manier, onder sommigen ook al bekend van Stylegala. Nadat er eerder al een tutorial was verschenen hoe dit te bewerkstelligen middels [...]

  12. CSS Star Rating Part Deux » Blog » Komodo Media said over 4 years ago

    [...] y, General, CSS, Tutorials — logie @ 1:36 pm My last CSS tutorial, Creating a Star R [...]

  13. Quicklinks said over 4 years ago

    links for 2006-01-10

    Re-Introducing the Real Windows Vista Microsoft playing catch up? :) (tags: Apple humor Microsoft OSX) Creating a Star Rater using CSS (tags: CSS webdesign) Failed Redesigns “A failed redesign is a Web page created from scratch, or substantially upda…

  14. CSS Star Rating Part Deux » Blog » Komodo Media said over 4 years ago

    [...] y, General, CSS, Tutorials — logie @ 1:36 pm My last CSS tutorial, Creating a Star R [...]

  15. BorkWeb » Blog Archive » said over 4 years ago

    [...] s. Definately some useful stuff in there! Rounded Corners Rounded Corners without images Creating a Netflix style star ratings Tableless forms Styling L [...]

  16. CSS Star Rating Part Deux » Blog » Komodo Media said over 4 years ago

    [...] CSS, Tutorials, JavaScript — logie @ 3:44 pm My last CSS tutorial, Creating a Star R [...]

  17. VALSY.COM // Blogger // Open Source Solutions // Web Design // Content Management said over 4 years ago

    [...] – Best resource for CSS/layout: In search of the One True Layout – CSS – the coolest idea: Creating a star-rater with CSS – Best article for CSS and brows [...]

  18. TF » Il meglio dell’anno per i webmaster said over 4 years ago

    [...] ch of the One True Layout La migliore idea realizzata con i CSS: Creating a star-rater with CSS Milgior articolo su CSS e browse [...]

  19. Dan Veres » Blog Archive » Web Development Links said over 4 years ago

    [...] HAH Best resource for CSS/layout: In search of the One True Layout CSS – the coolest idea: Creating a star-rater with CSS Best article for CSS and browser [...]

  20. Peter Dorsi » Top 20 Webmaster Bookmarks of 2005 said over 4 years ago

    [...] HAH Best resource for CSS/layout: In search of the One True Layout CSS – the coolest idea: Creating a star-rater with CSS Best article for CSS and browser [...]

  21. Hakan Demiray » 2005 Y?l?n?n En ›yi Css Makale ve ›puclar? said over 4 years ago

    [...] e Generating Dynamic CSS with PHP A CSS Framework Avoiding classitis Architecting CSS Creating a Star Rater using CSS Introducing th [...]

  22. NonlinearMatters Blog » links for 2005-12-14 said over 4 years ago

    [...] ing CSS How to organize your css (tags: css design webdesign web tutorial articles) Creating a Star Rater using CSS star rating (tags: css webdes [...]

  23. NonlinearMatters Blog » links for 2005-12-14 said over 4 years ago

    [...] ing CSS How to organize your css (tags: css design webdesign web tutorial articles) Creating a Star Rater using CSS star rating (tags: css w [...]

  24. CSS Star Rating: Maintaining State » Blog » Komodo Media said over 4 years ago

    [...] SS, Tutorials, JavaScript — logie @ 11:20 am My last CSS tutorial, Creating a Star R [...]

  25. CSS Star Rating: Maintaining State » Blog » Komodo Media said over 4 years ago

    [...] CSS, Tutorials, JavaScript — logie @ 1:30 am My last CSS tutorial, Creating a Star R [...]

  26. tricklin - my thoughts on web design and more. » Blog Archive » Static Rating System Using One Image & CSS said over 4 years ago

    [...] , or 2 out of 4 stars, using the same image for both examples. So, using Mr. King’s concept, I was able to come up with an easy way to implement a [...]

  27. Cameron Blanton said over 4 years ago

    It was fun visiting here. Wishing you a great day! Revelations of John: http://www.circe-gets-laid.com/archives/002587_mating_in_captivity.html , out little pieces of bread and cups of juice

  28. TzMedia said over 4 years ago

    What about back-end processing… This is basically a five star mouseover…
    What form or server side solution handles the input and averages of user selections of the stars?
    I’ve been looking for a long-time for a solution for a star rating system, that is not .PHP or .NET.
    Where’s a link to a back end solution?

  29. Someone said over 4 years ago

    While clever, this could have been developed quicker using standard script techniques and would have been more easily understood. Makes sense when you have this documentation in hand but someone else coming along to maintain would have to figure it all out.

    CSS is best left to styling with script used for control. This goes beyond simple hover styling and into the realm of control.

  30. JsBook » Creating a Star Rater using CSS » Blog » Komodo Media said over 4 years ago

    [...] Tech. Creating a Star Rater using CSS

  31. Jens said over 4 years ago

    What about already highlighted stars. Lets say the current rating is 3 stars and I want to go and change it. Then the first three list elements should look like:
    1

    Had a go with the CSS but can’t get it working properly. Already highlighted stars don’t get un-highlighted. Also had a go with another second image with blue stars… looks pretty, but not working properly either. Any ideas?

    Cheers,
    Jens

  32. Saivan’s Blog » 20 CSS Tips & Tricks said over 4 years ago

    [...] has whipped up 20 CSS Tips and Tricks: Rounded Corners Rounded Corners without images Creating a Netflix style star ratings Tableless forms Styling [...]

  33. lala said over 4 years ago

    logie, cool! :) be sure to let us know when it’s finished :)

    also, once a user clicks on one of the stars, it would be nice if it stuck to that number. I guess that isn’t possible without javascript, right?

    Thanks again

  34. logie said over 4 years ago

    antony,

    I know not of which you speak. I recreated your scenario with the star-rater in a table filled with content and many rows and columns. I tried it in both firefox and IE6 & IE 5.5. Let me know whats going on, cause on my end, things look sweet.

    Rogie

  35. Antony said over 4 years ago

    When I put this star rater in a table cell it falls apart under MS Explorer (fine in Firefox). Does anyone who understands css know what is happening ?

  36. dankster said over 4 years ago

    /signed

  37. red rocker said over 4 years ago

    Anyone have any back end code they’d like to share to track the status of these? I’d love to see both an XML solution and a database driven one. Anyone, anyone?

  38. Skylog » Archivio » CSS Tips and Tricks - Il weblog di David Scatigna said over 4 years ago

    [...] i” con i css molto interessante. Rounded Corners Rounded Corners without images Creating a Netflix style star ratings Tableles [...]

  39. logie said over 4 years ago

    Good question Lala,

    I’ve thought about that one too. Let me see if I can get something working for you.

  40. lala said over 4 years ago

    how would i display the average rating using this approach? say i have a rating of 3.5 stars. It should display that as the start value on page load. any ideas?

    Thanks

  41. logie said over 4 years ago

    evan, check out comment #30 in this article for some direction on your question.

  42. Evan said over 4 years ago

    This tutorial rocks — but how can I log the ratings into like a database???

  43. LANtastic :: Links said over 4 years ago

    [...] ich auch in anderen F?

Sorry, the comment form is closed at this time.