[英]How to get flexbox columns to be the same height?
I am trying to wrap my head around flexbox and I have hit a wall with something very simple that I can't seem to get right. 我试图用我的头围绕Flexbox,我用一些非常简单的东西撞墙,我似乎无法做对。
I have 6 columns that I want all the same height with 3 on each row. 我有6列我希望所有相同的高度,每行3个。 I have set the flex items to have a width of 33% but I have not set a height on anything.
我已将flex项设置为宽度为33%,但我没有设置任何高度。 I thought once the flex items had content it would take the one with the greatest height and match everything to it.
我认为一旦flex项目有内容,它将采取具有最大高度的一个并且匹配它的一切。
Here is my markup: 这是我的标记:
.container { width: 800px; padding: 0 15px; } .row-flex { display: -webkit-box; display: -ms-flexbox; display: flex; margin-right: -15px; margin-left: -15px; flex-wrap: wrap; } .flexbox { flex: 0 0 33%; padding: 0 15px; } .icon-box-1 { position: relative; margin-bottom: 50px; background: #fff; text-align: center; border: 1px solid #eee; padding: 10px; }
<div class="container"> <div class="row-flex"> <div class="flexbox"> <div class="icon-box-1"> <h1> One </h1> <h3>Title</h3> <p>lots of random text, lots of random text, lots of random text, lots of random text</p> </div> </div> <div class="flexbox"> <div class="icon-box-1"> <h1> Two </h1> <h3>Title</h3> <p>lots of random text</p> </div> </div> <div class="flexbox"> <div class="icon-box-1"> <h1> Three </h1> <h3>Title</h3> <p>lots of random text, lots of random text, lots of random text, lots of random text</p> </div> </div> <div class="flexbox"> <div class="icon-box-1"> <h1> Four </h1> <h3>Title</h3> <p>lots of random text, lots of random text, lots of random text</p> </div> </div> <div class="flexbox"> <div class="icon-box-1"> <h1> Five </h1> <h3>Title</h3> <p>lots of random text, lots of random text, lots of random text</p> </div> </div> <div class="flexbox"> <div class="icon-box-1"> <h1> Six </h1> <h3>Title</h3> <p>lots of random text, lots of random text, lots of random text</p> </div> </div> </div> </div>
The equal height columns feature comes from align-items: stretch
, which is an initial setting on a flex container. 等高列功能来自
align-items: stretch
,这是flex容器的初始设置。 It makes flex items consume all available space in the cross axis . 它使弹性项目消耗横轴中的所有可用空间。
If you check the actual size of each flex item, you will see that they are, in fact, equal height on each row ( demo ). 如果检查每个弹性项目的实际大小,您将看到它们实际上是每行的相同高度( 演示 )。
The problem is that the content of each item ( .icon-box-1
) is not stretching full height. 问题是每个项目(
.icon-box-1
)的内容没有拉伸到整个高度。 It's just height: auto
(content height), by default. 它只是
height: auto
(内容高度),默认情况下。
In order for the content to stretch full height, add display: flex
to each flex item, so align-items: stretch
goes into effect, like on the parent ( demo ). 为了使内容伸展到全高,请在每个弹性项目中添加
display: flex
,这样align-items: stretch
生效,就像在父级( 演示 )上一样。
Then use flex: 1
to make the content fill the remaining space on the main axis ( demo ). 然后使用
flex: 1
使内容填充主轴上的剩余空间( 演示 )。
.container { width: 800px; padding: 0 15px; } .row-flex { display: -webkit-box; display: -ms-flexbox; display: flex; margin-right: -15px; margin-left: -15px; flex-wrap: wrap; } .flexbox { flex: 0 0 33%; padding: 0 15px; border: 1px dashed red; /* for illustration */ display: flex; /* new */ } .icon-box-1 { flex: 1; /* NEW */ position: relative; margin-bottom: 50px; background: #fff; text-align: center; border: 1px solid #eee; padding: 10px; }
<div class="container"> <div class="row-flex"> <div class="flexbox"> <div class="icon-box-1"> <h1> One </h1> <h3>Title</h3> <p>lots of random text, lots of random text, lots of random text, lots of random text</p> </div> </div> <div class="flexbox"> <div class="icon-box-1"> <h1> Two </h1> <h3>Title</h3> <p>lots of random text</p> </div> </div> <div class="flexbox"> <div class="icon-box-1"> <h1> Three </h1> <h3>Title</h3> <p>lots of random text, lots of random text, lots of random text, lots of random text</p> </div> </div> <div class="flexbox"> <div class="icon-box-1"> <h1> Four </h1> <h3>Title</h3> <p>lots of random text, lots of random text, lots of random text</p> </div> </div> <div class="flexbox"> <div class="icon-box-1"> <h1> Five </h1> <h3>Title</h3> <p>lots of random text, lots of random text, lots of random text</p> </div> </div> <div class="flexbox"> <div class="icon-box-1"> <h1> Six </h1> <h3>Title</h3> <p>lots of random text, lots of random text, lots of random text</p> </div> </div> </div> </div>
Try adding "display: flex" to .flexbox and "flex: 0 1 auto" to the icon box so it fills the heights out. 尝试将“display:flex”添加到.flexbox,将“flex:0 1 auto”添加到图标框中,使其填满高度。 Here ya go:
你走了:
.container { width: 800px; padding: 0 15px; } .row-flex { display: -webkit-box; display: -ms-flexbox; display: flex; margin-right: -15px; margin-left: -15px; flex-wrap: wrap; } .flexbox { flex: 0 0 33%; padding: 0 15px; display: flex; flex-wrap: wrap; flex-direction: column; } .icon-box-1 { position: relative; margin-bottom: 50px; background: #fff; text-align: center; border: 1px solid #eee; padding: 10px; flex: 1 0 auto; }
<div class="container"> <div class="row-flex"> <div class="flexbox"> <div class="icon-box-1"> <h1> One </h1> <h3>Title</h3> <p>lots of random text, lots of random text, lots of random text, lots of random text</p> </div> </div> <div class="flexbox"> <div class="icon-box-1"> <h1> Two </h1> <h3>Title</h3> <p>lots of random text</p> </div> </div> <div class="flexbox"> <div class="icon-box-1"> <h1> Three </h1> <h3>Title</h3> <p>lots of random text, lots of random text, lots of random text, lots of random text</p> </div> </div> <div class="flexbox"> <div class="icon-box-1"> <h1> Four </h1> <h3>Title</h3> <p>lots of random text, lots of random text, lots of random text</p> </div> </div> <div class="flexbox"> <div class="icon-box-1"> <h1> Five </h1> <h3>Title</h3> <p>lots of random text, lots of random text, lots of random text</p> </div> </div> <div class="flexbox"> <div class="icon-box-1"> <h1> Six </h1> <h3>Title</h3> <p>lots of random text, lots of random text, lots of random text</p> </div> </div> </div> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.