svgjsbincodepen

Why does this SVG image have a height of 150px


Why is this SVG image displayed at 150px height inside this 500px container? Why this specific value?

I found this weird behavior in both js bin and Codepen, so I think it is something to do with my code and not with the online editors.

enter image description here

Note: a 700px div container results in the same thing. So the height of the parent doesn't matter.

<div style="padding: 30px; background-color: yellow; height: 500px; width: 500px; ">
<svg>

  <pattern id="basicPattern" x="10" y="10" width="40" height="40" patternUnits="userSpaceOnUse" >
      <rect x= "0" y="0" width="4" height="4"
            stroke = "red"
            stroke-width = "1"
            fill = "black"/>
  </pattern>
  <!-- <rect x="0" y="0" width="300" height="151" // why this deletes the bottom line? -->
  <!-- <rect x="0" y="0" width="300" height="150" // why this deletes 1 px from the bottom line?  -->

  <!-- but this height="149" is the bottom limmit for this picture.. 
       what prevent's it bor beeing extended further -  we have unthil 500 px as you can see on the div.-->

  <rect x="0" y="0" width="300" height="149"
  stroke= "red" 
  stroke-width="2"
  fill="url(#basicPattern)" />
</svg>

This is Jsbin and this is CodePen.


Solution

  • You didn't set the SVG width and height, so it goes to the default size of 300px width x 150px height (for some user-agents).

    Here is your JSBin with the SVG width and height both set to 500px. Now the rectangle can go beyond 150px of height: https://jsbin.com/yafenemawe/1/edit?html,output