【问题标题】:Fill the remaining height or width in a flex container填充弹性容器中剩余的高度或宽度
【发布时间】:2016-10-11 05:36:08
【问题描述】:

我在 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>

【问题讨论】:

  • 你也可以使用grid-template-rows/columns

标签: html css flexbox


【解决方案1】:

基本上,我试图让我的代码在“行”上有一个中间部分,以自动调整两边的内容(在我的例子中,是虚线分隔符)。就像@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>

【讨论】:

    【解决方案2】:

    使用flex-grow 属性使弹性项目消耗主轴上的空闲空间

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

    一个常见的例子是flex-grow: 1,或者使用简写属性flex: 1

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


    你写的:

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

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

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

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

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


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

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

    【讨论】:

    • 我遇到了同样的情况,使用flex-grow: 1 的效果与flex: 1 不同。你可能知道为什么不一样吗? (我用后者解决了我的问题,谢谢)
    • 对于flex-grow: 1flex-basis 属性仍为默认值auto。使用flex: 1flex-basis 属性更改为0。请在此处查看我的答案以进行深入审查:stackoverflow.com/q/43520932/3597276@WhGandalf
    • flex-shrink 是我的关键 :)
    • 不仅如此,您还需要防止元素占用比使用min-width: 0(或min-height:0 是弹性方向列)可用的空间更多的空间,否则它会溢出超出布局中的可用空间,老实说,这非常奇怪。 stackoverflow.com/a/66689926/454780
    猜你喜欢
    • 2018-05-26
    • 1970-01-01
    • 2014-08-21
    • 1970-01-01
    • 2011-12-20
    • 2013-09-28
    • 1970-01-01
    相关资源
    最近更新 更多