在CSS3中,具有flex-grow 0.1的flex项目是否应占用所有额外空间或仅占用10%?

huangapple go评论83阅读模式
英文:

In CSS3, should a flex item with flex-grow 0.1 be taking all the extra space or just 10%?

问题

以下第三个弹性项目具有非零值的flex-grow,即0.1。这可能是合理的,我也读到一个建议,它应该占据任何额外空间的100%,因为其他弹性项目的值都是0,不能增长。这一个是0.1,甚至0.1 / 0.1等于100%(作为比例)。然而,在Chrome和Firefox上实际情况是它只占据了额外空间的10%。为什么以及应该如何表现?

#container {
  display: flex;
  border: 1px dashed blue
}

#container div {
  border: 1px dashed orange
}

#container div:last-child {
  flex-grow: 0.1;
  background: #ccc
}
<div id="container">
  <div>1</div>
  <div>2</div>
  <div>3</div>
</div>
英文:

The following third flex item has a flex-grow of non-zero value, which is 0.1. It might be reasonable and I also read a suggestion that it should take up 100% of any extra space, because the other flex items are 0, and cannot grow. This one is 0.1 and even 0.1 / 0.1 is 100% (as a ratio). However, in practice on Chrome and Firefox it only took 10% of the extra space. Why and how should it behave?

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#container {
  display: flex;
  border: 1px dashed blue
}

#container div {
  border: 1px dashed orange
}

#container div:last-child {
  flex-grow: 0.1;
  background: #ccc
}

<!-- language: lang-html -->

&lt;div id=&quot;container&quot;&gt;
  &lt;div&gt;1&lt;/div&gt;
  &lt;div&gt;2&lt;/div&gt;
  &lt;div&gt;3&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

答案1

得分: 6

然而,在Chrome和Firefox上的实际情况是,只占用额外空间的10%。

这是正确的行为。

找到项目的flex grow因子与该行上所有未冻结项目的flex grow因子总和的比例。将项目的目标主要大小设置为其flex基础大小加上与比例成比例的剩余自由空间的一部分。flexbox-ref

从上面可以看出,flex-grow的值实际上表示了剩余空间的百分比,而flex-grow: 1表示剩余空间的100%。此外,添加flex-grow后的大小由以下等式导出。

flex项目的基础大小 + (剩余自由空间 * (flex项目的flex因子 / 未冻结flex项目的flex因子))

如果在问题的背景下指定flex-grow为0.1,则剩余自由空间将是初始自由空间的0.1倍:

如果未冻结的flex项目的flex因子总和小于1,请将初始自由空间乘以这个总和。如果这个值的大小小于剩余自由空间的大小,请将其用作剩余自由空间。flexbox-ref

因此,使用上述公式,可以如下导出第三个flex项目的大小:

flex项目的基础大小 + (初始自由空间 * 0.1 * (0.1 / 0.1))

= flex项目的基础大小 + (初始自由空间 * 0.1)

因此,flex-grow: 0.1的结果是初始剩余自由空间的10%。

英文:

> However, in practice on Chrome and Firefox it only took 10% of the extra space.

This is correct behaviour.

> Find the ratio of the item’s flex grow factor to the sum of the flex grow factors of all unfrozen items on the line. Set the item’s target main size to its flex base size plus a fraction of the remaining free space proportional to the ratio. <sup>flexbox-ref</sup>

From the above, the flex-grow value effectively indicates the percentage of the free space, and flex-grow: 1 represents 100% of the free space. Also, the size after flex-grow is added is derived by such an equation.

> flex item's base size + (remaining free space * (flex item's flex factors / unfrozen flex item's flex factors))

If you specify 0.1 for flex-grow in the context of the question, remaining free space will be 0.1 times the initial free space:

> If the sum of the unfrozen flex items’ flex factors is less than one, multiply the initial free space by this sum. If the magnitude of this value is less than the magnitude of the remaining free space, use this as the remaining free space. <sup>flexbox-ref</sup>

So, using the above formula, the size of the third flex item can be derived as follows:

> flex item's base size + (initial free space * 0.1 * (0.1 / 0.1))
>
> = flex item's base size + (initial free space * 0.1)

So the result of flex-grow: 0.1 is 10% of the initial remaining free space.

答案2

得分: 1

规范中可以看到:

在0和1之间的Flex值具有一种特殊行为:当行上的Flex值总和小于1时,它们将占据不到100%的剩余空间

您可以继续阅读本节的详细信息,您将理解为什么您的0.1实际上是剩余空间的10%

重要的部分:

这种模式是在flex-grow接近零时需要的连续行为(这意味着该项不希望使用剩余空间)。如果没有这个,flex-grow: 1的项会占用所有剩余空间;但是flex-grow: 0.1的项,以及flex-grow: 0.01的项等,也会占用所有剩余空间,直到最终值足够小以至于下溢为零,该项突然不再占用剩余空间。

英文:

From the specification you can read:

>Flex values between 0 and 1 have a somewhat special behavior: when the sum of the flex values on the line is less than 1, they will take up less than 100% of the free space.

You can continue to read the detail of this section and you will understand why your 0.1 is actually 10% of the free space.

The important part:

>This pattern is required for continuous behavior as flex-grow approaches zero (which means the item wants none of the free space). Without this, a flex-grow: 1 item would take all of the free space; but so would a flex-grow: 0.1 item, and a flex-grow: 0.01 item, etc., until finally the value is small enough to underflow to zero and the item suddenly takes up none of the free space

答案3

得分: 0

最小值为1,作为flex-grow的值。如果您添加1,它将占据剩余的空间。flex-grow的默认值为0,这就是为什么您的前两个div只占据其值的空间的原因。如果您将第一个divflex-grow设置为1,您将注意到它的大小与最后一个相同。另一方面,如果您将其设置为flex-grow: 2;,它将比其他两个大,但不会是其可能的大小的两倍。在实际项目中,我很少使用除了flex-grow: 1;之外的任何东西,如果我想要某物是两倍大或类似的东西,我会使用flex-basiswidth。希望这有所帮助。

检查我的片段。如果您希望它占用特定百分比的空间,则使用flex-basiswidth属性。

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->
#container {
  display: flex;
  border: 1px dashed blue
}

#container div {
  border: 1px dashed orange
}

.remaining-space {
  flex-grow: 1;
  background-color: deepskyblue;
}

<!-- language: lang-html -->
<div id="container">
  <div>1</div>
  <div>2</div>
  <div class="remaining-space">3</div>
</div>

<!-- end snippet -->
英文:

The minimum is 1 as value for flex-grow. If you add 1 it will take upp the remaining of the space there. Default value is 0 for flex-grow, that is why your two first divs only take up the space that their value do. You will notice that it will behave different using flex-grow, if you take your first div and add flex-grow: 1; it will be the size as your last one. If you on the other hand take flex-grow: 2; it will be larger then the other two, but not twice as large as it could be interrupted it to be. In real projects I rarely use anything but flex-grow: 1;, if I want something to be twice as large or anything along those lines I use flex-basis or width. Hope this helps.

Check my snippet. If you want it to take up a scpecific percentage then use flex-basis or width properties.

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-css -->

#container {
  display: flex;
  border: 1px dashed blue
}

#container div {
  border: 1px dashed orange
}

.remaining-space {
  flex-grow: 1;
  background-color: deepskyblue;
}

<!-- language: lang-html -->

&lt;div id=&quot;container&quot;&gt;
  &lt;div&gt;1&lt;/div&gt;
  &lt;div&gt;2&lt;/div&gt;
  &lt;div class=&quot;remaining-space&quot;&gt;3&lt;/div&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2020年1月3日 14:27:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/59574099.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定