【发布时间】:2019-09-25 02:58:41
【问题描述】:
我正在尝试用未知数量的图像(最多 10 个)填充某个固定大小的网页区域,从而在不溢出该区域的情况下最大化图像的大小。为了对移动设备友好,我希望它无需滚动即可在横向和纵向模式下工作。
我一直在尝试使用弹性框来包裹图像,但是它们并没有按比例缩小并最终溢出该区域。如果我使用非环绕弹性框,图像会在横向模式下缩放,而不是纵向。我觉得应该有一个很好的,简单的解决方案。有什么想法吗?
更新:
这是我目前所掌握的代码的精髓。我一直在尝试很多不同的东西,但这是最干净的。
html {
height: 100%;
}
body {
padding-top: 0;
padding-bottom: 0;
height: 100%;
}
.container {
height: 100%;
width: 100%;
display: flex;
flex-direction: column;
flex-wrap: nowrap;
text-align: center;
}
.above {
margin-bottom: 20px;
font-size: larger;
background-color: lightgrey;
}
.middle {
flex-grow: 1;
max-height: 100%;
}
.below {
margin-top: 20px;
background-color: lightgrey;
}
img {
border: 5px solid transparent;
height: 100%;
}
.images {
flex-direction: row;
justify-content: center;
display: flex;
flex-wrap: wrap;
height: 100%;
}
.image {
margin: 5px;
}
<html>
<body>
<div class="container">
<div class="above">
Some text about the images
</div>
<div class="middle">
<div class="images">
<div class="image">
<img src="https://dummyimage.com/200x200/000/fff" alt="" />
</div>
<div class="image">
<img src="https://dummyimage.com/200x200/000/fff" alt="" />
</div>
<div class="image">
<img src="https://dummyimage.com/200x200/000/fff" alt="" />
</div>
<div class="image">
<img src="https://dummyimage.com/200x200/000/fff" alt="" />
</div>
<div class="image">
<img src="https://dummyimage.com/200x200/000/fff" alt="" />
</div>
<div class="image">
<img src="https://dummyimage.com/200x200/000/fff" alt="" />
</div>
<div class="image">
<img src="https://dummyimage.com/200x200/000/fff" alt="" />
</div>
<div class="image">
<img src="https://dummyimage.com/200x200/000/fff" alt="" />
</div>
<div class="image">
<img src="https://dummyimage.com/200x200/000/fff" alt="" />
</div>
<div class="image">
<img src="https://dummyimage.com/200x200/000/fff" alt="" />
</div>
</div>
</div>
<div class="below">
<button>Ok</button>
<button>Cancel</button>
</div>
</div>
</body>
</html>
关键是要尽量让图像完全保持在称为“中间”的 div 内,但要尽可能大地缩放。这是一些线框图。
风景,10 张图片:
纵向,10 张图片:
纵向,4 张图片
【问题讨论】:
-
可能是个愚蠢的建议,但您是否尝试过使用
vw和vh来设置图片的宽度和高度? -
您已经开发的一些代码将有助于您了解您需要在这里实现什么。你在这里谈论的图像的尺寸如何?所有图像都应该以相同的大小显示吗?
-
到目前为止你有什么尝试?您的图像设置为响应式 (
width: 100%; height: auto) 吗? Flexbox 可以很好地解决这类问题 -
图像是否具有相同的宽度和高度,或者每个图像都有自己的分辨率? flexbox + vw +vh 应该可以解决问题
-
用代码和线框更新了问题。希望能解决问题。