I'm attempting to build a kind of progress bar, similar to what Chris Coyier does here: https://css-tricks.com/css3-progress-bars/, except that I want to apply a SVG clipping path to the surrounding div element.
For example, I want to apply a SVG clipping path to .rating-stars
:
<div class="rating-stars">
<span class="rating"></span>
</div>
You can see what I have so far here: http://codepen.io/rleichty/pen/GrWmRR
It doesn't work in Safari for some reason, and I'm also not sure how to resize the SVG clipping path (if that's even possible).
Does anyone know how I might accomplish this, or perhaps have a better solution?
Figured it out. Here's the final code here: http://codepen.io/rleichty/pen/zNZajM
HTML:
CSS:
You can adjust the height of the stars by adjusting this SCSS variable
$ratingHeight: 100px;
You don't need to use the variable, but it makes it simpler to adjust only one value (you could also flip it around so that the width is prioritized).I found a lot of help here https://sarasoueidan.com/blog/css-svg-clipping/ as she notes:
So I had to adjust the SVG coordinates to fractions.
In order to keep the aspect ratio of a percentage based clipping path, I found help out here How do I keep the aspect ratio of a percentage based clipping path? and simply used the
padding-bottom
trick.Now all you need to do is use jQuery to dynamically set the width of the
.rating
element to represent a star rating. In my case, I'll be pulling data from Goodreads.Personally I like this method as opposed to having separate star images for every single rating. It's simpler and more lightweight, as well as being vector based and responsive.