<html>
<head></head><body>
<div>
<svg width="300px" height="200px" viewBox="0 0 2800 1200" preserveAspectRatio="none" xmlns="http://www.w3.org/2000/svg" version="1.1">
<rect x="0" y="0" width="1500" height="1000" fill="yellow" stroke="blue" stroke-width="12"/>
<path fill="red" d="M 750,100 L 250,900 L 1250,900 z"/>
<text x="100" y="600" font-size="200" font-family="Verdana">
Stretch to fit
</text>
</svg>
</div></body>
</html>
I have the above svg markup, and have the following doubts.
- In the svg tag width,height are mentioned as 300px, 200px respectively. But when i inspect it in with layout panel(in firebug) it shows 172px width and 119px height. Why is my svg element not taking the dimension as i specified.
- When i delete the viewBox attr in svg element, its taking the dimension of 1596*1096. Again svg element not taking the dimension as i specified and no graphics is visible on the screen.
- If i give preserveAspectRatio="none" on the svg tag there is a change in the dimension of the svg element, but thats also not correct.
By searching, found that view box dimension gives the actual dimension of the svg, which is mapped to the width of the svg element container(say a div in a html page.)
Could not able to put viewbox,viewport,userspace together and understand things.
I think, basically, Firebug is lying to you. Add an outline to the
svg
element with CSS and you'll see it is taking up 300x200 pixels of space. You can also try adding the width and height to thediv
container, however if you take theviewBox
off the browser no longer has enough information to scale the image.For some more examples, I have a test page which has various combinations of
viewBox
andwidth
&height
specified in different scenarios for scaling thesvg
element with CSS.