【问题标题】:flexbox: stretching the height of elements with flex-direction: rowflexbox:使用 flex-direction: row 拉伸元素的高度
【发布时间】:2016-08-26 21:22:39
【问题描述】:

我想拉伸两个 div 元素(.sideline.main-contents)以到达页面底部并与页脚保持一定高度。

这两个 div 嵌套在一个带有 flex-direction: row 的 div (.row-elements) 中,因为我希望它们位于同一行。

/* body {
      display:flex;
      flex-direction:column;
    } */

.one-above-all {
  display: flex;
  flex-direction: column;
  /*   flex: 1 0 auto; */
  /*   min-height: 1100px; */
  border: solid black 1px;
}
.top-block {
  margin: 0 auto;
  border: solid black 1px;
  width: 300px;
  height: 30px;
  margin-top: -15px;
}
.headline {
  border: solid black 1px;
  width: 90%;
  align-self: flex-end;
  margin-top: 40px;
  margin-right: 10px;
  height: 160px;
  display: flex;
  box-sizing: border-box;
}
.row-elements {
  display: flex;
  flex-direction: row;
  margin-top: 40px;
  align-items: stretch;
  /*   min-height: 900px;
         flex: 1 0 auto;
       */
}
.sideline {
  width: 160px;
  border: solid 1px;
  margin-left: calc(10% - 10px);
  box-sizing: border-box;
  flex-shrink: 1
}
.main-contents {
  border: solid 1px;
  margin-left: 40px;
  /*   align-self: stretch; */
  flex: 1 1 auto;
  margin-right: 10px;
  box-sizing: border-box;
}
.bottom-block {
  align-self: flex-end;
  margin-top: auto;
  border: black solid 1px;
  margin: auto;
  width: 300px;
  height: 30px;
  margin-bottom: -10px;
  /*   margin-top: 880px; */
}
/* .stretch-test {
      border:solid, 1px;
      display: flex;
      flex-direction: column;
      
      flex: 1 0 auto;
    } */
<div class="one-above-all">
  <div class="top-block"></div>
  <div class="headline"></div>
  <!-- <div class="stretch-test"> -->
  <div class="row-elements">
    <div class="sideline"></div>
    <div class="main-contents">jhjkdhfjksdhafjksdahfl</div>
    <!--</div> -->
  </div>
</div>
<div class="bottom-block">footer</div>

Codepen

css中注释掉的代码是我尝试过的东西。

我尝试将 body 设置为 flex 并赋予 1 0 auto 的行元素类 flex 属性,但不起作用。

我尝试将row-elements 类(具有rowflex-direction)嵌套在另一个具有flex-directioncolumn 的div 中,并将.row-elements 设置为flex:1 0 auto,这也不起作用.

我尝试完全删除 row-elements 类,但两个 div 不会出现在同一行。

任何解决方案将不胜感激。

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    要将页脚粘贴到底部,有两种方法:

    方法#1:justify-content: space-between

    将容器与flex-direction: column 垂直对齐。然后用justify-content: space-between 将最后一个元素固定到底部。

    revised codepen

    方法 #2:auto 边距

    同样对于列方向的容器,将margin-top: auto 应用于页脚,将其与其他弹性项目隔开。 (看来你已经熟悉这种方法了。)

    以下是两者的详细解释:Methods for Aligning Flex Items

    确保为您的容器定义一个高度,除非您只是想要内容高度。在我的示例中,我在 body 上使用了 height: 100vh

    【讨论】:

    • 效果很好。我添加了flex: 1 0 auto;,它也将.one-above-all 容器拉伸到了底部。谢谢
    • 最初,我想知道是否有一种方法可以拉伸名为.sidelinemain-contents 的两个div,就像这里拉伸.one-above-all(带有红色边框)的方式一样:@ 987654323@(我仍然想知道是否可以完成)但是您的修订也有效。
    • 是的。删除 .stretch-test 容器。然后只需给.row-elements 一个flex: 1codepen.io/anon/pen/ZOdwRK
    • 啊完美。我也不知道 vh 是什么。你每天学习新的东西。 :)
    猜你喜欢
    • 2020-09-01
    • 1970-01-01
    • 2017-06-10
    • 2017-02-25
    • 1970-01-01
    • 2014-01-22
    • 2017-10-19
    • 2021-05-09
    • 2021-07-03
    相关资源
    最近更新 更多