简体   繁体   English

如何按比例缩放图像并确保其始终占据屏幕的40%

[英]how to proportionally scale an image and make sure it is always taking up 40% of screen

I have a canvas animation that currently uses the entire screen, in front of the canvas, I've placed an image. 我有一个当前使用整个屏幕的画布动画,在画布前面,我放置了一个图像。 When I re-size the window's width, the canvas and image re-size correctly and proportionally, but when my window's HEIGHT changes, the canvas re-sizes and the image falls off the page, leaving just the canvas. 当我调整窗口的宽度大小时,画布和图像正确且成比例地重新调整大小,但是当我的窗口的高度改变时,画布将重新调整大小,图像从页面上掉下来,只剩下画布了。 How can I make sure that the image will always be, say, 40% of the screen's height or that when the windows height/width change, the image will always remain in the same place proportionally speaking? 我如何确保图像始终是屏幕高度的40%,或者当窗口的高度/宽度改变时,图像始终按比例保持在同一位置?

Ideally, I would just like the image to be centered, and to scale proportionally with the screen and not fall off completely if the browser's height changes. 理想情况下,我希望图像居中,并与屏幕成比例缩放,并且如果浏览器的高度发生变化,则不会完全掉落。

This is the CSS for the img element: 这是img元素的CSS:

img {
    margin:auto;
    position: fixed;
    top: 200px;
    left: 50%;
    height:auto;
    width:100%;
    text-align:center;
    min-width:1200px;
    max-width:100%;
    transform: translate(-52%, 15%);
}

You could maybe try setting the max height of the image to 40% 您可以尝试将图片的最大高度设置为40%

Img {
max-height: 40%;
}

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

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