CSS SVG star rating ⭐️

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
Search for a command to run...

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.
No comments yet. Be the first to comment.
Most of you know me for my consistency, a golden arrow in my blog series. I've written 1000 articles in 1008 days! Almost an article a day, and my honeymoon was the only holiday I ever took. I'm super proud of this achievement; it has been a fantasti...

It's not the first time I'll be talking about community. I think it's an essential aspect of any successful tool. This shows in my previous explorations of Astro, Medusa, and now Vendure as well. All these products thrive in a super open, welcoming, ...

The cool part about Vendure is how easy it is to set up and how abstract each layer is. Basically, we get the following elements: External database Server Worker Admin UI Frontend While this is amazing, it also brings a bit of complexity when it co...

The previous article looked at customizing Vendure on a data and process level. In this article, we'll look at customizing emails, as they are often a big part of a webshop system. We'll be looking at two different layers of customization for customi...

Even though Vendure is a pretty significant project out of the box, in some cases, we might want to go in and modify some elements to work to our specific use case. In this article, I'll take a high-level look at some elements we can customize within...

Today we will be looking at making an SVG based star rating. In our example, we will be using three types of stars. (Empty. Half, and full).
Then we will be showcasing some examples of how to use them to show a specific rating.
The end result, as you can see on this Codepen.
As mentioned we will be using three versions, and we will be using SVG Sprites to accomplish this.
<svg id="stars" style="display: none;" version="1.1">
<symbol id="stars-empty-star" viewBox="0 0 102 18" fill="#F1E8CA">
<path d="M9.5 14.25l-5.584 2.936 1.066-6.218L.465 6.564l6.243-.907L9.5 0l2.792 5.657 6.243.907-4.517 4.404 1.066 6.218" />
</symbol>
<symbol id="stars-full-star" viewBox="0 0 102 18" fill="#D3A81E">
<path d="M9.5 14.25l-5.584 2.936 1.066-6.218L.465 6.564l6.243-.907L9.5 0l2.792 5.657 6.243.907-4.517 4.404 1.066 6.218" />
</symbol>
<symbol id="stars-half-star" viewBox="0 0 102 18" fill="#D3A81E">
<use xlink:href="#stars-empty-star" />
<path d="M9.5 14.25l-5.584 2.936 1.066-6.218L.465 6.564l6.243-.907L9.5 0l2.792" />
</symbol>
</svg>
As you can see we have the following:
That's going to be our source, and we can use this in the following ways.
The main question is, of course, how can we now showcase our stars?
Lets say you want to show a empty star:
<svg aria-hidden="true" focusable="false" class="rating">
<use xlink:href="#stars-empty-star" />
</svg>
Or a full star:
<svg aria-hidden="true" focusable="false" class="rating">
<use xlink:href="#stars-full-star" />
</svg>
Or even the half star:
<svg aria-hidden="true" focusable="false" class="rating">
<use xlink:href="#stars-half-star" />
</svg>
That works, awesome!
But now we want to make a 5-star rating component, and SVG's tend to crawl on top of each other.
So if we have the following code:
<!-- 2.5 Rating -->
<svg aria-hidden="true" focusable="false" class="rating">
<use xlink:href="#stars-full-star" />
<use xlink:href="#stars-full-star" />
<use xlink:href="#stars-half-star" />
<use xlink:href="#stars-empty-star" />
<use xlink:href="#stars-empty-star" />
</svg>
If all sits like this:

Hmm, weird? It only shows one star? Correct!
So let's use CSS to fix that.
use {
&:nth-child(2) {
transform: translate(20px);
}
&:nth-child(3) {
transform: translate(40px);
}
&:nth-child(4) {
transform: translate(60px);
}
&:nth-child(5) {
transform: translate(80px);
}
}
Every x child we give 20px offset position.
So now we get this:

You can find the rest of the combinations on the Codepen!
Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter