could not able to put viewbox,viewport,userspace t

2019-08-04 06:16发布

<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.

  1. 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.
  2. 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.
  3. 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.

标签: svg
1条回答
你好瞎i
2楼-- · 2019-08-04 06:28

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 the div container, however if you take the viewBox 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 and width & height specified in different scenarios for scaling the svg element with CSS.

查看更多
登录 后发表回答