preface

The Flex layout is very common in front end development. Although it has been many years since its inception, there are many people who have a superficial understanding of the Flex layout, such as using Flex: 1 or Flex: Auto and so on, causing some style problems or browser compatibility problems, immediately cannot be dealt with.

Therefore, I have compiled an article from two aspects of basic flex layout knowledge and browser compatibility, combined with my personal work experience, hoping to be helpful to you. If there is anything wrong, please kindly advise me!

The article is roughly divided into two parts, the first piece is the basic knowledge of Flex, including layout features, attributes, etc., relatively theoretical. The second part is a summary of the browser compatibility problems of Flex, as well as suggestions on how to use Flex. This is derived from the actual problems encountered in personal work scenarios. After trying to solve the summary, it is a bit practical application.

For those of you who are familiar with flex layout but have browser compatibility issues, you can jump right to the second piece.

Flex layout – One-dimensional layout

Flexbox is a one-dimensional layout, because a flexbox can only handle the layout of elements on one dimension at a time, one row or one column

Flex layout – Container properties

display: flex vs display: inline-flex

The same

  • For the child elements inside the container, there is no difference

differences

  • For the container itself,flexRepresented as block-level elements;inline-flexRepresents a block inline element
    • So when the container is not set to a width,flexThe default container width is 100% (one line is full);inline-flexThe default width is adaptive to the width of the child element

flex-direction

Axis – Spindle & cross axis

  • The spindle consists offlex-directionDefinition, desirablerow(Transverse spindle),column(longitudinal spindle) equivalent.
  • The cross axis is the axis perpendicular to the main axis.
  • flexboxThe property is to align elements along the main or cross axes.



Start line & end line

Start line and end line are used for descriptionflexboxThe writing (arrangement) direction of the child elements.

When the spindle is ROW (horizontal spindle) :

  • When writing English, the starting line of the main axis is on the left; When writing Arabic, the starting line of the main axis is to the right.
  • The intersecting axis starts with aflexboxThe top, the terminating line is the bottom, because both languages are horizontal writing patterns.





When the main axis is column (longitudinal axis) :

  • The starting line of the spindle isflexboxAnd the terminating line isflexboxBecause both languages are written horizontally.
  • When writing English, the starting line of the crossing axis is on the left. When writing Arabic, the starting line of the cross axis is to the right.

The relationship between property values and axis, start and end lines

Assuming that row is the benchmark, the line bit changes for the other attributes are as follows:

  • Row-reverse: spindle & cross axis unchanged; Start line & end line changed
    • The starting line and ending line of the spindle are exchanged; The starting line and ending line of the intersecting axis remain unchanged
  • Column: spindle and cross axis swap; Start line & end line changed
    • The starting terminating line of the principal axis is interchanged with the starting terminating line of the intersecting axis
  • Column-reverse: the main axis and cross axis are reversed; Start line & end line changed
    • The starting terminating line of the principal axis is interchanged with the starting terminating line of the intersecting axis
    • The starting line and ending line of the crossing axis are interchanged; The starting line and ending line of the spindle remain unchanged

justify-content

Aligns the child elements on the main axis

  • The initial value isflex-start, the child elements are arranged from the starting line of the container.
  • flex-endElements are arranged from the container terminator line
  • center, centered, with each element close to each other
  • space-between, fill the container, and the elements are equally spaced
  • space-around, fill the container, and the left and right sides of each element are equal

align-items

Aligns the child elements on the cross axis

  • The initial value isstretch
    • [Fixed] The container height is not defined. The container will be stretched to the height of the highest element.
    • Defines the container height to which elements are stretched (contracted).
  • flex-startAlign with the starting line of the container
  • flex-endAlign the container’s stop line
  • center, center aligned

align-content

Aligns the child elements on the cross axis, but on multiple lines

  • The default value isstretchThe remaining space is bisected by all rows to enlarge their cross axis size
  • flex-start, all rows are arranged from the starting line of the container
  • flex-end, all lines are arranged from the container terminating line
  • center, all the rows are in the middle of the container, in a compact arrangement
  • space-between, all rows fill the container and are equally spaced
  • space-around, all rows fill the container, and the space above and below each row is equal

flex-wrap

Line feed way

  • The default is NowRap, with no line breaks
  • Wrap, and line breaks
  • Wrap-reverse, a line feed is arranged in reverse

flex-flow

This is a combination of flex-direction and flex-wrap

Flex layout – Child element properties

Child element default style

  • Flex-direction: row, where the elements are arranged in a single line starting at the beginning of the main axis
  • Flex: 0 1 auto, elements do not stretch in the main axis but shrink with available space
  • Flex-wrap: nowrap, if there are too many elements out of the container, they overflow without line wrapping
  • If some elements are taller than others, the element is stretched along the cross axis to fill its size

flex-basis

The initial (base) size of the element

  • The default value isauto
    • Element sets the width,flex-basisIs the set width
    • Element has no width set,flex-basisIs the size of the element content
  • flex-basisAttributes take precedence overwidthProperties;
  • Set to 0, the size of the child element is not considered in the space allocation calculation

flex-grow

The expansion of the element along the main axis takes up the space available on the main axis

  • Space is allocated proportionally (default is 0), expansion value isflex-basisThe benchmark is multipliedflex-growExpansion of the scale
    • If you set the same value, the available space is equally divided
    • Set different values to divide the available space proportionally

The following figure shows differentflex-basisIn the case of,flex-growScenario where both sets to 1:

flex-shrink

The shrinkage of an element along the principal axis takes effect only if the sum of the child elements exceeds the principal axis

  • Allocate space proportionally (default is 1), shrink value isflex-basisThe benchmark is multipliedflex-shrinkShrinkage ratio
  • As the box gets smaller and smaller, the smaller child elements end up withmin-contentThe following space will always be removed from the large child element
    • The so-calledmin-content, the width of the smallest non-breakable element inside the current container
      • Below largeSizeContentWithoutWrap is continuous not to line breaks, so cannot be compressed space
      • Middle-size-content-width-split-code can be broken with dashes, so newlines can be used to compress space
      • Content-content-content-content has been broken to an unbreakable line width, so no line breaks can be continued to compress space

The following figure shows differentflex-basisIn the case of,flex-shrinkScenario where both sets to 1:

order

Order of items

  • The smaller the value is, the more advanced it is. The default value is 0

align-self

Allow individual items to have different alignments than other items

  • coveringalign-itemsProperty, the default value isauto
  • Represents inheriting from the parent elementalign-itemsattribute
    • Equals if there is no parent elementstretch

A few Flex-style abbreviations

flex: initial

Equivalent to Flex: 0 1 Auto

flex: auto

Equivalent to Flex: 1 1 Auto

flex: none

Equivalent to Flex: 0 0 auto

flex: 1

Equivalent to Flex: 1 1 0

Flex: Auto vs. Flex: 1

flex: 1 1 auto

  • The available space is allocated evenly based on the initial width of each element
  • Elements are of unequal width, and those with larger initial widths will have larger allocated widths

flex: 1 1 0

  • The available space is allocated evenly based on the 0 width of each element
  • The elements are of equal width

Flex Layout – Browser compatibility

Browser Compatibility Overview

  • Red indicates that the Flex specification is not supported
  • In yellow, only the old version 09 Flex specification is supported (display: box;).
  • Green is full support for the new version 12 flex specification (display: flex;)

Flex specification, 09 vs 12





Solutions that support a few older browsers

  • Work with the Flex layout that supports both version 09 and 12
.container {
	display: box; 
	display: flex; 
}
Copy the code

Pay attention to
  • Browser-compatible syntax (-webkit-, etc.) can be implemented with tools like PostCSS
  • But Flex 09 doesn’t support many features

Compatibility issues with Chrome 49

Problem description

The parent elementflex: 1The child elementsheight: 100%Cannot fill the parent element because there is too little content

why

The parent element does not set height, so the child element does not get the height of the parent element

Solution 1

Absolute, relative, the height of the element is calculated according to the height of the parent element

explain

As mentioned in the specification, if the height of the containing block is not explicitly specified (the height is determined by the content), and the element is not absolutely positioned, then the computed value is auto, and the height and percentage values cannot be computed! auto * 100/100 = NaN

defects

Effects of absolute, such as setting width to 100%

Solution 2

Instead of using height: 100%, the child element uses flex-grow to fill up the space

Solution 3

Instead of using flex: 1, the parent element uses display: flex; + height: 100%;

explain

Applied to thedisplay: flexTo make it a Flex container.

This will automatically setalign-items: stretch, tells child (.item) the full height of the extension parent.

conclusion

Chrome49 is advised to use display: flex for the parent element if the child element is set to height: 100%. + height: 100%;

Chrome 79 + compatibility issues

Problem description

When the parent element is setflex: 1Fill container, child element setheight: 100%After that, the child element has more content than the parent element

The solution

For an element with Flex: 1 set min-height: 0 to keep the contents within the outer container

explain

Parent element Settingsmin-height: 0This is equivalent to telling the child the parentheight > 0The child element can then indirectly get the height of the parent element and set itheight: 100%Keep the parent element at the same height to avoid overflows

Pay attention to

To be compatible with Chrome49, use display: flex; + height: 100%; Replace the flex: 1

conclusion

Height: 100% for the child element in Chrome79 or above, it is recommended that the parent element use min-height: 0. (Chrome 79) + display: flex; + height: 100%; (49) Chrome

Chrome 49 differs from Chrome 79 and above in another way

Differences between the description

  • Flexbox one child sets Flex and the other sets a high height
  • When the Flexbox height is insufficient to accommodate both, both heights are compressed
  • But setting up flex’s child elements on chrome49 will compress them so that even the child elements can’t be fully displayed
  • On chrome79, flex’s child elements will be displayed as much as possible

chrome 49



Chrome above 79

The solution

To maintain the complete display of flex child elements, it is best to set Flex-shrink: 0 when you set flex;

QQ browser 10 under Flex: auto does not take effect compatibility issues

Problem description

QQ Browserchromium70, parent element flex: auto, child element height: 100%, cannot expand or shrink child element

The solution

Change flex: auto to flex: 1 with min-height: 0

Flex layout usage recommendations

For horizontal layout

Just write it in general

.container {
	display: flex;
	flex-direction: row;
}
Copy the code

For vertical layout

Need to add compatible chrome49 and chrome79 styles

.container {
	display: flex;
	flex-direction: column;
	height: 100%;
	min-height: 0;
}
Copy the code

For child elements

Be flexible with a combination of flex-basis, flex-grow, and flex-shrink as you see fit, rather than using flex:1 or flex: Auto all the time

The last

Attached is the git address of the case project:

Github.com/hezhikai/bl… (The case may not be well written -0-)

Reiterated some

  • The basics of Flex layout above are summarized based on many documents I have read. Some descriptions may contain words or fragments from articles in other places. Please let me know if there are any infringements and I will delete them.
    • Because I checked a lot of materials when I sorted out, NOW I can’t remember where I referred to the articles in many places, so it is not good to attach reference materials, please forgive me.
  • Compatibility issues with the flex layout are more likely to be addressed based on working scenarios. Of course, there are also various information (although the information on the Internet is not detailed enough), but there is also a summary of personal experience in it.
    • So if you run into any other Flex browser compatibility issues later on, this will be added.
  • Although this article can not be regarded as completely original, but after all, it is also personal spent time and energy to sort out, based on the actual work scene to try and summarize, so if you need to reprint, please attach this source ha!
    • It is also some recognition of the personal summary