简体   繁体   English

flexbox:使用flex-direction拉伸元素的高度:行

[英]flexbox: stretching the height of elements with flex-direction: row

I want to stretch two div elements ( .sideline and .main-contents ) to reach the bottom of the page and stay at a certain height from the footer. 我想拉伸两个div元素( .sideline.main-contents )以到达页面的底部,并保持页脚一定的高度。

The two divs are nested inside a div ( .row-elements ) with the flex-direction: row since I wanted them to be on the same row. 这两个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 Codepen

The commented out code in the css is the things I have tried. CSS中注释掉的代码就是我尝试过的东西。

I tried to set the body as flex and give the row-elements class flex property of 1 0 auto which didn't work. 我试图将body设置为flex,并给row-elements类的flex属性设置为1 0 auto ,但该属性无效。

I tried nesting the row-elements class (which has the flex-direction of row ) in another div with a flex-direction of column and setting .row-elements to flex:1 0 auto which also didn't work. 我试着嵌套row-elements类(具有flex-directionrow )在另一个DIV与flex-directioncolumn和设置.row-elementsflex:1 0 auto也没有工作。

I tried totally removing the the row-elements class but the two divs won't come on the same row. 我试图完全删除row-elements类,但是两个div不在同一行上。

Any solution will be appreciated. 任何解决方案将不胜感激。

To stick the footer to the bottom, here are two methods: 要将页脚固定在底部,有两种方法:

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

Align the container vertically with flex-direction: column . 将容器与flex-direction: column垂直对齐。 Then pin the last element to the bottom with justify-content: space-between . 然后使用justify-content: space-between将最后一个元素固定在底部。

revised codepen 修改后的代码笔

Method #2: auto margins 方法2: auto边距

Also with the container in column-direction, apply margin-top: auto to the footer, which spaces it away from the other flex items. 同样,使容器在列方向上,对页脚应用margin-top: auto ,使它与其他弹性项目隔开。 (Seems you're already familiar with this method.) (似乎您已经熟悉此方法。)

Here's a detailed explanation for both: Methods for Aligning Flex Items 以下是这两种方法的详细说明: 对齐弹性项目的方法

Make sure to define a height for your container, unless you simply want content height. 确保定义容器的高度,除非您只希望内容高度。 In my example I've used height: 100vh on body . 在我的例子我用height: 100vhbody

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM