In Flex layout, we know that the Flex-basis property is used to set the initial main size of a Flex project.
Same effect
But in practice, using the Flex-basis and width attributes on Flex projects gives the same results.
Take the following structure for example:
<div style="display: flex;">
<div class="item">Item 1</div>
<div class="item">Item 2 is a bit too much</div>
<div class="item">Item 3</div>
</div>
Copy the code
- Equal-width column: in
.item
Set up theflex-basis: 0
orwidth: 0
You can get this effect.
.item { flex-basis:0; /* The same effect as setting width: 0 */ flex-grow: 1; }Copy the code
- The width is fixed on both sides and the remaining space is occupied in the middle.
.item:first-child..item:last-child { flex-basis: 100px; /* The same effect as setting width: 100px */ }
.item:nth-child(2) { flex-grow: 1; }Copy the code
Observation shows that both layouts give the same effect whether you use Flex-basis or Width on a Flex project.
What’s the difference?
In fact, the width is not enough. Why design one more Flex-basis property?
Well, there’s a difference:
Note: For the sake of description, assume that the current document is typeset left to right, top to bottom, in horizontal writing mode.
- Spindle size: The first thing to be clear is,
flex-basis
Set the Flex project spindle size whenflex-direction
The valuescolumn
或column-reverse
When,flex-basis
It’s notwidth
, butheight
. - Supported abbreviation: Passed
flex
Shorthand properties, we can set bothflex-grow
,flex-shrink
,flex-basis
These three properties, if usedwidth
, had to be set separately. - Scope of use:
flex-basis
Only inFlex
Works on the project, not yet on the located Flex project. - Priority level: When set on a Flex project
flex-basis
和width
/height
, the effect will beflex-basis
.
Auto and content
The default value for flex-basis is auto, which is only slightly different from the other valuable content.
When flex-basis: auto, the current Flex project is checked to see if the width/height attribute is set. If not, it will revert to Flex-basis: content.
The size of the flex project is no larger than max-width/height and no smaller than min-width/height.
Flex-basis: content equals Flex-basis: auto; width/height: auto; In my opinion, this value does not seem to have any practical significance. It is a bit weak.
To summarize
- In horizontal writing,
flex-basis
Analysis effect andwidth
Exactly the same (providedflex-basis
The value of theta does not equalauto
或content
). flex-basis
Only true (not absolutely located)The Flex projectTo take effectflex-basis
Set up theThe spindle size, according to theflex-direction
Different values of, may representwidth
Can also meanheight
flex-basis
Support for shorthand propertiesflex
flex-basis
与width
/height
When used together,Higher priority
Refer to the link
- The flex – basis, MDN
- Flex Basis Property in Flexbox
- What are the differences between flex-basis and width?
(End of text)
Advertising time (long term)
I have a good friend who owns a cattery, and I’m here to promote it for her. Now the cattery is full of Muppets. If you are also a cat lover and have the need, scan her FREE fish QR code. It doesn’t matter if you don’t buy it. You can just look at it.
(after)