html - 使 flexbox 中的元素占据所有垂直和水平空间

标签 html image css flexbox

我目前有一个容器中的元素,每个元素都包含一个图像。我想使用 flexbox 将它们分布到四个 Angular 或容器中。图像在水平方向上正确分布,但不会在垂直方向上占据所有可用空间。

这是目前的样子:enter image description here

这是我的标记:

<div class="container">
    <div class="photo">
        <img src="https://placehold.it/180">
    </div>
    <div class="photo">
        <img src="https://placehold.it/180">
    </div>
    <div class="photo">
        <img src="https://placehold.it/180">
    </div>
    <div class="photo">
        <img src="https://placehold.it/180">
    </div>
</div>

还有我的 (S)CSS:

div.container {
    width: 405px;
    height: 405px;
    background: rgba(0,0,0,0.1);
    display: flex;
    flex-wrap: wrap;
    justify-content: space-between;

    div.photo {
        width: 180px;
        height: 180px;
        overflow: hidden;
        border-radius: 5px;

        img {
            height: 100%;
        }                   
    }
}

div.container {
  width: 405px;
  height: 405px;
  background: rgba(0, 0, 0, 0.1);
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}

div.container div.photo {
  width: 180px;
  height: 180px;
  overflow: hidden;
  border-radius: 5px;
}

div.container div.photo img {
  height: 100%;
}
<div class="container">
  <div class="photo">
    <img src="https://placehold.it/180">
  </div>
  <div class="photo">
    <img src="https://placehold.it/180">
  </div>
  <div class="photo">
    <img src="https://placehold.it/180">
  </div>
  <div class="photo">
    <img src="https://placehold.it/180">
  </div>
</div>

最佳答案

align-content: space-between 应用到你的 flexbox 来做到这一点(这当然假设你有足够的可用空间垂直对齐)- 请参见下面的演示:

The align-content property modifies the behavior of the flex-wrap property. It is similar to align-items, but instead of aligning flex items, it aligns flex lines. (Source: W3schools)

div.container {
  width: 405px;
  height: 405px;
  background: rgba(0, 0, 0, 0.1);
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
  align-content: space-between;
}
div.container div.photo {
  width: 180px;
  height: 180px;
  overflow: hidden;
  border-radius: 5px;
}
img {
  height: 100%;
}
<div class="container">
  <div class="photo">
    <img src="https://placehold.it/180">
  </div>
  <div class="photo">
    <img src="https://placehold.it/180">
  </div>
  <div class="photo">
    <img src="https://placehold.it/180">
  </div>
  <div class="photo">
    <img src="https://placehold.it/180">
  </div>
</div>

关于html - 使 flexbox 中的元素占据所有垂直和水平空间,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40912941/

相关文章:

javascript - jsp中添加css报错

html - 如何在 IE7 中使一个空的 anchor 标记可点击?

javascript - 为什么将单独的 "paste"事件监听器都粘贴到同一个 <div>?

python - 除了 python 中的文本,我如何删除图像上的所有内容?

html - CSS Resize/Zoom-In 对图像的影响,同时保持尺寸

html - 如果 div 是固定的,则内容超出容器的限制

javascript - 将子跨度更改为调用函数的链接

image - 火狐 : image base64 data using canvas object not working

php - 不支持驱动程序 []。 - 拉拉维尔 5.3

html - 为什么2个<div>都在考虑不同宽度的屏幕进行渲染?