作出留在Flexbox的*水平*空间格填充

问题描述 投票:118回答:2

我有2周的div并排侧的Flexbox的。右手每个人都应该是相同的宽度,我希望左手一个只获取的剩余空间。但它不会,除非我专门设置它的宽度。

因此,在目前,它的设置为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>
html css css3 flexbox
2个回答
221
投票

使用flex-grow属性,以使柔性项消耗主轴线的自由空间。

此属性将展开项目尽可能,调整长度以动态的环境中,如屏幕调整大小或添加/移除的其他项目。

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

因此,而不是在你的DIV width: 96%,使用flex: 1


你写了:

因此,在目前,它的设置为96%,这看起来不错,直到你真的壁球在屏幕上 - 然后右手DIV变得有点饥饿,它需要的空间。

固定宽度的DIV的挤压是另一弯曲性能:flex-shrink

默认情况下,柔性项都是flex-shrink: 1这使他们能够以防止容器溢出萎缩。

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

欲了解更多详情,请参阅这里的答案flex-shrink因素部分:


详细了解这里沿主轴线弯曲排列:

了解更多关于沿着这里的交叉轴弯曲排列:


17
投票

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

这里是什么,我试图对我如何解决它做和示例代码PIC。

编辑:根据您的浏览器如何显示它点缀下边框可能会看起来很怪异。我个人建议使用黑色圆圈SVG作为一个重复的背景图像,适当大小和位置向中间容器的底部。当我得到的时间将增加这一替代解决方案。

enter image description here

.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>
© www.soinside.com 2019 - 2024. All rights reserved.