简体   繁体   English

什么是flex-basis属性设置的?

[英]What exactly flex-basis property sets?

Is there a difference between setting max-width or width to a flex item instead of the flex-basis ? max-widthwidth设置为弹性项而不是flex-basis之间是否存在差异?

Is it the "breaking point" for the flex-grow/shrink properties? 它是flex-grow/shrink特性的“突破点”吗?

And when I set flex-wrap: wrap how does the browser decides at which point to move down item to new line? 当我设置flex-wrap: wrap浏览器如何决定将项目向下移动到新行? Is it according to their width or 'flex-basis'? 是根据他们的宽度还是'弹性基础'?

Example: http://jsfiddle.net/wP5UP/ The last two boxes have the same flex-basis: 200px , yet only one of them moves down when the window is between 300px and 400px. 示例: http//jsfiddle.net/wP5UP/最后两个框具有相同的flex-basis: 200px ,但当窗口介于300px和400px之间时,其中只有一个向下移动。 Why? 为什么?

flex-basis allows you to specify the initial/starting size of the element, before anything else is computed. flex-basis允许您在计算任何其他内容之前指定元素的初始/起始大小。 It can either be a percentage or an absolute value. 它可以是百分比或绝对值。

It is, however, not the breaking point for flex-grow/shrink properties. 然而,它不是柔性生长/收缩特性的突破点。 The browser determines when to wrap the element on the basis of if the initial sizes of elements exceed the width of the cross-axis (in conventional sense, that is the width). 浏览器根据元素的初始大小是否超过横轴的宽度(在传统意义上,即宽度)确定何时包装元素。

Based on your fiddle, the reason why the last one moves down the window is because the width of the parent has been fully occupied by the previous siblings — and when you allow content to wrap, the elements that fail to fit in the first row gets pushed to the subsequent row. 基于你的小提琴,最后一个向下移动窗口的原因是因为父级的宽度已被前一个兄弟姐妹完全占用 - 当你允许内容换行时,不能适应第一行的元素得到推到后续行。 Since flex-grow is a non-zero value, it will simply stretch to fill all spaces left in the second row. 由于flex-grow是一个非零值,它将简单地拉伸以填充第二行中剩余的所有空格。

See demo fiddle (modified from yours) . 请参阅演示小提琴(由您修改)

If you look at the fiddle, I have modified for the last item to have a new size declaration: 如果你看一下这个小提琴,我已经修改了最后一个项目以获得一个新的大小声明:

.size3 {
  flex: 0 1 300px;
}

You will realize that the element measures 300px across as intended. 您将意识到该元素按预期测量300px。 However, when you tweak the flex-grow property such that its value exceeds 0 ( see example ), it will stretch to fill the row, which is the expected behavior. 但是,当您调整flex-grow属性使其值超过0( 参见示例 )时,它将拉伸以填充行,这是预期的行为。 Since in its new row context it has no siblings to compare to, an integer between 1 to infinity will not influence it's size. 因为在它的新行上下文中它没有可比较的兄弟,所以1到无穷大之间的整数不会影响它的大小。

Therefore, flex-grow can be seen as this: 因此, flex-grow可以看作:

  • 0 : (Default value) Do not stretch . 0 :(默认值) 不拉伸 Either size to element's content width, or obey flex-basis . 大小到元素的内容宽度,或遵循flex-basis
  • 1 : Stretch . 1伸展
  • ≥2 (integer n ): Stretch . ≥2 (整数n ): 拉伸 Will be n times the size of other elements with flex-grow: 1 on the same row, for example. 例如,将使用flex-grow: 1 n倍于其他元素的大小flex-grow: 1在同一行上。

Good article https://gedd.ski/post/the-difference-between-width-and-flex-basis/ 好文章https://gedd.ski/post/the-difference-between-width-and-flex-basis/

flex-basis is: the size of flex items before they are placed into a flex container. flex-basis是:flex项目放入Flex容器之前的大小。 It's the ideal or hypothetical size of the items. 这是物品的理想或假设尺寸。 But flex-basis is not a guaranteed size! 但是flex-basis不是保证大小! As soon as the browser places the items into their flex container, things change. 一旦浏览器将项目放入其Flex容器中,事情就会发生变化。 Often times the flex container won't have enough space, or will have extra space, after all its items' flex-basis values are added up. 通常,在将所有项目的弹性基础值相加之后,弹性容器将没有足够的空间,或者将具有额外的空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM