How to use CSS to display rating stars

March 31st, 2006 in Basic Tutorials · By David Tapper

Everyone on the ‘net is familiar with rating stars. These are like star like images that indicate the rating of an item. The more stars, the better. Typically we find a scale of 0 to 5 stars, and most often these are in increments of half-stars of even full-stars only.

In this tutorial we’ll discuss how we can use CSS to display the stars in a very elegant manner. It even allows to show any rating very clearly, so you’d be able to distinguish a rating of 3.8 from a rating of 4.2!

Using the 5 star rating system

First of all, have a look at this system in action. Look at the artworks in TheBroth gallery. Each image has a star rating displayed below it. If you look carefully, you should be able to see some that have quarter stars or nine-tenths of a star.

Of course, you might be unlucky and all the ratings fall exactly on whole stars. If so, click the ‘more’ links to see more snapshots, or look at this example:

How it works

The stars are constructed out of only two images. A bright star: , and a faint star: .

The html for these stars is very simple – one DIV inside another:

<div class="rating_bar">
  <div style="width:51%"></div>
</div>

The style attribute for the inner DIV is what determines how many stars are displayed. A rating of 100% corresponds to 5 full stars. A rating of 40% corresponds to 2 stars, a rating of 42% corresponds to a little more than 2 stars, and so on.

The class on the outer DIV allows us to apply CSS. The CSS used is as follows:

.rating_bar {
  width: 55px;
  background: url(/img/star_x_grey.gif) 0 0 repeat-x;
}

.rating_bar div {
  height: 12px;
  background: url(/img/star_x_orange.gif) 0 0 repeat-x;
}

The first rule-set applies to the outer DIV. It defines the width for the entire set of stars. The width determines how many stars will be visible. In our case, we have stars that are 11 pixels wide and we want 5 of them – hence 55 pixels wide. The faint star image is set as the background image.

The second rule-set applies to the inner DIV. The height needs to be the height of your star image. The bright star image is set as the background image.

This all combines to create our example:

You can think of it as two layered boxes. The lower one is filled with the faint stars and takes up the entire width. The upper one contains the bright stars and it’s width is what conveys the bar’s meaning. Here is the same thing again, but with colored borders to reveal the edges of the boxes:

Pretty simple, huh!

Extending the system to create rating bars

You could use this system with many kinds of images. Another good image to use is a colored bar. You can use this: and this: to create this:

An advantage of this method over the stars is that the entire bar can be any width. It doesn’t have to be the exact width of five stars or anything.

An even fancier bar

You can add a rounded end to the bar itself:

To do this, you need an extra image:

…and an extra inner DIV:

<div class="rating_bar">
  <div style="width:52%">
    <div></div>
  </div>
</div>

…and some extra CSS:

.rating_bar div div {
  background: url(/img/rating_bar_tip.gif) right 0 no-repeat;
}

This seems complicated, why would I want to do this?

There are a couple of alternatives to this system. You could display one IMG element for each star and calculate how many bright and faint stars you need. This means that you have more HTML (more for your visitors to download). It also restricts you to displaying full or perhaps half-stars.

Another method is to create a whole lot of different images – one for each possible combination of stars. This would result in less HTML, but more images for your visitors to download. And you are restricted to as many permutations as you can be bothered creating.

But I don’t want two-fifths of a star

You may decide that you only want to display whole stars (or half stars). This is easy to accomplish by first rounding the percentage width of the bar. If you only want to display whole stars, then round the percentage to the nearest 20%. This method still has advantages over the alternatives because it results in less HTML.

Now go rate stuff

Before you run off to create your own bars, take another look at TheBroth gallery and rate some of the incredible artworks. If you’re lucky, you might see a bar change its width in response to your vote.

Share this:
  • del.icio.us
  • StumbleUpon
  • Furl
  • Netscape
  • Reddit
  • Technorati
  • YahooMyWeb

48 Responses to “How to use CSS to display rating stars”

Ryx26 Jul 06

Interesting. I should try Ajax some time.

rhain21 Aug 06

this is an interesting tutorial. i will definatly be back to re-read it a lot.

flipflop30 Sep 06

thank u for sharing that. i found it informative and easy to understand.

Sven20 Dec 06

Nice idea, but it lacks accessibility.
Anyone using a screen-reader has no chance to get the information as the actual rating is ‘hidden’ in the style-attribute of the inner div.

One would at least want to provide an additional textual representation of the percentage somewhere near to the graphical one.

Mike8 May 07

Grat tutorial! One question though: is there any way to get it to display inline? For example, have “Rating: ” without the stars dropping to the next line due to being in a DIV tag?

Mike8 May 07

Great tutorial! One question though: is there any way to get it to display inline? For example, have “Rating: ” without the stars dropping to the next line due to being in a DIV tag?

Nishi10 May 07

Thanks for a crisp, and useful CSS tip.

I tried it, and it works great. However, I am trying to get each star a hotspot (link), but having some problems. This is what I’m trying (It works fine with NS, but not with IE). Do you have any suggestions?

.rating_bar {
width: 55px;
background: url(”star_x_grey.gif”) 0 0 repeat-x;
}

.rating_bar div {
height: 12px;
background: url(”star_x_orange.gif”) 0 0 repeat-x;
}

Smild26 Oct 07

How can i get 10 stars with this, and make rating from imdb, så it work. ??

ecogryff13 Feb 08

Thank you! This is exactly the information I needed so that I could add ratings onto my yearly Book List.

nishant4 Jul 08

m not able to do this pls help

shinee almeida13 Jul 08

Thanks, really wonderful script ..
makes life easier :)

leon24 Sep 08

How can I use this script WITHOUT using CSS?
I want to use it on a page where html is loaded by php.

MorningStar24 Sep 08

Leon, you can make the PHP also produce the CSS declarations for this to work, within the same HTML page.

leon24 Sep 08

I tried but it doesn’t work(or more precisely I don’t know how to make it work)
I use php catalogue module :
http://www.artofplatinum.com/4images
When I add images I have a text box that allows me to add HTML. I want to be able to add your script manually.
I did try adding the code to CSS page but failed to see any results.
I can send you the source code so you can see for your self.

leon25 Sep 08

Got it! I was referencing wrong CSS sheet!
Works excellent!
Thank you for a wonderful script, sorry for the dumb question!

Foya8 Nov 08

How do I set the width as in: , programmatically?

Andrew18 Jan 09

Excellent article. I’m not using this on my website.
Thanks heaps!

Sean9 May 09

You have no idea how long this poor programmer with no knowledge of CSS spent looking for a simple article like this. Works like a charm, very elegant.

How to use CSS to display rating stars - Developer Blog @ TheBroth.com6 Aug 09

[…] Here is the original post: How to use CSS to display rating stars - Developer Blog @ TheBroth.com […]

Sadhan19 Aug 09

Hello, thanks for your nice tutorial about star. I did not think that was so sipmle.

Thanks Again.

sadhan.

ed-hardy17 Mar 10

I have seen pictures of the new colors of ice plant hybrids in yellow, salmon pink, orange and lavender; I hope to order some for next year.ed hardy t-shirts

Justin24 Mar 10

Wonderful tutorial! I don’t know anything about css or programming. But, would like to learn more. Maybe this could be the best start.

sunil p1 Apr 10

Very good article… css programming explained in an interseting way.. Thanks for the info, which I was searching for some time

Ed Hardy on sale3 Apr 10

Ed Hardy Woens Denim Shorts | Ed Hardy Kids Long Sleeve Shirt | ED HARDY Womens Beautiful Shorts Medium | ED Hardy Kids T-Shirt | Womens Ed Hardy Hoodies | ED Hardy Kids Hoodies | Ed Hardy Womens Capri Leggings | Ed Hardy Mens Capri Leggings | Ed Hardy Womens Set |
http://www.vogue2sale.com

Timberland boots sale3 Apr 10

Kids Timberland Boots - http://itstimberland.com/Timberland/Kids-Timberland-Boots/
Women Timberland Boots - http://itstimberland.com/Timberland/Women-Timberland-Boots/
Men Timberland Custom Boots - http://itstimberland.com/Timberland/Men-Timberland-Custom-Boots/

Nike Dunk SB3 Apr 10

Nike Dunk High Coraline
Nike Dunk High Deftones Black Classic Green
Nike Dunk High FC St. Pauli Black Black Medium Grey
Nike Dunk High Notebook White Black University Blue
Nike Dunk High Pharrell Black Metallic Silver Varsity Red
Nike Dunk High Ueno Panda White Black Sail
http://www.thenikedunk.com

Walk straight11 Apr 10

I believe it will be better! This is an interesting tutorial. I will definatly reread it back.
MBT Sport Men Black [url=http://www.fitnessshoes2u.com/mbt-sport-men-black-p-53.html]MBT Sport Men Black[/url]

shoes MBT11 Apr 10

Thank you! This is exactly the information I needed so that I could add ratings onto my yearly Book List.

http://www.thefitnessshoes.com

air max15 Apr 10

I am a shopoholic,especially like buy shoes,i like the air max very much, http://www.saleairmax.com this net Provide all kinds of air max shoes ,i suggest all shopoholic who like nike shoes to go this net.

Cheap shoes online cheap designer sunglasses sale mbt shoes18 Apr 10

Well Done!I do hope u can publish more posts later.buy the way ,if you wanna buy cheap brand shoes online,you may visit our www.cheap-shoesonline.com,I am

sure you will find what u like,mbt shoes and desinger sunglasses are on sale now with free shipping,tax free as well.
come on,have a nice shopping day.

Cheap shoes online cheap designer sunglasses sale mbt shoes20 Apr 10

Well Done!I do hope u can publish more posts later.buy the way ,if you wanna buy mbt shoes online,you may visit our
www.cheap-shoesonline.com/mbt-shoes-c-22447.html/,I am sure you will find what u like,mbt shoes and desinger sunglasses are on sale now with free shipping,tax free as well.come on,have a nice shopping day

Cheap shoes online cheap designer sunglasses sale mbt shoes20 Apr 10

Well Done!I do hope u can publish more posts later.buy the way ,if you wanna buy mbt shoes online,you may visit our
www.cheap-shoesonline.com/mbt-shoes-c-22447.html,I am sure you will find what u like,mbt shoes and desinger sunglasses are on sale now with free

shipping,tax free as well.come on,have a nice shopping day

essays21 Apr 10

this is interesting
but as i already said in my previous post, not all of this is as opinionated as it should be…

Jack22 Apr 10

Very impressive, thank you for posting!

www.cnbrandshop.com25 Apr 10

http://www.cnbrandshop.com

essay topics30 Jul 10

Got it! I was referencing wrong CSS sheet!
Works excellent!
Thank you for a wonderful script, sorry for the dumb question!

custom essays2 Aug 10

great tutorial. nice approach. I’ve seen a same solution for ratings, using bar ratings. It uses php to generate the width. Anyway, your solution is good and helpful for me. Thanks for sharing.

poker chairs4 Aug 10

Very interesting article… css programming explained in very informative way.. Thanks for the info, which I was searching before.

insurance companies6 Aug 10

Well, I think that clears up a few issues for myself. How about everyone else?

john samuels9 Aug 10

I don’t understand what you’re saying in the second paragraph. stamp duty calculator Is it only me that completely missed the purpose? Maybe Im just being cynical, either way it was a solid post. Regards,

gps handby iphone10 Aug 10

http://www.efox-shop.com

I read a lot of posts today and yours is the best one i have read ever.I appreciate your attitute
more info please visit the web for b2c iphone etc.
Ein seltenes Angebot: Im Handy-Shop des Mobilfunkbetreibers E-Plus steht
zurzeit das Dual-SIM-Handy Samsung B5722. Das Mobiltelefon für den parallelen
Ciphone

china handy17 Aug 10

efox-shop the best place to buy dual SIM dual standby phone. The efox-shop service is good, and the full range, such as Lesegerät chinesische handy kaufen china handy kaufen Großhandel Handy Grosshandel Handy Großhandel Handys chinesische handy TV Handy Chinesische Handys welcome to purchase http://www.efox-shop.com chinesische handy kaufen china handy tv handy Chinesische Handys

university dissertation24 Aug 10

Absolutely nice. People will surely enjoy the professional style and manner.

Good resource for students writing university dissertation uk

halloween contacts25 Aug 10

Thanks for sharing this. I’ll try this one into my blog. Hope I can manage to create this into my blog.

replica watches26 Aug 10

where you can buy the best watches at lowest price?
http://www.rolexclassic.com/

We have good quality and their service is the best

Here you will see rolex watches ,omega watches,breitling watches,chanel watches,cartier watches and so on.

angel27 Aug 10

http://www.gogomalls.com
http://www.abuysunglasses.com

air jordan shoes30 Aug 10

Great article! Very interesting and well written. I’ll bookmark it and read it again. Thanks and keep up the great work. air jordan shoes, jordan fusion

louis vuitton purseYesterday

This website is awesome. I constantly come across something new & different right here. Thank you for that data.Damier Canvas

Leave a comment