作者:手机用户2602883205_410 | 来源:互联网 | 2023-05-18 07:38
Ihaveagalleryonmysitewhereuserscanuploadimages.我的网站上有一个用户可以上传图片的图库。Iwouldliketheim
I have a gallery on my site where users can upload images.
我的网站上有一个用户可以上传图片的图库。
I would like the images to sit in a div that maintains its height, the images should be no larger than 500px in height. The width should be automatic to maintain aspect ratio.
我希望图像位于保持其高度的div中,图像的高度不应超过500px。宽度应自动保持纵横比。
HTML:
I've tried this CSS:
我试过这个CSS:
#gallery{
height: 500px;
img{
max-height: 500px;
max-width: 100%;
}
}
The above works well, the gallery is always 500px high and images never exceed 500px in height. I run into problems though with smaller images, if a user uploads a really small image, I would like it 'blown up' to a minimum of 200px. I know this can be achieved by setting a min-height
on the image, the problem with this is, if the image is less than 200px in height but say, 2000px wide, the image gets blown up to 200px in height, but then the aspect ratio is screwed, as the image is wider than the images parent div.
以上效果很好,画廊总是500px高,图像高度不超过500px。我遇到问题虽然图像较小,但如果用户上传的图像非常小,我希望它“炸毁”至少200px。我知道这可以通过在图像上设置最小高度来实现,问题是,如果图像高度小于200像素,但是说宽度为2000像素,则图像的高度会高达200像素,但是纵横比被拧紧,因为图像比图像父div更宽。
How can I have a min height but retain aspect ratio?
我怎样才能达到最小高度但保持纵横比?
4 个解决方案