html - "space-between"没有flexbox的图片垂直分布

标签 html css image layout

我有几列不同尺寸的图像。由于尺寸未知,一列将是最高的。我现在想通过相应地增加图像之间的间隙来拉伸(stretch)其他(较小的)列以匹配该高度。这是一个示例图片:

enter image description here

这是一个 jsfiddle我用 flexbox 设置的这个例子。

#main {
  width: 50%;
  display: flex;
  justify-content: space-between;
}

.column {
  background-color: lightpink;
  margin-right: 20px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.column:last-child {
  margin-right: 0;
}

.column img {
  width: 100%;
  align-self: center;
}
<div id="main">

  <div class="column">
    <img src="http://placekitten.com/g/200/300">
    <img src="http://placekitten.com/200/300">
    <img src="http://placekitten.com/g/200/400">
  </div>

  <div class="column">
    <img src="http://placekitten.com/g/200/200">
    <img src="http://placekitten.com/200/280">
    <img src="http://placekitten.com/g/200/250">
  </div>

  <div class="column">
    <img src="http://placekitten.com/g/200/400">
    <img src="http://placekitten.com/200/220">
    <img src="http://placekitten.com/g/200/260">
  </div>

</div>

但是在我的具体情况下,我不能使用 flexbox(因为我需要绝对定位一些 child ),所以我现在正在寻找一种无需 flexbox 即可实现相同目的的方法。有什么方法可以 flexbox 来获得这种垂直的“间隔”分布?

最佳答案

基于关于绝对定位的评论:

I have tried to get the absolute positioning to work without any success. Basically I am trying to place captions underneath each images, however this captions should not be part of the flow, so the gaps should keep the same with as if there were no captions. When I tried to place the captions underneath, I ended up with all captions on the bottom of the entire column.

解决方案是将图像和标题生锈包裹在一个 div 中(或者更好的是一个 figure)并给那个位置 relative...然后绝对定位你的标题。

像这样:

#main {
  max-width: 80%;
  margin: auto;
  display: flex;
  justify-content: space-between;
}

.column {
  background-color: lightpink;
  margin-right: 20px;
  display: flex;
  flex-direction: column;
  justify-content: space-between;
}

.holder {
  position: relative;
}

.column img {
  display: block;
  max-width: 100%;
  height: auto;
}

.caption {
  position: absolute;
  bottom: 0;
  text-align: center;
  background: rgba(255, 255, 255, 0.5);
  width: 100%;
}
<div id="main">

  <div class="column">
    <div class="holder">
      <img src="http://placekitten.com/g/200/300">
    </div>
    <div class="holder"> <img src="http://placekitten.com/200/300"></div>
    <div class="holder">
      <img src="http://placekitten.com/g/200/400">
    </div>
  </div>

  <div class="column">
    <div class="holder">
      <img src="http://placekitten.com/g/200/200">
      <div class="caption">My Caption</div>
    </div>
    <div class="holder"> <img src="http://placekitten.com/200/280"></div>
    <div class="holder">
      <img src="http://placekitten.com/g/200/250">
      <div class="caption">My Caption</div>
    </div>
  </div>
  <div class="column">
    <div class="holder">
      <img src="http://placekitten.com/g/200/400">
      <div class="caption">Superduper long Caption In Here</div>
    </div>
    <div class="holder"> <img src="http://placekitten.com/200/220"></div>
    <div class="holder">
      <img src="http://placekitten.com/g/200/260">
    </div>
  </div>

</div>

关于html - "space-between"没有flexbox的图片垂直分布,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53008806/

相关文章:

java - JPEG 格式的原始数据 - JAVA

html - 背景图片不显示?

使用 CDHtmlDialog (MFC) 注入(inject) Javascript

javascript - jQuery - 单击单选按钮而不是更改单选按钮

html - 整个网页左侧不需要的边距/间隙

image - 使用 GIF 作为网站上所有图像和图片的格式有什么缺点吗?

javascript - 以错误的顺序应用的 CSS 类

css - div标签,直线对齐

javascript - 当任何国王(卡片 13)被选中时,我如何让我的游戏结束?

css - 如何绝对容器内的元素溢出 :hidden not be cropped