简体   繁体   English

在 HTML 中调整 SVG 大小

[英]Resizing SVG in HTML

I found this question Resizing SVG in HTML but the answer (removing the <svg> width and height attributes and resizing the viewBox attribute) doesn't correctly rescale my SVG file.我发现这个问题Resizing SVG in HTML但答案(删除<svg> widthheight属性并调整viewBox属性的大小)没有正确重新调整我的 SVG 文件。

It's a logo file that I purchased from Looka and I'd like to begin using it but I'm stumped as to how to rescale|resize it.这是我从Looka购买的徽标文件,我想开始使用它,但我不知道如何重新缩放|调整它的大小。

<?xml version="1.0" encoding="UTF-8"?>
<svg
  xmlns="http://www.w3.org/2000/svg"
  xmlns:xlink="http://www.w3.org/1999/xlink"
  version="1.1"
  width="3171.4285714285716"
  height="2645.5238095238096"
  viewBox="0 -25.714285714285715 3171.4285714285716 2645.5238095238096">
   <rect fill="#67b231" width="3171.4285714285716" height="2645.5238095238096" />
   <g transform="scale(8.571428571428571) translate(10, 10)">
      <defs id="SvgjsDefs2369" />
      <g transform="matrix(...)"></g>
      <g transform="matrix(...)"></g>
      <g transform="matrix(...)"></g>
   </g>
</svg>

If I remove the <svg> width and height , the viewBox retains the size.如果我删除<svg> widthheightviewBox会保留大小。

If I also then reduce the viewBox , the image retains the same size, I assume (!?) because the child <rect> contains width and height attributes too.如果我也缩小viewBox ,图像保持相同的大小,我假设(!?)因为子<rect>也包含widthheight属性。

But, if I remove the <rect> width and height attributes, the result is similarly-sized whitespace (and no logo).但是,如果我删除<rect> widthheight属性,结果是类似大小的空白(并且没有徽标)。

  1. How should I approach arbitrarily rescaling|resizing the image?我应该如何处理任意缩放|调整图像大小?
  2. Should I preserve the width : height ratio when I adjust the viewBox ?调整height时是否应该保留width viewBox
  3. Should the viewBox have a positive y-position (currently -25.714... )? viewBox是否应该有一个正的 y 位置(当前-25.714... )?

How about this as a useful snippet.作为一个有用的片段怎么样。 Without editing the <svg> code at all I've set it up so that you can wrap any <svg> in a div with class of svgSize like this:完全不编辑<svg>代码,我已经设置了它,以便您可以将任何<svg>包装在具有 svgSize 类的 div 中,如下所示:

<div class="svgSize"><svg>...</svg></div>

Then using an inline style you can pass either --svgHeight or --svgWidth using whatever value you wish, so for example --svgHeight: 100px and the SVG will resize to the value given while keeping it's aspect ratio.然后使用内联样式,您可以使用您希望的任何值传递--svgHeight--svgWidth ,例如--svgHeight: 100px并且 SVG 将调整为给定的值,同时保持其纵横比。 If you don't pass either value it will default to auto which will resize to fill the parent如果您不传递任何一个值,它将默认为 auto ,它将调整大小以填充父级

Gotta love CSS Custom Properties一定要喜欢 CSS 自定义属性

 .svgSize { display: inline-flex; height: var(--svgHeight, auto); width: var(--svgWidth, auto); } .svgSize svg { height: auto; width: auto; max-height: 100%; max-width: 100%; }
 <div class="svgSize" style="--svgWidth: 5rem"> <?xml version="1.0" encoding="UTF-8"?> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="3171.4285714285716" height="2645.5238095238096" viewBox="0 -25.714285714285715 3171.4285714285716 2645.5238095238096"> <rect fill="#67b231" width="3171.4285714285716" height="2645.5238095238096" /> <g transform="scale(8.571428571428571) translate(10, 10)"> <defs id="SvgjsDefs2369" /> <g transform="matrix(...)"></g> <g transform="matrix(...)"></g> <g transform="matrix(...)"></g> </g> </svg> </div> <div class="svgSize" style="--svgHeight: 15rem"> <?xml version="1.0" encoding="UTF-8"?> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="3171.4285714285716" height="2645.5238095238096" viewBox="0 -25.714285714285715 3171.4285714285716 2645.5238095238096"> <rect fill="#67b231" width="3171.4285714285716" height="2645.5238095238096" /> <g transform="scale(8.571428571428571) translate(10, 10)"> <defs id="SvgjsDefs2369" /> <g transform="matrix(...)"></g> <g transform="matrix(...)"></g> <g transform="matrix(...)"></g> </g> </svg> </div> <div class="svgSize" style=""> <?xml version="1.0" encoding="UTF-8"?> <svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="3171.4285714285716" height="2645.5238095238096" viewBox="0 -25.714285714285715 3171.4285714285716 2645.5238095238096"> <rect fill="#67b231" width="3171.4285714285716" height="2645.5238095238096" /> <g transform="scale(8.571428571428571) translate(10, 10)"> <defs id="SvgjsDefs2369" /> <g transform="matrix(...)"></g> <g transform="matrix(...)"></g> <g transform="matrix(...)"></g> </g> </svg> </div>

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

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