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?
I used all the techniques in all the answers and they were all 100% Correct, however I have some additional requirements i didn't mention. I need to have different sizes and colors and change them with css. So i found the easiest way was to make a 2 part svg icon.
Here is the result:
http://jsbin.com/vikoju/3/
If i was using web fonts i'd probably go with that solution.
Thanks All
Depending on your target audience,
<use>
on some older browsers is not well supported. The alternative is to draw the<path>
outside<defs>
with a function.stop-opacity
is optional (added there for future readers):-Why not use a web font?
I know you asked about how to modify an SVG image, but have you considered using a web font instead?
It's quite easy to create a font containing full, half-full and empty star glyphs. These can be mapped to any code points you like — in the example below I've used these three characters
●
,◐
and○
. Alternatively you could use "★", "½" and "
". <rant>Maybe someday Unicode will actually include a half-filled star symbol, but not yet. (But if you ever need a pile of poo...)</rant>With a web font you can get the same results as with SVGs, but with the added benefit of having ratings that can be displayed elsewhere (like search engine snippets).
▶▶ JSFiddle link ◀◀
You can make your own web fonts quite easily at places like icomoon.io.
create a linearGradient to fill the star. The 2 stops ensure an instant transition.
then set the fill of the star to this.