简体   繁体   English

Flexbox:一旦另一个弹性项目的宽度为0,如何包装一个弹性项目?

[英]Flexbox: how to wrap a flex item once the width of another flex item is 0?

I got a flex container that contains 2 elements.我有一个包含 2 个元素的 flex 容器。 Here is a JSFiddle Demo .这是一个JSFiddle 演示 The first one is a dynamic header with text.第一个是带有文本的动态标题。 The second one is a wrapper for multiple dynamic buttons.第二个是多个动态按钮的包装器。 Dynamic meaning the text is generated with javascript and does not have a specific width and there can be 2-4 buttons depending on the user (so I cannot set flex-basis or width to a specific amount of px on these flex-items).动态意味着文本是用 javascript 生成的,没有特定的width并且可以有 2-4 个按钮,具体取决于用户(因此我无法在这些 flex 项目上将flex-basiswidth设置为特定的px量)。

I have set the overflow of the text to hidden and the text-overflow property to ellipsis .我已将文本的溢出设置为hidden并将text-overflow属性设置为ellipsis

Now I would like for the buttons to wrap to a new line only when the text has completely dissapeared.现在,我希望按钮在文本完全消失时才换行。 This is what I mean:这就是我的意思:

在此处输入图片说明

This article has led me to believe it has something to do with the flex-basis property.这篇文章让我相信它与flex-basis属性有关。

The first gotcha with flex-wrap is that flex items will only begin to wrap if their sum total flex-basis is greater than the size of the flex container.使用 flex-wrap 的第一个问题是 flex 项目只有在它们的总 flex-basis 大于 flex 容器的大小时才会开始包装。

However, whatever properties I try, I cannot seem to get it to work how I want to.但是,无论我尝试什么属性,我似乎都无法让它按照我想要的方式工作。

 .wrapper { display: flex; justify-content: space-between; border: 1px solid red; max-width: 600px; } .flex-wrapper { display: flex; min-width: 0; } .header { overflow: hidden; display: inline-block; text-overflow: ellipsis; white-space: nowrap; line-height: 24px; } .actions { display: flex; /* Only wrap once the text is fully gone and not visible anymore. */ /* flex-wrap: wrap; */ } .actions button:not(:last-child) { margin-right: 10px; }
 <div class="wrapper"> <div class="flex-wrapper"> <div class="header"> Lorem ipsum dolar sit amet constructeur </div> </div> <div class="actions"> <button> button1 </button> <button> button2 </button> <button> button3 </button> <button> button4 </button> </div> </div>

You can approximate this by using a big flex-shrink on the first container.您可以通过在第一个容器上使用大的flex-shrink来近似这一点。 This will give more priority to the first container for the shrink effect.这将更优先考虑第一个容器的收缩效果。

 .wrapper { display: flex; justify-content: space-between; border: 1px solid red; max-width: 600px; } .flex-wrapper { display: flex; min-width: 0; flex-shrink:99999; } .header { overflow: hidden; display: inline-block; text-overflow: ellipsis; white-space: nowrap; line-height: 24px; } .actions { display: flex; flex-wrap: wrap; column-gap:10px; /* you can use gap with flexbox*/ }
 <div class="wrapper"> <div class="flex-wrapper"> <div class="header"> Lorem ipsum dolar sit amet constructeur </div> </div> <div class="actions"> <button> button1 </button> <button> button2 </button> <button> button3 </button> <button> button4 </button> </div> </div>

You can also simplify all the code like below:您还可以简化所有代码,如下所示:

 .wrapper { display: flex; border: 1px solid red; max-width: 600px; } .flex-wrapper { flex-basis:0; flex-grow:1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; line-height: 24px; } .actions { display: flex; flex-wrap: wrap; column-gap:10px; /* you can use gap with flexbox*/ }
 <div class="wrapper"> <div class="flex-wrapper"> Lorem ipsum dolar sit amet constructeur </div> <div class="actions"> <button> button1 </button> <button> button2 </button> <button> button3 </button> <button> button4 </button> </div> </div>

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

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