First, a quick preview for the over-caffeinated and impatient (try clicking on a rating):

Classic Rater:

  • Currently 3/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Inline Rater:

This

  • Currently 1.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

and

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5


Small Rater

  • Currently 2.5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5



Quite awhile back, I wrote and article entitled Creating a Star Rater using CSS. In fact, if the CSS for this article is a little confusing, you may want to visit my previous articles to get yourself acquainted. I wrote it to my best ability and I shared what I could with the growing CSS-loving community. It was pretty flippin’ cool if you ask me, but it needed more. So, out of that need was born CSS Star Rating Part Deux, a vast improvement to the first, but by no means perfection. Out of the many comments I received about the most recent version, there were still some lacking areas.

By now, people’s grasp as a whole has grown quite a bit I feel and I think that this time does not warrant a full-fledged tutorial (also, my lack of time doesn’t help either). However, there are some significant issues with my most current Star Rating tutorial and I have always wanted to take another go at it.

In my thoughts, here is what the CSS Star Rating, part Deux is missing:

  • Cross-browser support: I received multiple emails and comments stating that it was pretty quirky in browsers like IE6 and even Firefox.
  • Ajax & Back end code : This is a need, but I’m not gonna tackle it. Plenty of people have contributed to the creation of the ajax or back end code, including slim, jon, and an especially nice presentation and project over at masuga. Needless to say, these guys do a great job and I don’t need to replicate their work.
  • Tables and other wierdness: I had tons of report that the rater didn’t work in table cells and only God knows how many other situations.
  • Inline Ratings: People also wanted to place their ratings inline. The current rater didn’t allow for that at all. Also, my feeble attempts to coerce it to work were of no avail.
  • Uber mini Ratings: People also wanted little mini raters. Well, guess what. Problems arose with those too :(
  • Pseudo-State: I say pseudo, because using only CSS, I cannot create a rating system that will hold when you click it and when you refresh it. Back end scripts an databases are needed for that. Basically, it still would be nice when you click the 3rd star, for it to stay there for a little bit.
  • Percentage-based widths: This wasn’t really a request, but It seemed if a person wanted to make a different sized rater, there was a lot of repeating of 20px. 40px, 60px….etc. There just was a lot of exact dimension redundancy.

Well, thats the list! No wonder I wasn’t really happy with the thing. I remember when I first created it, it was my baby. Now, I hate the previous version. Whatever. Ok, enough rant from me. Lets get you guys up into the nerdy stuff.

Ok, so for a refresher, here is the XHTML:

Basic Rater XHTML:

<ul class="star-rating">
<li class="current-rating" style="width:60%;">Currently 3/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>

Inline Rater XHTML:

<span class="inline-rating">
<ul class="star-rating small-star">
<li class="current-rating" style="width:30%;">Currently 1.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></span>

Small Rater XHTML:

<ul class="star-rating small-star">
<li class="current-rating" style="width:50%">Currently 2.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>

No big shockers with the code. Basically new to the scene here is the fact that the inline rater is wrapped with a span and the small rater has an additional “small-star” class added. I’m sure you can imagine that you can call “small-star” any class you would like and adapt the CSS to your liking for the right size and image you would like.

Next, in true throw-it-in-your-face tutorial style, here is the CSS:

Basic Rater CSS

.star-rating,
.star-rating a:hover,
.star-rating a:active,
.star-rating a:focus,
.star-rating .current-rating{
background: url(star.gif) left -1000px repeat-x;
}
.star-rating{
position:relative;
width:125px;
height:25px;
overflow:hidden;
list-style:none;
margin:0;
padding:0;
background-position: left top;
}
.star-rating li{
display: inline;
}
.star-rating a,
.star-rating .current-rating{
position:absolute;
top:0;
left:0;
text-indent:-1000em;
height:25px;
line-height:25px;
outline:none;
overflow:hidden;
border: none;
}
.star-rating a:hover,
.star-rating a:active,
.star-rating a:focus{
background-position: left bottom;
}
.star-rating a.one-star{
width:20%;
z-index:6;
}
.star-rating a.two-stars{
width:40%;
z-index:5;
}
.star-rating a.three-stars{
width:60%;
z-index:4;
}
.star-rating a.four-stars{
width:80%;
z-index:3;
}
.star-rating a.five-stars{
width:100%;
z-index:2;
}
.star-rating .current-rating{
z-index:1;
background-position: left center;
}

The real difference between this CSS and my previous Star Rating tutorials has to do with the addition of :active and :focus pseudo states to hold the rating when clicked, percent-based widths, and the elimination of the use of floats to accomplish the CSS. It is important to mention also the use of overflow:hidden to keep the rater from spilling out over its set width and height as well as the use of a set pixel height line-height. I found that Internet Explorer really had issues with the rater CSS when resizing text, so if I set it to an exact pixel height, issues were resolved.

Inline Rater CSS

.inline-rating{
display:-moz-inline-block;
display:-moz-inline-box;
display:inline-block;
vertical-align: middle;
}

I found that if I wrapped the Star Rater unordered list with a span with a class of “inline-rating” and used the CSS rules above, I could achieve an inline rater. My apologies for the non-standard CSS attributes, but achieving an inline-block behavior in CSS is not an easy task. Most importantly to me is that it works and works in quite a few browsers.

Smaller Rater CSS

.small-star{
width:50px;
height:10px;
}
.small-star,
.small-star a:hover,
.small-star a:active,
.small-star a:focus,
.small-star .current-rating{
background-image: url(star_small.gif);
line-height: 10px;
height: 10px;
}

I’ve tried to make adapting the rater much easier for all of you. As you can see in the CSS above, you will not have to change a ton. Just add a class to your unordered list; In this case I used “small-star.” Update the width and height to the desired values. Make sure to change your image and line-heights as well. This makes creating other variations of the rater much more simple. If you need to make a 3 star, 4 star …N star rating system, you are gonna have to get your hands a little more dirty…sorry :(.

I am much more happy with the performance and support of this version and I feel that it will help to resolve the shortcomings that the previous versions had.

Comments

69 Comments

Pages:
  1. CSS Buttons Instead Of Image Files | BrainPropeller said 1690 days ago

    [...] CSS Star Rating Redux [...]

  2. Building a Ruby on Rails application in a week « 41 technologies said 1834 days ago

    [...] This was added late, it turned out really nice with this tutorial, adding nice css/ajax support. Also note that all the averages are calculated when the entity is [...]

  3. Jason said 2232 days ago

    nope, that doesn’t work. anytime i click anywhere else the previous selected star rating losses its value. suggestions?

  4. Jason said 2232 days ago

    i need this to be displayed 5 times in a column. each time i click on a different one, the other one losses its value. do i need 5 separate css classes for this to work? suggestions?

  5. SergioGP said 2233 days ago

    hey,
    I’m using this system and works very well.
    I’d a problem because the stars show absolute when’re working inside div.
    I read a comment 23875 and I found the answer.
    Great system, thanks,
    I’m from colombia – South america.
    God Bless

  6. MoMo said 2233 days ago

    “more dirty” mo “dirtier”

  7. john said 2236 days ago

    Just want to say thank you, it works great. Our site is currently using php and mysql. Integrated seamlessly.

  8. john said 2236 days ago

    disregard the last post. I figured it out. I didn’t realize that you have to change the width percentage in the inline style to get the current rating.

  9. john said 2236 days ago

    how do I get the current rating. I have the current stored in a database and can retrieve it. how do I use that value to display the current rating?

  10. xhixhi said 2237 days ago

    i need help !

    Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ‘WHERE id=’id21” at line 1

    what can i do?
    please help me

    if you can help email me: xhixhi_uk@hotmail.com

    thanks

  11. HelloWorld said 2243 days ago

    Peace people

    We love you

  12. mashstix.com said 2244 days ago

    I agree with Kat above

    I’ve been learning about this but am stuck at the database part. I have godaddy and the mySQL is confusing as hell.

    Got a new database ready and even when i do a create table code it errors like no other.

    Ex:
    CREATE TABLE `vote` (
    `voteNr` int(8) NOT NULL default ‘0’,
    `voteValue` int(8) NOT NULL default ‘0’,
    `imgId` varchar(100) NOT NULL default ‘’,
    UNIQUE KEY `imgId` (`imgId`)
    ) TYPE=MyISAM;

    So i don’t understand why its so hard to make a table with some columns. If anybody did this through godaddy, please post instructions (I’d love super easy step by step instructions) Thanks

    Best Wishes,
    Paul
    http://www.Mashstix.com

  13. Kat said 2251 days ago

    Would love to use this on my site. However I’m a newbie to this type of webdesign. I notice that the rating is not saved. Above someone stated you needed a database for it. Anybody have instructions for this? As I am new to this. Do I need a fresh new database? what needs to be in there, etc.

    I think some clear instructions on how to get this up for newbies should be included in the zip file.

  14. bkw said 2258 days ago

    … er …

    li id=”liRating” class=”current-rating” …

    then for the LI rows, in the href adding this jscript …

    onclick=”javascript:document.getElementById(‘idStars’).value=’1′;document.getElementById(‘liRating’).style.width=’20%’;return false;”

    ^_^

  15. bkw said 2258 days ago

    Regarding losing state: in ie7 the stars were going back to the “current” value when changing focus. My ugly hack was to set an ID for the header LI and something like this:

    Currently 1/5 Stars.
    1

    … then 40/60/80/100% for the rest of the values.

    Perhaps this will help someone.

  16. denniland.com said 2258 days ago

    Web Development Resources…

    Since I have been in Web Development for like forever, I’d like to put some resources I found useful and interesting for my future references. If this can also help you, that’s cool.
    Clearing Floats Elegantly

    The tutorial will introduce an…

  17. Aleksey said 2261 days ago

    Hey, never mind! I got it ;) Nice feature to put the rating and its text in one line

  18. Aleksey said 2261 days ago

    Hi, what difference between inline rating and classic? Unfortunately I see no difference… :(

Sorry, the comment form is closed at this time.