Understand Flexbox elastic box and flexbox elastic box
Bytes
1: to start using Flexbox, you must first turn the parent element into a Flex container..
You can explicitly set in the parent Elementdisplay:flex
Ordisplay:inline-flex
. That's simple, so you can start using the Flexbox module.
2. Flex container Properties
flex-direction || flex-wrap || flex-flow || justify-content || align-items || align-content
Flex-direction: Row | column | row-reverse | column-reverse;
Flex-wrap: Wrap | nowrap | wrap-reverse;
Flex-flow:flex-flow
Yesflex-direction
Andflex-wrap
Stenographer attributes of two attributes
You still remember to useborder
?border: 1px solid red
. The concept here is the same, and multiple values are written in the same row, for example, the following example:
ul { flex-flow: row wrap;}
justify-content
The property can accept one of the following five values:
ul { justify-content: flex-start || flex-end || center || space-between || space-around}
align-items
Properties are similarjustify-content
Attribute. Only understoodjustify-content
Attribute to better understand this attribute.
align-items
Attributes can accept these attribute values:flex-start || flex-end || center || stretch || baseline
.
ul { align-items: flex-start || flex-end || center || stretch || baseline}
It is mainly used to control the Flex project inCross-AxisAlignment. This is alsoalign-items
Andjustify-content
The difference between two attributes.
Align-content
Remember what we discussed earlierwrap
Attribute? We have added more Flex projects to the Flex container. Arrange Flex projects in the Flex container in multiple rows.
align-content
Attribute is used for multi-row Flex containers. It is also used to control the arrangement, effect andalign-items
The value is the same,baseline
Attribute Value.
Imagealign-items
The property is the same, and its default value isstretch
. You should be familiar with these values now. Then how does it affect the multi-row arrangement of 10 Flex projects in the Flex container.
3. Flex Project Properties
order || flex-grow || flex-shrink || flex-basis
Flex-basis
As I said before, I did not have a Flex project. But we can also control it.
flex-basis
Attribute can specify the initial size of the Flex project. That isflex-grow
Andflex-shrink
Property to adapt to the Flex container.
As we have mentioned above, we need a little time to enhance our understanding of them.
flex-basis
The default value isauto
.flex-basis
Anywidth
Any value of the property. For example% || em || rem || px
.
Note:: Ifflex-basis
The attribute value is0
The Unit is also required. That isflex-basis: 0px
Cannot be writtenflex-basis:0
.
By default, the initial width of a Flex project is determinedflex-basis
The default value is:flex-basis: auto
. The Flex project width is calculated automatically based on the amount of content (obviouslypadding
Value ).
Flex stenographer
flex
Yesflex-grow
,flex-shrink
Andflex-basis
Shorthand for the three attributes ).
Pay attention to the order between them.flex-grow
First, thenflex-shrink
And finallyflex-basis
. AbbreviatedGSBTo help you better remember.
Ifflex
What happens if there is one less value in the attribute value?
If you only setflex-grow
Andflex-shrink
Value,flex-basis
It may be the default value.0
. This is the so-called absolute flex project. Only when you setflex-basis
, You will get a relatively flex project.
Flex: 0 1 auto
li { flex: 0 1 auto;}
This is equivalent to writingflex
Default attribute values and all Flex projects are default actions.
Flex: 0 0 auto
li { flex: 0 0 auto;}
This is equivalentflex: none
.
Still old rule: the width is automatically calculated, but the elastic project will not stretch or contract (because both are set to zero ). Both the stretch and shrink switches are turned off.
It is basically a fixed-width element, and its initial width is based on the content size in the elastic project.
Flex: 1 auto
This correspondsflex: auto
The project is the same.
Or follow the rules I set above. That is,Automatically calculates the initialization width, but expands or shrinks to fit the entire available width if necessary.
The stretch and shrink switch is turned on, and the width is automatically calculated.
Flex: "positive number"
A positive number can represent any positive number (without quotation marks ). This correspondsFlex: "positive" 1 0
Same.
flex: 2 1 0
And writeflex: 2
Is the same,2
Represents any positive number.
Li {flex: 2 1 0;/* same as flex: 2 */}
Like the rules I set above, that is,Set the initial width of the elastic project to zero (um? No width ?), Stretch the project to fill up the available space and contract the project as soon as possible.
If there is no width for an elastic project, how can we calculate the width?
This timeflex-grow
The value takes effect, and it determines the degree to which the elastic project becomes wider. It is responsible for the problem of no width.
When there are multiple elastic projects and their initial widthflex-basis
When it is set to any zero-based value, for example0px
, Use thisflex
Abbreviations are more practical.
Actually, the width of the elastic project is based onflex-grow
Value ratio.
Remember settingsflex-grow : 1
The elastic project will fill up the available space. The stretch switch is enabled.
There are two elastic projects. Oneflex-grow
The property value is1
And the other is2
So what will happen?
The stretch switch is enabled for both projects. However, the stretching degree is different,1
And2
.
Both of them will fill up the available space, but it is proportional.
It works like this: the previous one occupies1/3
Available space, the latter occupies2/3
.
Absolute and relative Flex Projects
The spacing of a Flex project is calculated based on its content size. In the absolute Flex projectflex
Attribute, rather than content.
The absolute Flex project width is only based onflex
Property, while the width of the Flex project is based on the content size..
Align-self
align-self
Attributes further allow us to better control elastic projects.
You have seenalign-items
How attributes help align all the elastic projects in the elastic container as a whole.
What should I do if I want to change the position of an Elastic project along the side axis without affecting adjacent elastic projects?
This isalign-self
The attribute has been highlighted.
The value of this attribute can be one of these values:auto || flex-start || flex-end || center || baseline || stretch
.
Auto-margin alignment
When used in a Flex Projectmargin: auto
The value isauto
(Left, right, or both) will occupy all the remaining space.
Solve the Problem of space usage ratio of the nested flex container
There must be a way to put all of these in one row, and each paragraph occupies equal space for this row. Use Flexbox! The concept here is the same as that used in many raster systems.
Li {display: flex;/* Section is now displayed on a row */padding: 0 20px;/* Reserved breathing space */min-height: 50px;} li p {flex: 0 0 25%;/* This is sweet Noodle Sauce */}
What will happen to the section?
flex: 0 0 25%;
"Do not stretch or contract, but each paragraph should occupy25%
Available space". The space occupied by paragraphs is equal.