[英]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. 与原始代码的区别在于width
和height
都是auto
以保持宽高比。 Since max-height
and max-width
are provided, they will determine which dimension fills the container. 由于提供了max-height
和max-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-height
和min-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
方法:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.