I have a hidden SVG in my HTML that contains all of the site's icons. It looks something like this (I'm including only the "half gold star" icon for the purposes of this question):
<svg xmlns="http://www.w3.org/2000/svg" style="display: none;">
<symbol width="19.783" height="20" viewBox="0 0 19.783 20" id="icon-half-star" role="img" aria-labelledby="icon-half-star-title">
<title id="icon-half-star-title">Half a star</title>
<defs>
<clipPath id="icon-half-star-clip-a">
<rect width="18.783" height="18" fill="#f0f0f0"/>
</clipPath>
<clipPath id="icon-half-star-clip-b">
<rect width="10" height="20" transform="translate(137 2256)" fill="#fff"/>
</clipPath>
<clipPath id="icon-half-star-clip-c">
<rect width="18.783" height="18" fill="#f4ba1c"/>
</clipPath>
</defs>
<g transform="translate(1 1.001)">
<g clip-path="url(#icon-half-star-clip-a)">
<path d="M18.734,6.8a1,1,0,0,0-.859-.686l-5.422-.492L10.311.605a1,1,0,0,0-1.837,0L6.331,5.622.908,6.115A1,1,0,0,0,.341,7.861l4.1,3.594L3.231,16.778a1,1,0,0,0,1.485,1.079l4.676-2.8,4.675,2.8a1,1,0,0,0,1.486-1.079l-1.208-5.323,4.1-3.594A1,1,0,0,0,18.734,6.8" transform="translate(0 0.001)" fill="#f0f0f0"/>
</g>
</g>
<g transform="translate(-137 -2256)" clip-path="url(#icon-half-star-clip-b)">
<g transform="translate(137.783 2257.001)">
<g clip-path="url(#icon-half-star-clip-c)">
<path d="M18.734,6.8a1,1,0,0,0-.859-.686l-5.422-.492L10.311.605a1,1,0,0,0-1.837,0L6.331,5.622.908,6.115A1,1,0,0,0,.341,7.861l4.1,3.594L3.231,16.778a1,1,0,0,0,1.485,1.079l4.676-2.8,4.675,2.8a1,1,0,0,0,1.486-1.079l-1.208-5.323,4.1-3.594A1,1,0,0,0,18.734,6.8" transform="translate(0 0.001)" fill="#f4ba1c"/>
</g>
</g>
</g>
</symbol>
</svg>
When I reference this symbol later in the document using <svg width="19.783" height="20" viewBox="0 0 19.783 20"><use href="#icon-half-star" /></svg>
, it displays as a full gold star. In other words, the <clipPath>
elements are being ignored. What should I be doing differently?
I put the <clipPath>
elements inside the SVG that has a <use>
element and that fixed the problem, but that's not an ideal solution. I don't want to have to type the <clipPath>
s every time I reference the icon.
position:absolute
instead of display:none. Also I would like to comment that your svg is full of transform="translate(... going to and fro. You can simplify the code and eliminate all the transforms. Also consider using a <defs> star with <use> for the grey and the orange stars (since the d attribute is the same). Additionally you may want to clip only the orange star.