html - 在 flex 容器中,将子级 1 放置在顶部,将 2 和 3 堆叠在底部

标签 html css position flexbox

我使用 flexbox 使父级的高度相同(需要它们匹配),并使用 flex-wrap 来包裹每 3 个。

然后我有一个始终位于底部的按钮。

第一个子元素的高度始终不同,但宽度始终相同。

现在的技巧是,我希望子级 2 位于按钮顶部的底部,并且都清除子级 1 的内容。

绝对定位不会清除子级 1 的文本。

我尝试了 flex-grow,但它对我不起作用,可能是因为我是 Flexbox 新手。

请看 fiddle :https://jsfiddle.net/mm783qc6/4/

.contain {
  display: flex;
  border: 1px solid #000;
  width: 100%;
  flex-wrap: wrap;
}
.p {
  width: 29.5%;
  float: left;
  position: relative;
  margin: 0 10px 30px;
  border: 1px solid red;
}
.bb {
  position: absolute;
  bottom: 0;
  border: 1px solid green;
}
.c1 {
  border: 1px solid blue;
}
.c2 {
  border: 1px solid orange;
}
<div class="contain">

  <div class="p">
    <div class="c1">
      <p>Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text</p>
    </div>
    <div class="c2">
      <p>Go to bottom, but on top of bottom bumper - text can grow sdfkjlkfdj;lksjdf;</p>
    </div>
    <div class="bb">
      <p>---------------Button--------------</p>
    </div>
  </div>

  <div class="p">
    <div class="c1">
      <p>Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and textRandom Height and text Random Height and
        text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text</p>
    </div>
    <div class="c2">
      <p>Go to bottom, but on top of bottom bumper - text can grow sdfkjlkfdj;lksjdf;</p>
    </div>
    <div class="bb">
      <p>-------Button-------</p>
    </div>
  </div>

  <div class="p">
    <div class="c1">
      <p>Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and text Random Height and textRandom Height and text Random Height and
        text Random Height and text Random Height and text Random Height and text Random Height and</p>
    </div>
    <div class="c2">
      <p>Go to bottom, but on top of bottom bumper - text can grow sdfkjlkfdj;lksjdf;</p>
    </div>
    <div class="bb">
      <p>-------Button-------</p>
    </div>
  </div>


  <div class="p">
    <div class="c1">
      <p>Random Height and text Random Height</p>
    </div>
    <div class="c2">
      <p>Go to bottom, but on top of bottom bumper - text can grow sdfkjlkfdj;lksjdf;</p>
    </div>
    <div class="bb">
      <p>---------------Button--------------</p>
    </div>
  </div>

  <div class="p">
    <div class="c1">
      <p>Random Height and text Random Height</p>
    </div>
    <div class="c2">
      <p>Go to bottom, but on top of bottom bumper - text can grow sdfkjlkfdj;lksjdf;</p>
    </div>
    <div class="bb">
      <p>-------Button-------</p>
    </div>
  </div>

  <div class="p">
    <div class="c1">
      <p>Random Height and text Random Height</p>
    </div>
    <div class="c2">
      <p>Go to bottom, but on top of bottom bumper - text can grow sdfkjlkfdj;lksjdf;</p>
    </div>
    <div class="bb">
      <p>-------Button-------</p>
    </div>
  </div>
</div>

最佳答案

尝试将类 p 元素放入具有列方向的 Flex 容器中。然后使用 auto 边距将子级 2 和 3 推到底部。此方法根据需要对齐元素,并防止重叠。

修改了 CSS

.contain {
    display: flex;
    border: 1px solid #000;
    width: 100%;
    flex-wrap: wrap;
}

.p {
    width: 29.5%;
    /* float: left;              <-- remove; not necessary */
    /* position: relative;       <-- remove; not necessary */
    margin: 0 10px 30px;
    border: 1px solid red;

    display: flex;               /* new */
    flex-direction: column;      /* new */
}

.bb {
    /* position: absolute;       <-- remove; not necessary */
    /* bottom: 0;                <-- remove; not necessary */
    border: 1px solid green;
}

.c1 {
    border: 1px solid blue;
    margin-bottom: auto;         /* new; push self to top, everything else to bottom */
    flex: 1;                     /* new; consume all available free space */
}

.c2 {
    border: 1px solid orange;
}

Revised Fiddle

在此处了解有关 Flex auto 边距的更多信息:Methods for Aligning Flex Items along the Main Axis

此外,如果您希望等高的列延伸到第一行之外 - 即您希望第二行上的元素与第一行上的元素高度匹配 - 这对于 Flexbox 来说是不可能的。我在这里解释一下原因:Equal height rows in a flex container

关于html - 在 flex 容器中,将子级 1 放置在顶部,将 2 和 3 堆叠在底部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36291903/

相关文章:

html - 需要帮助使此页面响应

html - 有没有办法在没有 jQuery/javascript 的情况下将元素与其 'grandparent' 对齐?

javascript - If Else 语句切换 'Help' 按钮

javascript - 在javascript中使图像移动适应窗口大小

html - 如何避免由绝对定位的div引起的水平滚动

php - 表单提交后,在输入中显示新值而不是缓存的值

javascript - Apache 404 错误 - Drupal

jquery - Tab div 未拉伸(stretch)到内联内容。 jQuery 选项卡

java - 如何更改 JavaFX 中的 TableView 框架颜色?

html - 根据内容固定宽度和高度的位置(而不是 100%)