Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Fill only Half a star with SVG

Tags:

css

svg

For a rating system I'm building. Would there be any way to add a css class to this svg example such that it would show only half the star filled in?

See jsbin: http://jsbin.com/cifip/2/

In this example the current fill is a yellow color. If i add the class is-half I'd like it to show half grey and have yellow.

Is this possible in SVG?

like image 931
Tim Avatar asked Jul 29 '14 21:07

Tim


People also ask

How do I fill only part of an SVG?

Your best solution is probably going to be to use two <canvas> tags and write the fill portion over the container portion. That way you can calculate both separately and don't have to worry about a partial SVG file. Doing it this way, you'll have much greater control over everything.

How do you fill a half star in CSS?

Just set . star width to 24 and . star. half:after margin-left to -24px, width to 12px, and the half-stars will render correctly.


1 Answers

create a linearGradient to fill the star. The 2 stops ensure an instant transition.

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="256px" height="256px" viewBox="0 0 32 32">
    <defs>
      <linearGradient id="grad">
        <stop offset="50%" stop-color="yellow"/>
        <stop offset="50%" stop-color="grey"/>
      </linearGradient>
    </defs>
    <path fill="url(#grad)" d="M20.388,10.918L32,12.118l-8.735,7.749L25.914,31.4l-9.893-6.088L6.127,31.4l2.695-11.533L0,12.118
  l11.547-1.2L16.026,0.6L20.388,10.918z"/>
  </svg>

then set the fill of the star to this.

like image 148
Robert Longson Avatar answered Sep 28 '22 10:09

Robert Longson