简体   繁体   English

使flex div的子代具有相同的高度(基于“行”)

[英]Make children of flex div have same height (on per-“row” basis)

I have a flex div ( .parent ) with child divs (you guessed it, .child ) that wraps into more rows. 我有一个带有子div(您猜对了, .child )的flex div( .parent ),该子div包装成更多行。 My problem is that I can't seem to get the border of each div .card to have the same height without ruining my "grid". 我的问题是,在不破坏我的“网格”的情况下,我似乎无法使每个div .card的边框具有相同的高度。 When I try designating a specific height for .child , etc. the heights look terrible because not each row has the same lengths of content. 当我尝试为.child等指定特定高度时,这些高度看起来很糟糕,因为并非每一行都有相同的内容长度。 I've seen examples of this but am not sure how to tackle it with flex so that the multiple rows have the same height (without every single "cell" being the same height and producing as much of a mess. 我已经看到了这样的示例,但是不确定如何使用flex来解决它,以便使多行具有相同的高度(没有每个“单元格”具有相同的高度,并且会产生很多混乱。

 .parent { display: flex; flex-grow: 1; flex-wrap: wrap; margin: 0 auto; max-width: 100%; width: 100%; } .child { height: 100%; margin-top: 20px; margin: 1%; display: inline-flex; } .a-title { font-size: 1.3em; font-weight: 700; width: 100%; } .child .card { border-radius: 3px; border: 1px solid; font-size: .8em; padding: 10px; display: inline-block; overflow: hidden; /* height: 600px; */ } 
 <div class="parent"> <div class="child"> <div class="card"> <img src="https://i.imgur.com/D1p6UX3.jpg" width="240" height="240"><br> <p class="a-title"> Title </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> <a href="#">Some fake link</a> </p> </div> </div> <div class="child"> <div class="card"> <img src="https://i.imgur.com/D1p6UX3.jpg" width="240" height="240"><br> <p class="a-title"> Title </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> <a href="#">Some fake link</a> </p> </div> </div> <div class="child"> <div class="card"> <img src="https://i.imgur.com/D1p6UX3.jpg" width="240" height="240"><br> <p class="a-title"> Title </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> <a href="#">Some fake link</a> </p> </div> </div> <div class="child"> <div class="card"> <img src="https://i.imgur.com/D1p6UX3.jpg" width="240" height="240"><br> <p class="a-title"> Title </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> <a href="#">Some fake link</a> </p> </div> </div> <div class="child"> <div class="card"> <img src="https://i.imgur.com/D1p6UX3.jpg" width="240" height="240"><br> <p class="a-title"> Title </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> <a href="#">Some fake link</a> </p> </div> </div> <div class="child"> <div class="card"> <img src="https://i.imgur.com/D1p6UX3.jpg" width="240" height="240"><br> <p class="a-title"> Title </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> <a href="#">Some fake link</a> </p> </div> </div> <div class="child"> <div class="card"> <img src="https://i.imgur.com/D1p6UX3.jpg" width="240" height="240"><br> <p class="a-title"> Title </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> <a href="#">Some fake link</a> </p> </div> </div> </div> 

Trying to avoid what's going on here (current code example): https://jsfiddle.net/4gy7fzw1/ 试图避免这里发生的事情(当前代码示例): https : //jsfiddle.net/4gy7fzw1/

Example of code without set heights: https://jsfiddle.net/t2yzfkm9/ 没有设置高度的代码示例: https : //jsfiddle.net/t2yzfkm9/

Typically, to have each column in a flexbox layout have the same height, all you need to do is specify display: flex on the parent element ( .parent ). 通常,要使flexbox布局中的每一列具有相同的高度,您所需要做的就是在父元素( .parent )上指定display: flex In your specific situation, you're setting height: 100% on the child element ( .child ). 在特定情况下,您要设置height: 100%在子元素( .child )上。

In flexbox, height: 100% actually does the opposite of what you may expect, due to percentage-driven values being based on the height of the containing block. 在flexbox中, height: 100%由于百分比驱动的值基于包含块的高度,因此height: 100%实际上您期望的相反 height: auto will allow the element to expand (and is the default). height: auto将允许元素扩展(默认设置)。

In short, to have your columns be equal height, simply remove height: 100% from .child : 简而言之, .child列的高度相等,只需删除 height: 100% .child height: 100% .child

 .parent { display: flex; flex-grow: 1; flex-wrap: wrap; margin: 0 auto; max-width: 100%; width: 100%; } .child { /*height: 100%;*/ margin-top: 20px; margin: 1%; display: inline-flex; } .a-title { font-size: 1.3em; font-weight: 700; width: 100%; } .child .card { border-radius: 3px; border: 1px solid; font-size: .8em; padding: 10px; display: inline-block; overflow: hidden; /* height: 600px; */ } 
 <div class="parent"> <div class="child"> <div class="card"> <img src="https://i.imgur.com/D1p6UX3.jpg" width="240" height="240"><br> <p class="a-title"> Title </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> <a href="#">Some fake link</a> </p> </div> </div> <div class="child"> <div class="card"> <img src="https://i.imgur.com/D1p6UX3.jpg" width="240" height="240"><br> <p class="a-title"> Title </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> <a href="#">Some fake link</a> </p> </div> </div> <div class="child"> <div class="card"> <img src="https://i.imgur.com/D1p6UX3.jpg" width="240" height="240"><br> <p class="a-title"> Title </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> <a href="#">Some fake link</a> </p> </div> </div> <div class="child"> <div class="card"> <img src="https://i.imgur.com/D1p6UX3.jpg" width="240" height="240"><br> <p class="a-title"> Title </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> <a href="#">Some fake link</a> </p> </div> </div> <div class="child"> <div class="card"> <img src="https://i.imgur.com/D1p6UX3.jpg" width="240" height="240"><br> <p class="a-title"> Title </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> <a href="#">Some fake link</a> </p> </div> </div> <div class="child"> <div class="card"> <img src="https://i.imgur.com/D1p6UX3.jpg" width="240" height="240"><br> <p class="a-title"> Title </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> <a href="#">Some fake link</a> </p> </div> </div> <div class="child"> <div class="card"> <img src="https://i.imgur.com/D1p6UX3.jpg" width="240" height="240"><br> <p class="a-title"> Title </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> <a href="#">Some fake link</a> </p> </div> </div> </div> 

width and height don't work with flex, use "flex" property to give/set width of the elements. width和height不适用于flex,请使用“ flex”属性指定/设置元素的宽度。

I have updated your css for "child" and "parent" class to make all the cards same in height. 我已经为“孩子”和“父母”类更新了CSS,以使所有卡片的高度相同。

 .parent { display: flex; flex-grow: 1; flex-wrap: wrap; } .child { margin: 1%; display: flex; align-items: stretch; } .a-title { font-size: 1.3em; font-weight: 700; width: 100%; } .child .card { border-radius: 3px; border: 1px solid; font-size: .8em; padding: 10px; display: inline-block; overflow: hidden; /* height: 600px; */ } 
 <div class="parent"> <div class="child"> <div class="card"> <img src="https://i.imgur.com/D1p6UX3.jpg" width="240" height="240"><br> <p class="a-title"> Title </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> <a href="#">Some fake link</a> </p> </div> </div> <div class="child"> <div class="card"> <img src="https://i.imgur.com/D1p6UX3.jpg" width="240" height="240"><br> <p class="a-title"> Title </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> <a href="#">Some fake link</a> </p> </div> </div> <div class="child"> <div class="card"> <img src="https://i.imgur.com/D1p6UX3.jpg" width="240" height="240"><br> <p class="a-title"> Title </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> <a href="#">Some fake link</a> </p> </div> </div> <div class="child"> <div class="card"> <img src="https://i.imgur.com/D1p6UX3.jpg" width="240" height="240"><br> <p class="a-title"> Title </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> <a href="#">Some fake link</a> </p> </div> </div> <div class="child"> <div class="card"> <img src="https://i.imgur.com/D1p6UX3.jpg" width="240" height="240"><br> <p class="a-title"> Title </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> <a href="#">Some fake link</a> </p> </div> </div> <div class="child"> <div class="card"> <img src="https://i.imgur.com/D1p6UX3.jpg" width="240" height="240"><br> <p class="a-title"> Title </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> <a href="#">Some fake link</a> </p> </div> </div> <div class="child"> <div class="card"> <img src="https://i.imgur.com/D1p6UX3.jpg" width="240" height="240"><br> <p class="a-title"> Title </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> Some example text that I'm too lazy to ipsum right now. </p> <p> <a href="#">Some fake link</a> </p> </div> </div> </div> 

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

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