在flexbox中使div填充剩余*水平*空间

Ada*_*son 159 html css css3 flexbox

我在flexbox中有两个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);
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)

Mic*_*l_B 252

使用该flex-grow属性可使Flex项目占用主轴上的可用空间.

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

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

因此,不要width: 96%使用你的div,而是使用flex: 1.


你写了:

所以目前,它设置为96%,看起来没问题,直到你真的挤压屏幕 - 然后右手div有点缺乏它所需的空间.

压缩固定宽度div与另一个flex属性有关: flex-shrink

默认情况下,将flex项设置为flex-shrink: 1允许它们缩小以防止容器溢出.

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

有关详细信息,请参阅答案中flex-shrink因子部分:


了解有关沿主轴的柔性对齐的更多信息:

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

  • 不仅如此,您还希望防止元素占用比使用“min-width: 0”(或“min-height:0”是弯曲方向列)可用的空间更多的空间,否则它将溢出超出布局中的可用空间,老实说这非常奇怪。/sf/answers/4668294851/ (13认同)
  • 使用`flex-grow:1`,`flex-basis`属性保持为`auto`,即默认值.使用`flex:1`,`flex-basis`属性变为`0`.请参阅我的回答以获得深入的评论:/sf/ask/3046465271/ @WhGandalf (4认同)
  • 我遇到了同样的情况,并且使用“flex-grow:1”与“flex:1”没有相同的效果。您也许知道为什么会有所不同吗?(我解决了后者的问题,thx) (3认同)

Luc*_*ler 18

将这两行用于 Flex 容器中的元素:

flex-grow: 1; // Fill remaining space
min-width: 0; // Don't use more space than available
Run Code Online (Sandbox Code Playgroud)


Bre*_*84c 17

基本上我试图让我的代码在'行'上有一个中间部分来自动调整两边的内容(在我的例子中,是一个虚线分隔符).就像@Michael_B建议的那样,关键是display:flex在行容器上使用,并至少确保行上的中间容器的flex-grow值至少为1(如果外部容器没有flex-grow应用任何属性).

这是我试图做的图片以及我如何解决它的示例代码.

编辑:虚线底部边框可能看起来很奇怪,具体取决于浏览器的显示方式.我个人建议使用黑色圆圈SVG作为重复的背景图像,尺寸合适并定位在中间容器的底部.当我有时间时,会添加这个替代解决方案.

在此输入图像描述

.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;
}
Run Code Online (Sandbox Code Playgroud)
<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>
Run Code Online (Sandbox Code Playgroud)