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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/37745051/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 13:22:57  来源:igfitidea点击:

Make div fill remaining *horizontal* space in flexbox

htmlcssflexbox

提问by Adam Benson

I have 2 divs side-by-side in a flexbox. The right hand one should always be the same width, and I want the left hand one to just grab the remaining space. But it won't unless I specifically set its width.

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

So at the moment, it's set to 96% which looks OK until you really squash the screen - then the right hand div gets a bit starved of the space it needs.

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

I guess I could leave it as it is but it feels wrong - like there has to be a way to say:

我想我可以保持原样,但感觉不对 - 就像必须有一种说法:

the right one is always the same; you on the left - you get everything that's left

正确的永远是一样的;你在左边 - 你得到了剩下的一切

.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>

回答by Michael Benjamin

Use the flex-growproperty to make a flex item consume free space on the main axis.

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

This property will expand the item as much as possible, adjusting the length to dynamic environments, such as screen re-sizing or the addition / removal of other items.

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

A common example is flex-grow: 1or, using the shorthand property, flex: 1.

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

Hence, instead of width: 96%on your div, use flex: 1.

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



You wrote:

你写了:

So at the moment, it's set to 96% which looks OK until you really squash the screen - then the right hand div gets a bit starved of the space it needs.

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

The squashing of the fixed-width div is related to another flex property: flex-shrink

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

By default, flex items are set to flex-shrink: 1which enables them to shrink in order to prevent overflow of the container.

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

To disable this feature use flex-shrink: 0.

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

For more details see The flex-shrinkfactorsection in the answer here:

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



Learn more about flex alignment along the main axishere:

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

Learn more about flex alignment along the cross axishere:

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

回答by Brett84c

Basically I was trying to get my code to have a middle section on a 'row' to auto-adjust to the content on both sides (in my case, a dotted line separator). Like @Michael_B suggested, the key is using display:flexon the row container and at least making sure your middle container on the row has a flex-growvalue of at least 1 higher than the outer containers (if outer containers don't have any flex-growproperties applied, middle container only needs 1 for flex-grow).

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

Here's a pic of what I was trying to do and sample code for how I solved it.

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

Edit: Dotted bottom border will probably look weird depending on how your browser displays it. I'd personally recommend using a black circle SVG as a repeated background image, properly sized and positioned to the bottom of the middle container. Will add this alternative solution when I get time.

编辑:虚线底部边框可能看起来很奇怪,具体取决于浏览器的显示方式。我个人建议使用黑色圆圈 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>