简体   繁体   English

将弹性项目的内容与底部对齐

[英]Align content of flex items to bottom

Very simple, a container has to have its children stretched (as they are), but the content of the children divs has to be aligned to the bottom, same as flex-end but while maintaining the height. 非常简单,一个容器必须让它的孩子伸展(因为它们),但子div的内容必须与底部对齐,与flex-end相同,但保持高度。

Any ideas about how to do that? 关于如何做到这一点的任何想法?

Take a look at this pen: http://codepen.io/anon/pen/NAjRvo 看看这支笔: http//codepen.io/anon/pen/NAjRvo

 .container { height: 100px; background: BurlyWood; display: flex; align-items: stretch; } .item{ flex:1; margin: 4px; background: Crimson; } .item p { margin: 0; text-align: center; } 
 <div class="container"> <div class="item"> <p>lorem</p> </div> <div class="item"> <p>ipsum</p> </div> <div class="item"> <p>dolor</p> </div> </div> 

Make the children flex-containers as well.. then align accordingly 让儿童成为柔性容器......然后相应地对齐

 .container { height: 100px; background: BurlyWood; display: flex; } .item{ flex:1; margin: 4px; background: Crimson; display: flex; justify-content: center; align-items: flex-end; } .item p { margin: 0; text-align: center; } 
 <div class="container"> <div class="item"> <p>lorem</p> </div> <div class="item"> <p>ipsum</p> </div> <div class="item"> <p>dolor</p> </div> </div> 

Or similarly with flex-columns 或类似于flex-columns

 .container { height: 100px; background: BurlyWood; display: flex; } .item{ flex:1; margin: 4px; background: Crimson; display: flex; flex-direction: column; justify-content: flex-end; } .item p { margin: 0; text-align: center; } 
 <div class="container"> <div class="item"> <p>lorem</p> </div> <div class="item"> <p>ipsum</p> </div> <div class="item"> <p>dolor</p> </div> </div> 

In addition to the keyword properties already mentioned, you can also use flex auto margins : 除了已经提到的关键字属性,您还可以使用flex auto margin

 .container { height: 100px; background: BurlyWood; display: flex; align-items: stretch; } .item { flex: 1; margin: 4px; background: Crimson; justify-content: center; text-align: center; display: flex; /* NEW */ } .item p { margin: auto 0 0 0; /* ADJUSTED */ } 
 <div class="container"> <div class="item"> <p>lorem</p> </div> <div class="item"> <p>ipsum</p> </div> <div class="item"> <p>dolor</p> </div> </div> 

You can use position absolute for that. 你可以使用绝对位置。

In order to keep the text centered, you need left and right:0 . 为了保持居中文本,您需要leftright:0

 .container { height: 100px; background: BurlyWood; display: flex; align-items: stretch; } .item{ flex:1; margin: 4px; background: Crimson; position:relative; } .item p { margin: 0; text-align: center; position:absolute; bottom:0; left:0; right:0; } 
 <div class="container"> <div class="item"> <p>lorem</p> </div> <div class="item"> <p>ipsum</p> </div> <div class="item"> <p>dolor</p> </div> </div> 


You could also use grow on one element, so the other will just take space that it requires. 你也可以在一个元素上使用grow ,所以另一个元素只需占用它所需的空间。 This is not a real clean solution in my eyes, but it works: 这不是我眼中真正干净的解决方案,但它有效:

 .container { height: 100px; background: BurlyWood; display: flex; align-items: stretch; } .item{ flex:1; margin: 4px; background: Crimson; display:flex; flex-direction: column; } .item div { flex:1; } .item p { margin: 0; text-align: center; } 
 <div class="container"> <div class="item"> <div>&nbsp;</div> <p>lorem</p> </div> <div class="item"> <div>&nbsp;</div> <p>ipsum</p> </div> <div class="item"> <div>&nbsp;</div> <p>dolor</p> </div> </div> 

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

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