如何使div的高度始终与其宽度成比例(无hack)?

时间:2019-01-10 21:44:48

标签: html css3 flexbox percentage

我有一个Flexbox div,其中包含12张图像。我希望每个图像的width是该25%的{​​{1}}。该图像必须具有div的{​​{1}} = height。如果图像的尺寸不等于我的3 * 4,则必须缩放图像以适合我的3 * 4。那么我该如何实现呢?我认为我需要像133,33%这样的工具。

我的CSS和HTML:

width
calc(width*4/3)
我的代码可以完美匹配3 * 4的图像,但是如果图像具有其他尺寸,我会得到: bad image

好的,找到解决方案。只需将包装器的填充设置为133.33%而不是高度(并将img的位置设置为绝对)。这就是我所说的“技巧”或“ hack”,但是它可以很清楚地解决我的问题。此处有更多详细信息:description

3 个答案:

答案 0 :(得分:2)

您可以通过在包装器上放置padding-top: 133.33%来保持3:4的宽高比。 因此,您可以设置填充而不是设置高度。

另外,您需要将position: relative设置为包装器,并将position: absolute设置为子级,这样子级的位置将忽略填充。

以下代码段中有一个示例:

.row {
  display: flex;
  flex-wrap: wrap;
}

.column {
  width: 25%;
}

.wrapper {
  padding-top: 133.33%;
  border: 1px solid red;
  position: relative;
}

.wrapper img {
  position: absolute;
  display: block;
  width: 100%;
  height: 100%;
  top: 0;
  left: 0;
  bottom: 0;
  right: 0;
  object-fit: cover;
}
<div class="row">
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
  <div class="column">
    <div class="wrapper">
      <img src="https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcREb_QCChITKrV0oIljZeP1irlF4UT74fQbJYNiawkZ9efEgREQoA">
    </div>
  </div>
</div>

答案 1 :(得分:1)

尝试一下:

.image-grid {
  display: flex;
  flex-wrap: wrap;
}

.image-grid .image-wrapper {
  position: relative;
  width: 25%;
  height: 0;
  padding-bottom: 33.3333%;
}

.image-grid .image {
  position: absolute;
  width: 100%;
  height: 100%;
  object-fit: cover;
}
<div class="image-grid">
  <div class="image-wrapper"><img src="https://picsum.photos/200/300?1.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="https://picsum.photos/300/200?2.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="https://picsum.photos/200/200?3.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="https://picsum.photos/300/300?4.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="https://picsum.photos/300/400?5.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="https://picsum.photos/200/200?6.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="https://picsum.photos/200/200?7.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="https://picsum.photos/200/400?8.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="https://picsum.photos/400/200?9.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="https://picsum.photos/200/200?1.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="https://picsum.photos/200/200?2.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="https://picsum.photos/200/200?4.jpg" alt="" class="image"></div>
</div>

答案 2 :(得分:0)

如果容器跨越窗口的整个宽度,这是一个可行的想法:

使图像容器的宽度为25%,高度为25vw * 4/3(您有calc ...)。还要在容器上使用overflow: hidden来保持固定比率。

使图像高100%,并将其宽度设置为auto。这将使它们垂直填充容器。然后通过应用transform: translate-x(-50%)position: relativeleft: 50%;来居中。

这仅适用于高度小于宽度的4/3的图像,但是据我了解,这就是您的情况吗?

* {
  margin: 0;
}

.image-grid {
  display: flex;
  flex-wrap: wrap;
}

.image-grid .image-wrapper {
  flex-shrink: 1;
  flex-grow: 0;
  width: 25%;
  height: calc(25vw*4/3);
  overflow: hidden;
}

.image-grid .image {
  height: 100%;
  width: auto;
  display: block;
  position: relative;
  left: 50%;
  transform: translateX(-50%);
}
<div class="image-grid">
  <div class="image-wrapper"><img src="http://lorempixel.com/output/city-h-c-300-400-1.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="http://lorempixel.com/output/transport-h-c-300-400-5.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="http://lorempixel.com/output/cats-h-c-300-360-6.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="http://lorempixel.com/output/nature-h-c-300-335-10.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="http://lorempixel.com/output/transport-h-c-300-400-5.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="http://lorempixel.com/output/cats-h-c-300-360-6.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="http://lorempixel.com/output/nature-h-c-300-335-10.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="http://lorempixel.com/output/city-h-c-300-400-1.jpg" alt="" class="image"></div>

  <div class="image-wrapper"><img src="http://lorempixel.com/output/cats-h-c-300-360-6.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="http://lorempixel.com/output/city-h-c-300-400-1.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="http://lorempixel.com/output/transport-h-c-300-400-5.jpg" alt="" class="image"></div>
  <div class="image-wrapper"><img src="http://lorempixel.com/output/nature-h-c-300-335-10.jpg" alt="" class="image"></div>

</div>