How many stars would you give to a rating component? I think it deserves all of them!
Think about it: from e-commerce websites to mood diaries those shiny little helpers make a day a little bit brighter.
Ready to take it on?
Star Rating: requirements and recipe
As a user I want to be able to see a visual representation of a rating, so that I can make a quick judgement just by glancing on it.
Here's an example of how it can look like: ⭐⭐⭐⭐⭐.
Though just emojis are unfortunately not enough to meet all the requirements:
Requirements
- It should be possible to render 1,2,3,4 and 5 stars ratings
- It should be possible to render ratings in between, rounded to a half (e.g. 4.5)
- Both filled and empty stars should have sufficient contrast ratio
- Rating should have a title, label or a screen reader specific text
- Rating markup should include microdata for search engine snippets
Recipe
- Create a star
- Create three versions of it: filled, empty and half-filled (or half-empty, depending on your preference 😁)
- Create a static markup for 2.5
- Make sure that markup matches all requirements, including accessibility ones
- Write a function that would round a rating to the nearest available value and render it
- Sprinkle with your favourite framework or leave it as is
- Give yourself a 5 star rating!
Hints
There are several ways to draw a star with HTML: for example one could use an image, inline SVG or pure CSS.
Hard mode: Star rating input
Now that you have a way to display a rating it would make sense to create an input so that you could get those ratings in when needed.
The hardest part about an input like this is to make it accessible and there's nothing better for it than a built-in HTML element. Unfortunately, there isn't one that would give the visual representation of lined up stars.
With accessibility in mind, think about all the visual feedback you'd like to give users: should you light the stars on hover? Maybe also handle the case when the user is changing one rating to another.
Have fun! And it if's not wild enough, you can join me in implementing a terminal-based star rating input (there's a special sequence of characters you could print to stdout to enable mouse input that I really want to try out 🤩).
Share your creations and feedback and see you tomorrow!
Liked the idea a lot and would love to have it all year long?
Top comments (4)
I have been trying to figure out on how to let my audience to put review on the travel blog post. or maybe tips
What kind of tech stack are you running travel blog on?
Sorry for the delay. It has been few crazy weeks before holidays.
I have been using OctoberCMS. many ideas sprouting from it also other projects. would love to add in review with 1-5 stars on travel blog with different parameters like "Is it Family-friendly", "Will you go again?", "Will you recommend to Friends", "Worth for value",etc
I haven't worked with OctoberCMS, but I'm guessing you'd need to implement both frontend input field and some PHP code to submit that data somewhere.