① What is the difference between flex setting 1 and auto,
② If it is set to 1, the basis is 0%, how to understand it,
③ Set to auto. According to the explanation, the basis value should be auto. What does this mean,
④ What’s more, if you set it to AUO, add the prefix – WebKit box with autoprefix, and the corresponding value is 1?
I’m here to thank Zan
If there are any mistakes in the following personal tests, please correct them:
First, let’s be clear,flex
yesflex-grow
、flex-shrink
、flex-basis
Abbreviation for. Therefore, the following conditions can be considered for its value:
flex
The default value of is a combination of the above three property values. Assuming that the above three attributes also take default values, thenflex
The default value of is 0 1 auto. Similarly, the following are equivalent:
.item {flex: 2333 3222 234px;}
.item {
flex-grow: 2333;
flex-shrink: 3222;
flex-basis: 234px;
}
Whenflex
The value isnone
, then the calculated value is 0.00 auto, which is equivalent as follows:
.item {flex: none;}
.item {
flex-grow: 0;
flex-shrink: 0;
flex-basis: auto;
}
Whenflex
The value isauto
Then the calculated value is 1 1 auto, which is equivalent as follows:
.item {flex: auto;}
.item {
flex-grow: 1;
flex-shrink: 1;
flex-basis: auto;
}
Whenflex
If the value is a non negative number, the number isflex-grow
Value,flex-shrink
Take 1,flex-basis
Take 0% as follows:
.item {flex: 1;}
.item {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 0%;
}
Whenflex
If the value is a length or percentage, it is regarded asflex-basis
Value,flex-grow
Take 1,flex-shrink
Take 1 as follows (note that 0% is a percentage rather than a non negative number)
.item-1 {flex: 0%;}
.item-1 {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 0%;
}
.item-2 {flex: 24px;}
.item-1 {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 24px;
}
Whenflex
If the values are two non negative numbers, they are regarded asflex-grow
andflex-shrink
Value of,flex-basis
Take 0% as follows:
.item {flex: 2 3;}
.item {
flex-grow: 2;
flex-shrink: 3;
flex-basis: 0%;
}
Whenflex
Values are a non negative number and a length or percentage, respectivelyflex-grow
andflex-basis
Value of,flex-shrink
Take 1 as follows:
.item {flex: 2333 3222px;}
.item {
flex-grow: 2333;
flex-shrink: 1;
flex-basis: 3222px;
}
flex-basis
It specifies the base value of the child element. Therefore, the calculation of overflow is closely related to this property.flex-basis
The scope of the regulation depends onbox-sizing
。 Here we mainly discuss the followingflex-basis
The value of:
auto
: first retrieve the major dimension of the child element, if it is notauto
, then use the value to take the value of the main dimension; if it is alsoauto
, the value is usedcontent
。content
: refers to the automatic layout according to the content of the child element. Some user agents do not implement fetchingcontent
The equivalent alternative isflex-basis
And the main sizeauto
。- Percentage: calculated based on the main size of its containing block (i.e., the telescopic parent container). If the primary size of the containing block is undefined (that is, the primary size of the parent container depends on the child elements), the calculation result and are set to
auto
The same.
Take a difference between different values:
<div class="parent">
<div class="item-1"></div>
<div class="item-2"></div>
<div class="item-3"></div>
</div>
<style type="text/css">
.parent {
display: flex;
width: 600px;
}
.parent > div {
height: 100px;
}
.item-1 {
width: 140px;
flex: 2 1 0%;
background: blue;
}
.item-2 {
width: 100px;
flex: 2 1 auto;
background: darkblue;
}
.item-3 {
flex: 1 1 200px;
background: lightblue;
}
</style>
- The total size of the parent vessel on the spindle is 600px
-
The total reference value of sub elements is: 0% + auto + 200px = 300px, where
-0% means 0 width -Auto corresponding to the main size is 100px
- So the remaining space is 600px – 300px = 300px
- The sum of expansion and expansion coefficients is: 2 + 2 + 1 = 5
-
The remaining space is allocated as follows:
-Item-1 and item-2 were assigned 2 / 5 respectively, and each got 120px -1 / 5 of item-3 is assigned to get 60px
-
The final width of each project is:
- item-1 = 0% + 120px = 120px - item-2 = auto + 120px = 220px - item-3 = 200px + 60px = 260px
- When the reference value of item-1 is 0%, the item is regarded as zero size. Therefore, it is useless to declare its size as 140px
- The reference value of item-2 is
auto
According to the rule, the reference value is the main dimension value, i.e. 100px, so the 100px will not be included in the remaining space
For more information, please refer to:
http://www.w3.org/html/ig/zh/css-flex-1/
Thank you!
Hello, may I ask your exampleThe main dimension is 100px
How is it calculated?
Oh, my God. It’s so clear
.x{
flex:1 1 auto;
}
After autoprefix
.x{
-webkit-box-flex:1;
-webkit-flex:1 1 auto;
-ms-flex:1 1 auto;
flex:1 1 auto;
}
So the old syntax doesn’t support auto, right? So what if you want to initially layout with the width of child elements and recalculate according to the remaining or overflow space? @HaoyCn
How does wechat explain flex box