繁体   English   中英

填充弹性容器中剩余的高度或宽度

[英]Fill the remaining height or width in a flex container

我在 flexbox 中并排放置了 2 个 div。 右手的应该总是相同的宽度,我希望左手抓住剩余的空间。 但除非我专门设置它的宽度,否则它不会。

所以目前,它被设置为 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); }
 <br/> <br/> <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>&gt;</strong> </div> </div>

使用flex-grow属性使 flex 项占用主轴上的可用空间。

此属性将尽可能扩展项目,调整长度以适应动态环境,例如屏幕调整大小或添加/删除其他项目。

一个常见的例子是flex-grow: 1或者使用速记属性flex: 1

因此,不要在 div 上使用width: 96% ,而是使用flex: 1


你写了:

所以目前,它被设置为 96% 这看起来不错,直到你真正压扁屏幕 - 然后右手 div 会有点缺乏它需要的空间。

定宽 div 的压扁与另一个 flex 属性有关: flex-shrink

默认情况下,flex 项目被设置为flex-shrink: 1这使它们能够收缩以防止容器溢出。

要禁用此功能,请使用flex-shrink: 0

有关更多详细信息,请参阅此处答案中flex-shrink factor部分:


在此处了解有关沿主轴的flex 对齐的更多信息:

在此处了解有关沿交叉轴的flex 对齐的更多信息:

基本上,我试图让我的代码在“行”上有一个中间部分,以自动调整两侧的内容(在我的情况下,是虚线分隔符)。 就像@Michael_B 建议的那样,关键是在行容器上使用display:flex并且至少确保行中的中间容器的flex-grow值至少比外部容器高 1(如果外部容器没有任何应用的flex-grow属性,中间容器只需要 1 来表示flex-grow )。

这是我尝试做的事情的图片以及我如何解决它的示例代码。

在此处输入图片说明

 .row { background: lightgray; height: 30px; width: 100%; display: flex; align-items:flex-end; margin-top:5px; } .left { background:lightblue; } .separator{ flex-grow:1; border-bottom:dotted 2px black; } .right { background:coral; }
 <div class="row"> <div class="left">Left</div> <div class="separator"></div> <div class="right">Right With Text</div> </div> <div class="row"> <div class="left">Left With More Text</div> <div class="separator"></div> <div class="right">Right</div> </div> <div class="row"> <div class="left">Left With Text</div> <div class="separator"></div> <div class="right">Right With More Text</div> </div>

暂无
暂无

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

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