[英]Get flex children not to stretch (but inside an absolute parent)?
How can I stop <li>
elements from stretching to fill the height of it's flex container?如何阻止
<li>
元素拉伸以填充其 flex 容器的高度? i just want the background to stretch and the items to sit at the top of the box.我只是想让背景伸展,物品放在盒子的顶部。
I know normally you would use, align-items: flex-start
but this fails to work as the parent div
is absolutely positioned.我知道您通常会使用
align-items: flex-start
但这无法正常工作,因为父div
是绝对定位的。
I will need to retain absolute positioning for part of the layout that is unrelated to this question.我将需要保留与此问题无关的部分布局的绝对定位。
div { display: flex; flex-direction: column; position: absolute; top: 0; right: 0; bottom: 0; left: 0; } ul { align-items: flex-start; background: gold; display: flex; flex-wrap: wrap; flex-grow: 1; } li { flex-basis: 50%; }
<div> <h1>hello</h1> <ul> <li>Test</li> <li>Test</li> <li>Test</li> <li>Test</li> <li>Test</li> </ul> </div>
Add align-content: flex-start
to the ul
(to align flex line when you wrap a flexbox, you should use the align-content
property) - see demo below:将
align-content: flex-start
添加到ul
(要在包装flexbox 时对齐flex line ,您应该使用align-content
属性) - 请参阅下面的演示:
div { display: flex; flex-direction: column; position: absolute; top: 0; right: 0; bottom: 0; left: 0; } ul { align-items: flex-start; background: gold; display: flex; flex-wrap: wrap; flex-grow: 1; align-content: flex-start; /* added */ } li { flex-basis: 50%; }
<div> <h1>hello</h1> <ul> <li>Test</li> <li>Test</li> <li>Test</li> <li>Test</li> <li>Test</li> </ul> </div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.