bjmc
bjmc

Reputation: 3282

SVG path 'fill' is coloring more than expected

I've got a shape that I've defined as an SVG path, and I'd like to fill it with a solid color. The path itself looks correct, and a stroke follows the shape I want. However, when I change the fill property from none to a color, the color overflows the curve I've defined and creates a rectangle shape plus a blob.

In these examples, I'm using an inline SVG style attribute, but I get the same result using CSS styles to fill the <path>.

<p>Path with stroke looks correct:</p>
<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" width="210" height="297" viewBox="0 0 210 297" version="1.1">
  <path
   style="fill:none;stroke-width:0.3;stroke:#000" 
   d="m47.6 69.5c0 22.9 0 45.9 0 68.8 37 0 74.1 0 111.1 0 0-22.9 0-45.9 0-68.8m-111.1 0c7.5-9.2 17.7-17.8 30-18.8 11.1-0.8 20.6 7.2 26.4 15.9 5.6 9 12.6 18.9 23.6 21.3 11.1 2.1 21-5.6 27.7-13.7 1.3-1.5 2.5-3.1 3.5-4.7"/>
</svg>

<p>But 'fill' covers more than it should:</p>

<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" width="210" height="297" viewBox="0 0 210 297" version="1.1">
  <path 
   style="fill:blue;stroke-width:0.3;stroke:#000"
   d="m47.6 69.5c0 22.9 0 45.9 0 68.8 37 0 74.1 0 111.1 0 0-22.9 0-45.9 0-68.8m-111.1 0c7.5-9.2 17.7-17.8 30-18.8 11.1-0.8 20.6 7.2 26.4 15.9 5.6 9 12.6 18.9 23.6 21.3 11.1 2.1 21-5.6 27.7-13.7 1.3-1.5 2.5-3.1 3.5-4.7"/>
</svg>

How can I fix this and make the fill color follow the curve on the top of the shape?

Upvotes: 2

Views: 117

Answers (1)

enxaneta
enxaneta

Reputation: 33044

It's the way you are coding the path. If you look at the d attribute you have an m command (move to) in the middle. This means that you are not coding the path continuously. This is how I would do it:

<svg xmlns:svg="http://www.w3.org/2000/svg" xmlns="http://www.w3.org/2000/svg" width="210" height="297" viewBox="0 0 210 297" version="1.1">
  <path 
   style="fill:red;stroke-width:0.3;stroke:#000"
   d="M47.6,69.5
      C55.1,60.3,65.3,51.7,77.6,50.7
      C88.7,49.9,98.2,57.9,104,66.6
      C109.6,75.6,116.6,85.5,127.6,87.9
      C138.7,90,148.6,82.3,155.3,74.2
      C156.6,72.7,157.8,71.1,158.8,69.5
      V138.3H47.6z"/>
</svg>

Upvotes: 3

Related Questions