简体   繁体   English

如何在保持纵横比的同时使任意SVG扩展到其父容器?

[英]How to make arbitrary SVG scale up to its parent container while maintaining aspect ratio?

I want to make an arbitrary SVG scale up to its parent container while maintaining aspect ratio. 我想在保持宽高比的同时使任意SVG缩放到其父容器。 Note that I can't modify the SVGs themselves , so solutions involving modification of the SVG, like editing its viewBox attribute, are not applicable here. 请注意, 我无法修改 SVG 本身 ,因此涉及修改SVG的解决方案(例如编辑其viewBox属性)不适用于此处。

Here is a JSfiddle to demonstrate the issue: https://jsfiddle.net/mj9o0nea/ 这是一个演示该问题的JSfiddle: https ://jsfiddle.net/mj9o0nea/

If you modify the size of the window, you will see that the image scales without maintaining aspect ratio. 如果您修改窗口的大小,您将看到图像缩放而不保持纵横比。

CSS from that JSfiddle: 该JSfiddle中的CSS:

html {
  height: 100%;
  overflow-y: hidden !important;
  overflow-x: hidden !important;
}
body {
  height: 100%;
  margin: 0 0 0 0;
}
img {
  position: absolute;
  margin-left: auto;
  margin-right: auto;
  margin-top: auto;
  margin-bottom: auto;
  padding: 0;

  left: 5%;
  right: 5%;
  top: 3%;
  bottom: 3%;
  max-height: 94%;
  max-width: 90%;

}
.ph {
  width: 100%;
  max-width: 100%;
  max-height: 100%;
}

Normal images like JPGs will display correctly, scaled and centered (eg <img src=s.jpg ... ) however SVGs will not display correctly (eg <img src="data:image/svg+xml..." ) JPG之类的普通图像将正确显示,缩放并居中(例如<img src=s.jpg ... ),但是<img src="data:image/svg+xml..."将无法正确显示(例如<img src="data:image/svg+xml..."

You need to choose what happens when the aspect ratio of the parent element is different from the aspect ratio of the image. 您需要选择当父元素的纵横比与图像的纵横比不同时会发生什么。 I have forked the fiddle to show one way to do it. 把小提琴分叉了 ,展示了一种方法。 For clarity, I left out the ph class and added the code to the img css. 为了清楚起见,我省略了ph类,并将代码添加到了img css中。

The difference with the original code is that both width and height are auto to preserve the aspect ratio. 与原始代码的区别在于widthheight都是auto以保持宽高比。 Since max-height and max-width are provided, they will determine which dimension fills the container. 由于提供了max-heightmax-width ,它们将确定填充容器的尺寸。 The other dimension does not fill the container to preserve the aspect ratio. 另一个尺寸不填充容器以保持纵横比。

You could also fill the container by cropping the larger dimension. 您也可以通过裁剪较大的尺寸来填充容器 The trick is to provide min-height and min-width instead. 诀窍是改为提供min-heightmin-width

EDIT after comments: 评论后编辑:

As suggested in the comment, I had not taken into account the possibility that the size of the parent ( body ) could be larger than the native size of the img . 正如评论中建议的那样,我没有考虑父级( body )的大小可能大于img的本机大小的可能性。 In that case, I cannot think of a pure css solution. 在那种情况下,我无法想到纯css解决方案。 I have forked the fiddle again here to show a solution using jQuery . 我在这里再次分叉了小提琴,以显示使用jQuery的解决方案。 Briefly, the script stores the image ratio immediately after loading and sets a handler for resizing. 简而言之,脚本在加载后立即存储图像比率,并设置了用于调整大小的处理程序。 The procedure sets the minor dimension to that of the body and the other dimension accordingly. 该过程将次要尺寸设置为主体的尺寸,并相应地设置另一个尺寸。

EDIT for a pure javascript method: 编辑纯javascript方法:

See a pure javascript version here . 在此处查看纯javascript版本。

暂无
暂无

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

相关问题 扩大方形div以适合父div,同时保持宽高比 - Scale up a square div to fit parent div while maintaining aspect ratio 使图像适合并保持其父对象的长宽比 - make the images fit maintaining its aspect ratio to its parent 如何在保持宽高比的同时使用标题调整图像大小 - How to resizing an image with caption while maintaining its aspect ratio 如何使用其父容器制作 svg 比例尺? - How can I make an svg scale with its parent container? 在保持纵横比的同时缩放div以填充屏幕 - Scale div to fill screen while maintaining aspect ratio 如何在允许SVG自身缩放的同时保留SVG内部元素的长宽比? - How can I preserve the aspect ratio of internal elements of an SVG while allowing the SVG itself to scale? 是否可以在仍然填充容器并保持其纵横比的同时使图像尽可能小? - Is it possible to make an image as small as possible while still filling its container, and keeping its aspect ratio? 如何在保持纵横比的同时在所有设备上制作背景视频 100vh-navbar - How to make background video 100vh-navbar on all devices while maintaining aspect ratio 如何在保持纵横比的同时使此图像适合移动纵向视图? - How can I make this image fit on mobile portrait view while maintaining aspect ratio? 如何在保持纵横比的同时缩放背景图像以适应屏幕的整个宽度? 当我使用“封面”时,高度被切断 - How to scale a background image to fit the full width of the screen while maintaining aspect ratio? Height is cut off when I use "cover"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM