[英]Vertically aligning flex 1 content
I have a list where one of the items breaks onto two lines.我有一个列表,其中一个项目分成两行。
I need to keep all <li>
elements the same height, but also center align them, which I can't seem to do ( code on CodePen ):我需要让所有<li>
元素保持相同的高度,但也需要居中对齐它们,我似乎无法做到( CodePen 上的代码):
#test ul{ display:flex; border:1px solid black; list-style: none; padding-left:0; } #test li{ flex: 1; width:33.333333%; border-left:1px solid black; padding:10px; }
<div id="test"> <ul> <li> <a href="">Two line Text</a><br/> <a href="">Two line Text</a> </li> <li> <a href="">One line Text</a> </li> <li> <a href="">One line Text</a> </li> </ul> </div>
How can I do what I'm trying to without using any hacky paddings, margins, or heights on the "One line Text" elements?如何在不使用“一行文本”元素上的任何填充、边距或高度的情况下完成我想要做的事情? If I use the suggested align-items: center
on the ul
, then the borders no longer look correct, as the element is no longer full height.如果我在ul
上使用建议的align-items: center
,则边框看起来不再正确,因为元素不再是全高。
align-items
defaults to stretch
, which make the li
fill the height of the ul
. align-items
默认为stretch
,这使li
填充ul
的高度。
Change it to center
, by adding align-items: center;
将其更改为center
,通过添加align-items: center;
to the #test ul
rule, will align them vertically centered (this will make the li
collapse to their content though)根据#test ul
规则,将它们垂直居中对齐(尽管这会使li
折叠到它们的内容中)
#test ul { display:flex; align-items: center; /* added */ border:1px solid black; list-style: none; padding-left:0; } #test li { flex: 1; width:33.333333%; border-left:1px solid black; padding:10px; }
<div id="test"> <ul> <li> <a href="">Two line Text</a><br/> <a href="">Two line Text</a> </li> <li> <a href="">One line Text</a> </li> <li> <a href="">One line Text</a> </li> </ul> </div>
Or make the li
a flex container and vertical align the a
using justify-content: center
, which btw defaults to flex-start
.或者使li
成为一个 flex 容器并使用justify-content: center
垂直对齐a
,顺便说一句,默认为flex-start
。
Why the use of column direction and justify-content
, because there is 2 items and you most likely want to stack them on top of each other.为什么使用column direction和justify-content
,因为有 2 个项目,您很可能希望将它们堆叠在一起。
#test ul{ display:flex; border:1px solid black; list-style: none; padding-left:0; } #test li{ flex: 1; width:33.333333%; border-left:1px solid black; padding:10px; display:flex; /* added */ flex-direction: column; /* added */ justify-content: center; /* added */ }
<div id="test"> <ul> <li> <a href="">Two line Text</a> <a href="">Two line Text</a> </li> <li> <a href="">One line Text</a> </li> <li> <a href="">One line Text</a> </li> </ul> </div>
If there is only 1 item with two lines of text, we can use the default row direction and align-items
如果只有 1 个 item 有两行文本,我们可以使用默认的行方向和align-items
#test ul{ display:flex; border:1px solid black; list-style: none; padding-left:0; } #test li{ flex: 1; width:33.333333%; border-left:1px solid black; padding:10px; display:flex; /* added */ align-items: center; /* added */ }
<div id="test"> <ul> <li> <a href="">Two line Text<br>Two line Text</a> </li> <li> <a href="">One line Text</a> </li> <li> <a href="">One line Text</a> </li> </ul> </div>
You need to change the HTML structure a little bit & apply align-items: stretch
to ul
.您需要稍微更改 HTML 结构并将align-items: stretch
到ul
。
#test ul {
display: flex;
align-items: stretch;
}
And inside li
create a .inner
div and give it display: flex
with align-items: center
.在li
里面创建一个.inner
div 并给它display: flex
和align-items: center
。
#test li {
flex: 1;
display: flex;
align-items: center;
}
Have a look at the working snippet below:看看下面的工作片段:
#test ul{ display: flex; align-items: stretch; border: 1px solid black; list-style: none; padding-left: 0; } #test li { flex: 1; display: flex; align-items: center; border-left: 1px solid black; } #test li:first-child { border-left: none; } #test li .inner { padding: 10px; }
<div id="test"> <ul> <li> <div class="inner"> <a href="">Two line Text</a><br/> <a href="">Two line Text</a> </div> </li> <li> <div class="inner"> <a href="">One line Text</a> </div> </li> <li> <div class="inner"> <a href="">One line Text</a> </div> </li> </ul> </div>
Hope this helps!希望这可以帮助!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.