Fill only Half a star with SVG

2019-01-24 12:37发布

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?

标签: css svg
4条回答
唯我独甜
2楼-- · 2019-01-24 12:55

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

查看更多
姐就是有狂的资本
3楼-- · 2019-01-24 12:56

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):-

<svg version="1.1" xmlns="http://www.w3.org/2000/svg"
    width="200" height="200">
    <defs>
        <linearGradient id="half_grad">
            <stop offset="50%" stop-color="yellow"/>
            <stop offset="50%" stop-color="grey" stop-opacity="1" />
        </linearGradient>
    </defs>
    <path 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.118l11.547-1.2L16.026,0.6L20.388,10.918z"
        fill="url(#half_grad)" stroke-width="1" stroke="red"/>
</svg>
查看更多
我欲成王,谁敢阻挡
4楼-- · 2019-01-24 13:02

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 "&nbsp;". <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 ◀◀

<html>
<head>
<style>
@font-face {
    font-family:star-rating;
    src:url(data:application/x-font-ttf;base64,AAEAAAALAIAAAwAwT1MvMigiLIYAAAC8AAAAYGNtYX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AAAAACAA4AYwABAAAAAAADABYALAABAAAAAAAEABYAcQABAAAAAAAFABYAFgABAAAAAAAGAAsAQgABAAAAAAAKADQAhwADAAEECQABABYAAAADAAEECQACAA4AYwADAAEECQADABYALAADAAEECQAEABYAcQADAAEECQAFABYAFgADAAEECQAGABYATQADAAEECQAKADQAhwBzAHQAYQByAC0AcgBhAHQAaQBuAGcAVgBlAHIAcwBpAG8AbgAgADEALgAwAHMAdABhAHIALQByAGEAdABpAG4AZ3N0YXItcmF0aW5nAHMAdABhAHIALQByAGEAdABpAG4AZwBSAGUAZwB1AGwAYQByAHMAdABhAHIALQByAGEAdABpAG4AZwBGAG8AbgB0ACAAZwBlAG4AZQByAGEAdABlAGQAIABiAHkAIABJAGMAbwBNAG8AbwBuAC4AAAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==) format('truetype'),url(data:application/font-woff;base64,d09GRk9UVE8AAAUgAAoAAAAABNgAAQAAAAAAAAAAAAAAAAAAAAAAAAAAAABDRkYgAAAA9AAAAWMAAAFjbsVsoU9TLzIAAAJYAAAAYAAAAGAoIiyGY21hcAAAArgAAABUAAAAVCYLJk1nYXNwAAADDAAAAAgAAAAIAAAAEGhlYWQAAAMUAAAANgAAADYBHDApaGhlYQAAA0wAAAAkAAAAJAPiAehobXR4AAADcAAAABwAAAAcBwAAAG1heHAAAAOMAAAABgAAAAYAB1AAbmFtZQAAA5QAAAFpAAABaYWP6p1wb3N0AAAFAAAAACAAAAAgAAMAAAEABAQAAQEBDHN0YXItcmF0aW5nAAECAAEAOvgcAvgbA/gYBB4KABlT/4uLHgoAGVP/i4sMB4tr+JT4dAUdAAAAjg8dAAAAkxEdAAAACR0AAAFaEgAIAQEMFxkbHiMoLXN0YXItcmF0aW5nc3Rhci1yYXRpbmd1MHUxdTIwdTI1Q0J1MjVDRnUyNUQwAAACAYkABQAHAQEEBwoNVn29/JQO/JQO/JQO+5QO+JT3rRX7RaU89zQ8+zT7RXH3FPsQbftE9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AAAAAwAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA==) format('woff');
    font-weight:normal;
    font-style:normal;
}
body {
    font-family:sans-serif;
    font-size:x-large;
}
.rating {
    font-family:star-rating;
}
</style>
</head>
<body>
<ol>
    <li><span class="rating">◐○○</span> = Awful</li>
    <li><span class="rating">●○○</span> = Bad</li>
    <li><span class="rating">●◐○</span> = So-so</li>
    <li><span class="rating">●●○</span> = Good</li>
    <li><span class="rating">●●◐</span> = Great</li>
    <li><span class="rating">●●●</span> = Brilliant</li>
</ol>
</body>
</html>

You can make your own web fonts quite easily at places like icomoon.io.

查看更多
手持菜刀,她持情操
5楼-- · 2019-01-24 13:07

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

<svg>
    <linearGradient id="grad" x1="0" x2="0" y1="0" y2="100%">
        <stop offset="50%" stop-color="grey"/>
        <stop offset="50%" stop-color="white"/>
    </linearGradient>
</svg>

then set the fill of the star to this.

查看更多
登录 后发表回答