admin管理员组文章数量:1516870
How can I make SVG viewBox user coordinate system the same as the viewport coordinates system provided by SVG itself (height="100%" and width="100%")?
I need this special case for a project I'm doing, SVG element should be responsive, but still we need to keep height and width 100% on the SVG itself.
So, I need something like this:
<svg height="100%" width="100%" viewBox="0, 0, 100%, 100%">
<circle cx="25" cy="25" r="20" stroke="black" strokeWidth="1" fill="black" />
</svg>
.. but the viewBox attribute doesn't accept percentages.
How can I make SVG viewBox user coordinate system the same as the viewport coordinates system provided by SVG itself (height="100%" and width="100%")?
I need this special case for a project I'm doing, SVG element should be responsive, but still we need to keep height and width 100% on the SVG itself.
So, I need something like this:
<svg height="100%" width="100%" viewBox="0, 0, 100%, 100%">
<circle cx="25" cy="25" r="20" stroke="black" strokeWidth="1" fill="black" />
</svg>
.. but the viewBox attribute doesn't accept percentages.
Share Improve this question asked May 3, 2018 at 13:08 Tamara JovicTamara Jovic 2291 gold badge3 silver badges4 bronze badges 2 |1 Answer
Reset to default 35%/px is not allowed in the viewBox, those are the maximum coordinates.
By default the SVG content is contained to the SVG size.
If you want the content to stretch to 100%, disable the aspect ratio using preserveAspectRatio="none".
You can also use preserveAspectRatio="slice" to make the content cover the SVG (like background-size: cover).
<svg height="100%" width="100%" viewBox="0 0 100 100" preserveAspectRatio="none">
There are some good articles about this: https://css-tricks.com/scale-svg/ and https://alligator.io/svg/preserve-aspect-ratio/
本文标签: javascriptSVGmake viewBox(0100100) with percentagesStack Overflow
版权声明:本文标题:javascript - SVG - make viewBox(0, 0, 100%, 100%) with percentages - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:https://www.betaflare.com/web/1738127510a2065088.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。


viewBoxattribute simply sets the maximumxandycoordinates of your graphic's internal coordinate system, which will then be stretched to the size you specify inwidthandheight. Long story short, just useviewBox="0 0 100 100"and you can use numbers between 0 and 100. – Máté Safranka Commented May 3, 2018 at 13:10preserveAspectRatioattribute. As a default, it won't. @TamaraJovic, the answer to the question depends on what "responsive" behavior you want. Please describe what should happen when the SVG resizes: should the circle remain a circle, or be stretched/shrinked to an ellipse? – ccprog Commented May 3, 2018 at 14:44