简体   繁体   English

如何在 html 和 css 中制作具有保留比率的高度和宽度响应图像?

[英]How to make an Height and Width Responsive Image with Kept Ratio in html and css?

There exist many similar questions, but none has yet solved my requirements:存在许多类似的问题,但还没有解决我的要求:

  • Aspect ratio should be kept, AND应保持纵横比,并且
  • Height responsive, AND高度响应,并且
  • Width responsive, AND宽度响应,并且
  • Full image should always be seen within the browser window (ie the responsiveness should be with regards to the tightest dimension).应始终在浏览器 window 中看到完整图像(即响应性应与最紧凑的尺寸有关)。

I have achieved these requirements, but not all of them in the same time.我已经达到了这些要求,但不是同时满足所有要求。 For example:例如:

If I let the code below as it is with height="90%" width="auto" then it is height responsive but not width responsive.如果我让下面的代码使用height="90%" width="auto"那么它是高度响应的,但不是宽度响应的。

If I change to height="auto" width="90%" then it is width responsive but not height responsive.如果我更改为height="auto" width="90%"那么它是宽度响应但不是高度响应。

If I change to height="90%" width="90%" then it is both height and width responsive, but the ratio is not kept.如果我更改为height="90%" width="90%"那么它对高度和宽度都有响应,但不会保持比例。

To test, please "Run code snippet" and check with "Full page" the situation by changing both height and width of the browser window.要测试,请“运行代码片段”并通过更改浏览器 window 的高度和宽度来检查情况。

 .test { height: 90vh; width: 90%; background-color: #222; color: #eee; padding: 2rem; }.test img { align-items: center; }
 <div class="test"> <img height="90%" width="auto" src="https://images.unsplash.com/photo-1418065460487-3e41a6c84dc5?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1600&q=80"> </div>

I have tested in many different ways (eg with max-width, etc.) but not succeeded so far.我已经以许多不同的方式进行了测试(例如使用最大宽度等),但到目前为止还没有成功。 Any idea how to change the above code so that all requirements are fulfilled simultaneously?知道如何更改上述代码以同时满足所有要求吗?

try this in your css:在你的 css 中试试这个:

.test img {
   object-fit: cover;
   width: 100%;
   height: auto;
}

You can use max-width and max-height together on the image then it will keep your aspect ratio and always fit on the screen with the complete image showing您可以在图像上同时使用max-widthmax-height ,然后它将保持您的纵横比并始终适合屏幕并显示完整的图像

 body { margin: 0; }.test { background-color: #222; color: #eee; padding: 2rem; height: 90vh; width: 90%; box-sizing: border-box; }.test img { margin: auto; display: block; max-width: 100%; max-height: 100%; }
 <div class="test"> <img src="https://images.unsplash.com/photo-1418065460487-3e41a6c84dc5?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1600&q=80"> </div>

Solution with use of aspect-ratio :使用aspect-ratio解决方案:

 :root { --imageWidth: 300; --imageHeight: 150; }.image-container { max-width: calc(var(--imageWidth) * 1px); max-height: 100%; aspect-ratio: var(--imageWidth) / var(--imageHeight); }.image { display: block; height: 100%; aspect-ratio: var(--imageWidth) / var(--imageHeight); }.resize-wrapper { display: inline-block; resize: both; overflow: hidden; border: 1px solid black; }
 <div class="resize-wrapper"> <div class="image-container"> <img src="https://placehold.co/300x150/svg" class="image" /> </div> </div>

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

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