简体   繁体   English

根据容器的高度和宽度计算图像的最佳尺寸

[英]Calculate best dimensions for image based on height and width of container

If I have a container which has a fixed width of 500px and height of 500px and an image with a width of 600px and height of 1200px whats the best way to calculate the dimensions I need to have the image 'fit' within the container while preserving the aspect ratio of the image? 如果我有一个固定宽度为500px,高度为500px的容器,并且图像的宽度为600px,高度为1200px,那么什么是计算尺寸的最佳方法,我需要在保留容器的情况下使图像“适合”容器图像的纵横比?

If the image were smaller, by both width and height of the container; 如果图像较小,则按容器的宽度和高度进行; do nothing. 没做什么。

Based on the example dimensions above I know the best dimensions for the image are: 根据上面的示例尺寸,我知道图像的最佳尺寸是:

Width = 250 Height = 500 宽度= 250高度= 500

But what is the calculation I need to perform to achieve this? 但是,要实现这一目标,我需要执行什么计算?

You have to first determine if it's the height or the width of the image is causing the shrinking of the image. 您必须首先确定是图像的高度还是宽度导致图像缩小。 Then based on that you determine aspect ratio of the w/h of the image. 然后基于此确定图像w / h的纵横比。 Then set the max value either height or width to 500 and multiply that by the aspect ratio to get the other number of the image. 然后将高度或宽度的最大值设置为500,然后将其乘以纵横比,以获取其他图像数量。

In your example the aspect ration is 1/2, w/h and since you're shrinking the height to 500 the width should be 1/2 * 500 = 250 as the width. 在您的示例中,宽高比为1/2,w / h,由于将高度缩小到500,因此宽度应为宽度的1/2 * 500 = 250。

JS: JS:

 $('.myimg').css('max-height', '100%'); $('.myimg').css('max-width', '100%'); 
 .container { height: 500px; widtH: 500px; background: red; display: flex; align-items: center; justify-content: center; } .bggreen{ background: green; } 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <div class="container"> <img class="myimg" src="http://placehold.it/600x1200"> </div> <hr> <div class="container bggreen"> <img class="myimg" src="http://placehold.it/60x120"> </div> <hr> <div class="container"> <img class="myimg" src="http://placehold.it/120x60"> </div> <hr> <div class="container bggreen"> <img class="myimg" src="http://placehold.it/1200x600"> </div> 

It is much simple to use CSS Solution: 使用CSS解决方案非常简单:

set img to max-height: 100%; 将img设置为max-height: 100%; and max-width: 100%; max-width: 100%; :

 .container { height: 500px; widtH: 500px; background: red; display: flex; align-items: center; justify-content: center; } .myimg { max-height: 100%; max-width: 100%; } .bggreen { background: green; } 
 <div class="container"> <img class="myimg" src="http://placehold.it/600x1200"> </div> <hr> <div class="container bggreen"> <img class="myimg" src="http://placehold.it/60x120"> </div> <hr> <div class="container"> <img class="myimg" src="http://placehold.it/120x60"> </div> <hr> <div class="container bggreen"> <img class="myimg" src="http://placehold.it/1200x600"> </div> 

If you want to display the image, use max-height:100%; width:auto 如果要显示图像,请使用max-height:100%; width:auto max-height:100%; width:auto if your images are always vertical. max-height:100%; width:auto如果图像始终垂直,则为max-height:100%; width:auto

What this will do is make sure that your Image will always have the biggest height it can have (ie the same as the container it's in) while retaining it's width (= same AR, but a flexible size) 这将确保您的图片始终具有最大高度(即与其所在的容器相同),同时保持其宽度(=相同的AR,但尺寸灵活)

You can use image as the background of container div with background size fit to cover, that will help you to achieve it automatically. 您可以使用图像作为容器div的背景,背景尺寸适合覆盖,这将帮助您自动实现。

 .container { height: 500px; width: 500px; background-size: cover; background-repeat: no-repeat; background-position: center; } 
 <div class="container" style="background-image: url('http://lorempixel.com/600/1200/cats/1/')"></div> 

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

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