javascript - 如何调整图像的大小,使其在不大于窗口高度或窗口宽度的情况下始终尽可能最大?

标签 javascript html css

我想要一张尽可能大的图像,但无论窗口如何调整大小,我都希望图像 100% 可见。我也想保持图像比例。

我试过

img{
   width: 100vh;
}

但这会根据窗口的大小 chop 一些图像。

下面是我想要的一些示例:

When the width is restrictive

When the height is restrictive

最佳答案

您可以尝试使用屏幕尺寸设置图像,然后使用 object-fit: cover 来设置图像始终适合容器;

img{
   width: 100vw;
   height: 100vh;
   object-fit: contain;
}

关于javascript - 如何调整图像的大小,使其在不大于窗口高度或窗口宽度的情况下始终尽可能最大?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57207390/

相关文章:

html - 移动设备上的悬停效果

html - 增加td元素中 anchor 标签的高度

css - 我怎样才能删除 Bootstrap 中的额外空间

android - 如何设置 React Native 侧边菜单的样式?

c# - 网页与COM口对接控制页面

javascript - 使用 TypeScript 设置 window.location

javascript - 如何获取对象中 bool 值的计数

html - 内部下拉菜单上的 Bootstrap 折叠动画

javascript - 背景 : cover header image with menu always at the bottom of the screen

javascript - Vue 3 - "Failed to resolve component"带有子组件