Why does this SVG image have a height of 150px
Asked Answered
I

1

15

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.

Impervious answered 20/10, 2016 at 14:3 Comment(0)
H
17

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

Hypoglossal answered 20/10, 2016 at 14:16 Comment(4)
While digging into today's D3 not setting its own height and cutting off last data item I found some posts of yours claiming 300×150px to be the default size. There is also Why does 100% equal 150 pixels? where the accepted answer is referring to the post by Amelia Bellamy-Royds I linked to in my comment. However, I wasn't able to find a reference actually backing this, say, assumption. The HTML spec defines these dimensions for the <video> element but not for SVGs. Do you know of any source backing this?Kucera
@Kucera After more than one year, here is the reference: svgwg.org/specs/integration/#svg-css-sizing. It says: "If any of the sizing attributes are missing, resolve the missing ‘svg’ element width to '300px' and missing height to '150px'".Hypoglossal
Thanks for following up! Satisfied though that the document came to life no more than 3 days ago ;-) I always find myself disliking these dimensions, they just feel so arbitrary... What an aspect ration is 2:1, anyway? I briefly sifted through some minutes of the working group's meetings looking for an explanation but wasn't able to find any. 45k challenge: find the reasoning behind these rather odd numbers!Kucera
In my case, I had a SVG file, whose height and width were set to 100%. However, the SVG was 150px high. To fix this, I added in my CSS: html, body {margin: 0; height: 100%}.Lucila

© 2022 - 2024 — McMap. All rights reserved.