在绝对定位的容器中具有相同的柔性柱高度

时间:2017-08-28 08:29:03

标签: html css css3 flexbox equal-heights

Codepen:https://codepen.io/anon/pen/MvqWwg

我绝对定位div.container。其中div.wrapper。和div.wrapper中的两个div作为弹性列。这些列有背景。

如何使这些背景到达最长列的末尾(即使它们具有相同的高度),而不仅仅是容器的可见高度?我无法从position: absolute删除div.container

这是HTML代码:

<div class="container">
    <div class="wrapper">
        <div class="div1">
            <p>Some long content here</p>
            <p>Some long content here</p>
            <p>Some long content here</p>
            <p>Some long content here</p>
            <p>Some long content here</p>
            <p>Some long content here</p>
            <p>Some long content here</p>
            <p>Some long content here</p>
            <p>Some long content here</p>
        </div>
        <div class="div2">
            <p>Some even longer content here</p>
            <p>Some even longer content here</p>
            <p>Some even longer content here</p>
            <p>Some even longer content here</p>
            <p>Some even longer content here</p>
            <p>Some even longer content here</p>
            <p>Some even longer content here</p>
            <p>Some even longer content here</p>
            <p>Some even longer content here</p>
            <p>Some even longer content here</p>
            <p>Some even longer content here</p>
            <p>Some even longer content here</p>
            <p>Some even longer content here</p>
            <p>Some even longer content here</p>
            <p>Some even longer content here</p>
            <p>Some even longer content here</p>
        </div>
    </div>
</div>

这是CSS:

.container {
    position: absolute;
    top: 0;
    bottom: 0;
    right: 0;
    left: 0;
    display: flex;
    flex-direction: column;
    overflow: auto;
}
.wrapper {
    display: flex;
    align-items: stretch;
    width: 100%;
}
.div1 {
    background-color: yellow;
}
.div2 {
    flex: 1;
    background-color: green;
}

1 个答案:

答案 0 :(得分:3)

删除container上的flex属性。

我还从align-items: stretch; width: 100%;中删除了.wrapper,因为它是默认值,并且不需要设置。

&#13;
&#13;
.container {
  position: absolute;
  top: 0;
  bottom: 0;
  right: 0;
  left: 0;
  /*  commented out these 2 lines
    display: flex;
    flex-direction: column;
  */
  overflow: auto;
}

.wrapper {
  min-height: 100%;
  display: flex;
  /*  commented out these 2 lines as they are not needed
    align-items: stretch;
    width: 100%;
  */
}

.div1 {
  background-color: yellow;
}

.div2 {
  flex: 1;
  background-color: green;
}

p {
  padding: 20px 10px;
}
&#13;
<div class="container">
  <div class="wrapper">
    <div class="div1">
      <p>Some long content here</p>
      <p>Some long content here</p>
      <p>Some long content here</p>
    </div>
    <div class="div2">
      <p>Some even longer content here</p>
      <p>Some even longer content here</p>
      <p>Some even longer content here</p>
      <p>Some even longer content here</p>
      <p>Some even longer content here</p>
      <p>Some even longer content here</p>
      <p>Some even longer content here</p>
      <p>Some even longer content here</p>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;