简体   繁体   English

SVG ViewBox 到宽高

[英]SVG ViewBox To Width and Height

i really cannot understand how viewBox works i read alot about it but i want to know how can i make sure it fit the outer container like this example:我真的不明白 viewBox 是如何工作的,我读了很多关于它的内容,但我想知道如何确保它像这个例子一样适合外部容器:

<div class="container">
<svg viewBox="0 0 100% 100vh">
<rect x="0.5" y="0.5" width="1200" height="800" fill="#19dddd"/>
</svg>
</div>

css: css:

.container{
width :100%;
height:100vh;
}

.container svg{
width:100%;
height:100%
}

can someone explain how can i make it fit inside the container to take its full width and height?有人可以解释一下我怎样才能让它适合容器内部以获取其全部宽度和高度吗?

You must specify viewBox parameters without dimension units您必须指定不带尺寸单位的 viewBox 参数

viewBox="0 0 1200 800"

In order for the svg to completely fill the parent container, you need to delete the width and height of the svg or specify how you have done the width and height equal to 100%为了让svg完全填满父容器,需要删除svg的宽高或者指定你是怎么做到宽高等于100%的

 .container{ width:100vw; height:auto; }
 <div class="container"> <svg viewBox="0 0 1200 800"> <rect width="100%" height="100%" fill="#19dddd"/> </svg> </div>

MDN Web Docs' viewBox documentation has a straightforward explanation - inside the viewbox there are 4 numbers that you must modify for the min/max and default sizes. MDN Web Docs 的 viewBox 文档有一个简单的解释 - 在 viewbox 内部有 4 个数字,您必须修改它们以用于最小/最大和默认大小。 Yours probably isn't working because you've added % and vh parameteres;您的可能无法正常工作,因为您添加了 % 和 vh 参数; it only accepts numbers.它只接受数字。

So you would be better off entering the size of the svg in number units.因此,您最好以数字为单位输入 svg 的大小。

It works when you display css proterties ( width and height ) by 100%, then add preserveAspectRatio="none" to your svg.当您将 css 属性(宽度和高度)显示 100% 时,它会起作用,然后将 preserveAspectRatio="none" 添加到您的 svg。

my viewBox="0 0 750 500".我的 viewBox="0 0 750 500"。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM