我在flexbox中有两个div并排 . 右手应该总是相同的宽度,我希望左手一个只 grab 剩余的空间 . 但除非我专门设定其宽度,否则它不会 .
所以目前,它设置为96%,看起来没问题,直到你真的挤压屏幕 - 然后右手div有点缺乏它所需的空间 .
我想我可以保留它,但它感觉不对 - 就像必须有一种说法:
正确的一个总是一样的;你在左边 - 你得到了剩下的一切
.ar-course-nav {
cursor: pointer;
padding: 8px 12px 8px 12px;
border-radius: 8px;
}
.ar-course-nav:hover {
background-color: rgba(0, 0, 0, 0.1);
}
<div class="ar-course-nav" style="display:flex; justify-content:space-between;">
<div style="width:96%;">
<div style="overflow:hidden; white-space:nowrap; text-overflow:ellipsis;">
<strong title="Course Name Which is Really Quite Long And Does Go On a Bit But Then When You Think it's Stopped it Keeps on Going for even longer!">
Course Name Which is Really Quite Long And Does Go On a Bit But Then When You Think it's Stopped it Keeps on Going for even longer!
</strong>
</div>
<div style="width:100%; display:flex; justify-content:space-between;">
<div style="color:#555555; margin-right:8px; overflow:hidden; white-space:nowrap; text-overflow:ellipsis;" title="A really really really really really really really really really really really long department name">
A really really really really really really really really really really really long department name
</div>
<div style="color:#555555; text-align:right; white-space:nowrap;">
Created: 21 September 2016
</div>
</div>
</div>
<div style="margin-left:8px;">
<strong>></strong>
</div>
</div>
2 回答
使用
flex-grow
属性使弹性项目占用主轴上的可用空间 .此属性将尽可能扩展项目,将长度调整为动态环境,例如屏幕重新调整大小或添加/删除其他项目 .
一个常见示例是
flex-grow: 1
,或者使用简写属性flex: 1
.因此,在div上使用
flex: 1
而不是width: 96%
.你写了:
压缩固定宽度div与另一个flex属性相关:
flex-shrink
默认情况下,flex项目设置为
flex-shrink: 1
,这使它们能够收缩以防止容器溢出 .要禁用此功能,请使用
flex-shrink: 0
.有关详细信息,请参阅答案中的 The flex-shrink factor 部分:
了解有关沿主轴的柔性对齐的更多信息:
在此处了解有关沿交叉轴的柔性对齐的更多信息:
基本上我试图让我的代码在'row'上有一个中间部分来自动调整两边的内容(在我的例子中,是一个虚线分隔符) . 就像@Michael_B建议的那样,关键是在行容器上使用
display:flex
,并且至少要确保行上的中间容器的flex-grow
值至少为1(如果外部容器没有应用任何flex-grow
属性) .这是我试图做的图片以及我如何解决它的示例代码 .