【问题标题】:Layout with text and images where bottom and top is aligned底部和顶部对齐的文本和图像布局
【发布时间】:2017-12-03 01:43:36
【问题描述】:

尝试创建这样的布局:

规则:

  • 2 列。
  • 每列可以包含任意数量的图像(在我们的示例中为 2 和 3)。
  • 每张图片都可以有任意长度的标题。
  • 必须对齐每列中的第一个图像。
  • 每列的最后一张图片必须对齐。
  • 我们不知道标题会占用多少行,但我们知道每张图片的纵横比。

JSFiddle 的当前工作正在进行中https://jsfiddle.net/LpL2jmgj/ 如果我能找到解决方案,将发布更新。

我想即使使用display:grid 也不可能仅通过 CSS 做到这一点?

【问题讨论】:

  • col-1 / col-2 宽度比例是否固定?
  • @fen1x 否。每列可以有任意宽度,但其中的图像必须形成一个矩形 - 因此每列中第一个图像的顶部垂直对齐,每个列中最后一个图像的底部列垂直对齐。
  • 对于给定的图像,如果左上角图像下有 3 行文本,那么 空白 应该在第 2 列的什么位置?
  • @Marvin3 使用该图像,您还在右上方图像下方添加了文本......我们是否该文本来自?

标签: javascript html css flexbox css-grid


【解决方案1】:

这是你的答案??? 我只是使用弹性盒子和一点边距,希望你喜欢它:)

* {
	-webkit-box-sizing: border-box;
	-moz-box-sizing: border-box;
	
	box-sizing: border-box; 
}
.columns {
    display: flex;
    flex-direction: column;
    justify-content: flex-start;
    align-items: flex-start;
    width: 100%;
    flex-wrap: nowrap;
    margin : 5px;
}
.rows{
    display: flex;
    flex-direction: row;
    justify-content: flex-start;
    align-items: flex-start;
    width: 100%;
    margin : 5px;
    flex-wrap: nowrap;
}
p{
  margin : 2px;
}
.col {
  width: 100px;
  margin : 5px;
  position: relative;
}
.col1 {
  margin : 5px;
  flex : 5;
  background-color : red;
  height : 200vh;
}
.col2{
  margin : 5px;
  flex:3;
  background-color : blue;
  height : 200vh;
}
.image {
  width: 100%;
  position: relative;
  height: 0;
  overflow: hidden;
}
.image img {
  position: absolute;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
}
<div class="rows">
  <div class="col1">
     <div class="columns">
      <img style="width:100%;" src="https://dummyimage.com/600x400/000/fff" />
      <p>text</p>
    </div>
    <div class="columns">
    <img style="width:100%;" src="https://dummyimage.com/600x400/000/fff" />
      <div class="rows">
      <p>text</p>
      <p>text</p><p>text</p><p>text</p><p>text</p><p>text</p>
      <p>text</p><p>text</p><p>text</p><p>text</p><p>text</p>
      </div>
    </div>
  </div>
  <div class="col2">
   <div class="columns">
      <img style="width:100%" src="https://dummyimage.com/600x400/000/fff" />
      <div class="rows"><p>text</p> <p>text</p> <p>text</p> </div>
      <div class="rows"><p>text</p> <p>text</p> <p>text</p> </div>
      <div class="rows"><p>text</p> <p>text</p> <p>text</p> </div>
    </div>
    <div class="columns">
      <img style="width:100%" src="https://dummyimage.com/600x400/000/fff" />
      <p>text</p>
    </div>
    <div class="columns">
      <img style="width:100%" src="https://dummyimage.com/600x400/000/fff" />
      <p>text</p>
    </div>
  </div>
</div>

【讨论】:

  • 您错过了“每列中的最后一张图片必须在底部对齐。”
  • 投反对票的是我……如果你解决了这个问题,我会收回我的反对票,甚至可能投赞成票:)……但在那之前你的答案不起作用,因此投反对票
  • 好吧,它是否有一点差异并不重要,图像不是底部对齐,它们应该......如果你在图像下添加更多文本,它会再次改变,所以这个答案不能解决问题。
  • 这里没有人不礼貌,只是告诉你哪里出了问题。
  • 与其和我争论,告诉我我应该怎么做,不如用可行且问题已解决的东西来更新你的答案......或者删除它,你会得到减分,或者你打算保留一个错误的答案?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-07-18
  • 2015-10-04
  • 1970-01-01
  • 1970-01-01
  • 2012-02-16
  • 2018-02-09
相关资源
最近更新 更多