FLEXBOX-CSS3 Flexible Box Model Flexbox full version tutorial

Source: Internet
Author: User

Original Link: http://caibaojian.com/flexbox-guide.html

FLEXBOX-CSS3 Flexible Box Model Flexbox full version tutorialA-A+ Front-End Blogs2014-05-08• Front-End Development | Featured CSS3 | flexbox | flexible layout •16928View Article Directory

From a tutorial on CSS tricks, The original is: a complete guides to Flexbox. This paper introduces the relevant properties of the new elastic box in detail, and gives some examples of use. is an excellent tutorial for beginners to get started Flexbox. of course, Most of the content is written according to the Article. Each of these properties gives an example diagram, which is very easy to understand. I based on their ideas, combined with Chinese translation, wrote a complete tutorial flexbox, I hope it is useful to Everyone.

The flexible layout (flexible box) module (currently recommended by the Candidate) is designed to provide a more efficient way to lay out, align, and distribute the contents between containers, even if their size is unknown or dynamically changing.

The main idea of resilient layout is to allow the container to have the ability to change the width and height of the project to fill the available space (primarily to accommodate all types of display devices and screen sizes).

Most importantly, the elastic box layout is independent of orientation, relative to the regular layout (blocks are vertical and inline level based), It is clear that these work and Web page design is inflexible, unable to support large and complex applications (especially when it involves changing direction, scaling, stretching and shrinking, etc.).

Note: the Flexbox layout is the most suitable component for an application, as well as a small-scale layout, while a grid layout is used for larger Layouts.

Basic knowledge

Since Flexbox is a monolithic module, rather than a single attribute, it involves a lot of things, including its entire set of Properties. Some of them are set on the parent container, while others are set on the child Container.

If the regular layout is based on the block and inline flow direction, the flexible layout is based on the "flexible flow direction". Please take a look at this number from the specification, explaining the main idea behind the flexible Layout.

basically, the project will develop any of the following spindles (from Main-start to Main-end) or cross-axes (from Cross-start to cross-end).

Property Introduction Display:flex | inline-flex; (applies to the parent class container Element)

Defines a flex container, inline or based on a specified value, for the following subclass Container.

    • Box: displays the object as an elastic telescopic box. (the oldest version of the telescopic box) (css3)
    • Inline-box: displays the object as an inline block-level elastic expansion box. (the oldest version of the telescopic box) (CSS3)
    • Flexbox: displays the object as an elastic telescopic box. (telescopic box Transition Version) (CSS3)
    • Inline-flexbox: displays the object as an inline block-level elastic expansion box. (telescopic Box Transition Version) (CSS3)
    • Flex: displays the object as an elastic telescopic box. (latest version of Telescopic box) (CSS3)
    • Inline-flex: displays the object as an inline block-level elastic expansion box. (latest version of Telescopic box) (CSS3)
Please note:

1.css columns (css Columns) do not work in flexible boxes

2. float , clear and vertical-align does not work in flex projects

Flex-direction (applies to the element of the parent class Container)

Definition: Sets or retrieves the position of the child elements of a telescopic box object in the parent Container.

Flex-direction:row | Row-reverse | Column | Column-reverse
    • Row: horizontal left-to-right (left-aligned), arranged by Default.
    • Row-reverse: reverses the horizontal arrangement (right-aligned, from back to front, and the last item to the Front.)
    • Column: Vertical Arrangement.
    • Column-reverse: reverse the vertical arrangement, from the back to the front row, the last item on Top.

Flex takes effect to define its parent element display as Flex or Inline-flex (box or inline-box, which is the old Way)

Demo: flex-direction

Flex-wrap (applies to the parent class Container)

Sets or retrieves whether the child elements of a telescopic box object are wrapped when they are outside the parent Container.

Flex-wrap:nowrap | Wrap | Wrap-reverse
    • Nowrap: does not wrap when child elements overflow the parent container.
    • Wrap: wraps automatically when a child element overflows the parent Container.
    • Wrap-reverse: Reverse Wrap Arrangement.

Demo: Flex-wrap

Flex-flow (applies to the parent class Container)

Composite Properties. Sets or retrieves the arrangement of child elements of a telescopic box object.

Flex-flow: < ' flex-direction ' > | | < ' flex-wrap ' >
    • [flex-direction]: defines the direction in which the elastic box elements are Arranged.
    • [flex-wrap]: defines whether the elastic box element overflows the parent container when Wrapping.

Demo: Flex-flow

Justify-content (applies to the parent class Container)

Sets or retrieves the alignment of the elastic box element in the direction of the spindle (horizontal axis).

This property assists in allocating extra space when all child elements on a row in the elastic box cannot be scaled or have reached their maximum Value. When an element overflows a row, This property is also controlled on the Alignment.

Justify-content:flex-start | Flex-end | Center | Space-between | Space-around
    • Flex-start: The Elastic box element aligns to the starting position of the LINE. The boundary of the primary start position of the first child element of the row is aligned with the boundary of the primary start position of the row, and all subsequent telescopic box items are aligned with the previous Item.
    • Flex-end: The Elastic box element aligns to the end of the Line. The boundary of the primary end position of the first child element of the row is aligned with the boundary of the main end position of the row, and all subsequent telescopic box items are aligned with the previous Item.
    • Center: The Elastic box element aligns to the middle of the ROW. The child elements of the row are aligned to each other and centered in the row, while the first element is equal to the margin of the primary start position of the line with the margin of the last element and the primary end of the row (if the remaining space is negative, an overflow of equal lengths is maintained).
    • Space-between: The elastic box elements are evenly distributed in the ROW. If the left-most remaining space is a negative number, or if the row has only one child element, the value is equivalent to ' Flex-start '. In other cases, the boundary of the first element is aligned with the boundary of the primary start position of the row, while the last Element's boundary is aligned with the margin of the Row's main end position, while the remaining telescopic box items are evenly distributed and ensure that the white space between 22 is Equal.
    • Space-around: The elastic box elements are evenly distributed across rows, leaving half of the space between child elements and child Elements. This value is equivalent to ' center ' if the left-most remaining space is a negative number, or if the line has only one telescopic box Item. In other cases, the telescopic box items are evenly distributed and ensure that the space between 22 is equal, while the space before the first element and the last element are half as many other white space.

Demo: justify-content

Align-items (applies to the parent class Container)

Sets or retrieves the alignment of the elastic box element in the direction of the side axis (vertical axis).

Align-items:flex-start | Flex-end | Center | Baseline | Stretch
    • Flex-start: the boundary of the starting position of the side axis (longitudinal Axis) of the Elastic box element is close to the start boundary of the side axis of the ROW.
    • Flex-end: the boundary of the side axis (longitudinal Axis) of the Elastic box element is positioned close to the end boundary of the Line's side Axis.
    • Center: The Elastic box element is centered on the side axis (vertical Axis) of the ROW. (if the size of the row is smaller than the size of the elastic box element, it overflows the same length in two directions).
    • Baseline: the value is equivalent to ' flex-start ' if the inline axis of the elastic box element is the same as the side Axis. In other cases, the value will participate in the baseline Alignment.
    • Stretch: if the property value of the specified side axis size is ' auto ', its value causes the Item's margin box to be as close to the size of the row as possible, but at the same time, the limit of the ' Min/max-width/height ' property is Followed.

Demo: Align-items

Align-content (applies to the parent class Container)

Sets or retrieves the alignment of the Flex box stacking stretch Line.

Align-content:flex-start | Flex-end | Center | Space-between | Space-around | Stretch
    • Flex-start: each row is stacked to the starting position of the elastic box container. The start boundary of the side axis of the first row in the Elastic box container is close to the start boundary of the side axis of the elastic box container, and each row immediately follows the front ROW.
    • Flex-end: the rows are stacked to the end position of the elastic box container. The end boundary of the side axis of the last line in the Elastic box container is close to the end of the side axis of the elastic box container, and each row immediately follows the previous ROW.
    • Center: each row is stacked to the middle of the elastic box container. Each row 22 is close to live at the same time in the Elastic box container Center alignment, keep the elastic box container side axis start content boundary and the distance between the first row and the side of the container end of the content boundary and the last row of the same distance. (if The remaining space is negative, the rows are equal distances that overflow in two directions.) )
    • Space-between: the rows are evenly distributed in the elastic box container. If the remaining space is negative or there is only one row in the elastic box container, The value is equivalent to ' Flex-start '. In other cases, the start boundary of the side axis of the first row is close to the boundary of the start of the side axis of the elastic box container, the end boundary of the side axis of the last line is close to the end boundary of the side axis of the elastic box container, and the remaining rows are arranged in a flexible box window to maintain equal space between 22.
    • Space-around: the rows are evenly distributed in the elastic box container, with half of the space between the child elements and the child elements preserved on both ENDS. If the remaining space is negative or there is only one row in the elastic box container, The value is equivalent to ' center '. In other cases, the rows are arranged in a flexible box container in a certain way to maintain equal space between 22, while the space behind the first line and the last line is half the rest of the Space.
    • Stretch: each row will stretch to occupy the remaining space. If the remaining space is a negative number, the value is equivalent to ' Flex-start '. In other cases, the remaining space is divided by all rows to enlarge their side axis Dimensions.

Demo: align-content

Order (for Elastic Box model container child Elements)
Order: <integer>
    • <integer>: use integer values to define the order of arrangement, with small values in Front. Can be a negative value.

Demo: Order

Flex-grow (for Flexible box model container Sub-elements)

Sets or retrieves the expansion ratio of the elastic box.

Allocate the remaining space as a ratio based on the expansion factor set by the elastic box Element.

Flex-grow: <number> (default 0)
    • <number>: defines the expansion ratio using numeric values. Negative values are not allowed
    • The default value for Flex-grow is 0, and if no display defines the property, there is no allocation of the remaining space Rights. In this case, b,c two has explicitly defined the flex-grow, and you can see that the remaining space is divided into 4 parts, of which B is 1, and C is 3, or 1:3

Demo: Flex-grow

Flex-shrink (for Flexible box model container Sub-elements)

Sets or retrieves the shrink ratio of the elastic box, which shrinks the space according to the shrink factor set by the elastic box element as a ratio. )

Code from Http://caibaojian.com/flexbox-guide.htmlflex-shrink: <number> (default 1)

Demo: Flex-shrink

Description
    • The default value of Flex-shrink is 1, and if no display defines the property, the ratio is automatically calculated as the default value of 1 after all the factors are summed to make space shrinkage.
    • In this example, C explicitly defines that flex-shrink,a,b is not explicitly defined, but will be calculated according to the default value, you can see that the remaining space is divided into 5 parts, of which A is 1, B is 1, and C is 3 points, that is 1:1:3
    • We can see that the parent container is defined as 400px, the subkey is defined as 200px, and then 600px after the addition, beyond the parent container 200px. Then the 200px needs to be a,b,c Digested.
    • According to the above definition a,b,c will be assigned 200px according to 1:1:3, the calculation can be 40px,40px,120px, in other words, a,b,c need to digest 40px,40px,120px, then you need to subtract the value from the original defined width, and finally get a to 160px , B is 160px,c to 80px
Flex-basis (for Flexible box model container Sub-elements)

Sets or retrieves the Flex box scaling reference Value.

Flex-basis: <length> | Auto (default auto)
    • Auto: no specific width value, depending on other property values
    • <length>: defines the width with a length Value. Negative values are not allowed
    • <percentage>: defines the width in Percent. Negative values are not allowed

Demo: flex-basis

Flex (for Elastic Box Model child Elements)

Composite Properties. Sets or retrieves how the child elements of a telescopic box object allocate Space.

If the abbreviation is flex:1, its calculated value Is: 1 1 0

Flex:none | [flex-grow] | | [flex-shrink] | | [flex-basis]
    • The None:none keyword is calculated as: 0 0 Auto
    • [flex-grow]: defines an extended ratio of elastic box elements.
    • [flex-shrink]: defines the shrinkage ratio of the elastic box Element.
    • [flex-basis]: defines the default datum value for the elastic box Element.

Demo: Flex

Description
    • In the example above, the parent container width (that is, The spindle width) is defined as 800px, and the child element will overflow 1400-800=600px because the child element sets the scaling datum value flex-basis, and the 300+500+600=1400 is Added.
    • Because the shrinkage factor is set at the same time, the weighted synthesis can get 300*1+500*2+600*3=3100px;
    • So we can calculate what amount of overflow the a,b,c will be removed:
    • A is removed overflow amount: 300*1/3100*600=3/31, that is approximately equal to 58px
    • B is removed overflow: 500*2/3100*600=10/31, i.e. approximately equal to 194px
    • C is removed overflow: 600*3/3100*600=18/31, i.e. approximately equal to 348px
    • The actual widths of the last a,b,c were: 300-58=242px, 500-194=306px, 600-348=252px
Align-self (for Elastic Box Model Sub-elements)

Sets or retrieves the alignment of the elastic box element itself in the direction of the side axis (vertical axis).

Align-self:auto | Flex-start | Flex-end | Center | Baseline | Stretch
    • Auto: if the value of ' align-self ' is ' auto ', it evaluates to the ' Align-items ' value of the Element's parent element, and if it does not have a parent element, the value is ' Stretch '.
    • Flex-start: the boundary of the starting position of the side axis (longitudinal Axis) of the Elastic box element is close to the start boundary of the side axis of the ROW.
    • Flex-end: the boundary of the side axis (longitudinal Axis) of the Elastic box element is positioned close to the end boundary of the Line's side Axis.
    • Center: The Elastic box element is centered on the side axis (vertical Axis) of the ROW. (if the size of the row is smaller than the size of the elastic box element, it overflows the same length in two directions).
    • Baseline: the value is equivalent to ' flex-start ' if the inline axis of the elastic box element is the same as the side Axis. In other cases, the value will participate in the baseline Alignment.
    • Stretch: if the property value of the specified side axis size is ' auto ', its value causes the Item's margin box to be as close to the size of the row as possible, but at the same time, the limit of the ' Min/max-width/height ' property is Followed.

Demo: align-self

Common Example 1. Center alignment
. Flex-container {/  * We First create a flex layout context */  display:flex;  /* then we define the flow direction and if we allow the items to wrap * Remember This is the    same as:   * flex-dire ction:row;   * flex-wrap:wrap;   */  Flex-flow:row wrap;  /* then we define what is distributed the remaining space */  justify-content:space-around;}
    1. You control the arrangement of child elements by setting the CSS code of the parent class container (flex-direction:row) from left to right (the default).
    2. Whether the child element wraps when it exceeds the Content. Flex-wrap:wrap (in Line Wrapping mode).
    3. together, It's Flex-flow:row wrap.
    4. Sets the alignment of a child element's elastic box stacking stretch line to a draw distribution in a box justify-content:space-around

Demo: Flexbox3

2. Adaptive Navigation
/* Large */.navigation {  display:flex;  Flex-flow:row wrap;  /* This aligns items to the end line on Main-axis */  justify-content:flex-end;} /* Medium Screens */@media All and (max-width:800px) {  . navigation {/* when on    Medium sized screens, We center I T by evenly distributing empty space around items */    justify-content:space-around;  }}  /* Small Screens */@media All and (max-width:500px) {  . navigation {/    * On Small screens, We is no longer using Row direction But column */    flex-direction:column;  }}
    1. Sets the child element to be left-to-right (flex-direction:row), and the content is out of line wrapping (flex-wrap:wrap).
    2. Set the content of child elements to align right (justify-content:flex-end)
    3. When less than 800px, the content is centered, and when it is less than 500px, the content is arranged vertically, from top to Bottom.

Demo: flexbox4

3. Common 3 Column Mobile priority layout
. wrapper {  display:flex;  Flex-flow:row wrap;} /* We tell all items to be 100% width */.header,. main,. nav,. aside,. footer {  flex:1 100%;} /* We rely on source order for Mobile-first approach * in this case: * 1. Header * 2. Nav * 3. Main * 4. aside * 5.  Footer *//* Medium Screens */@media All and (min-width:600px) {/  * We tell both sidebars to share a row */  . aside {flex:1 auto;}}  /* Large Screens */@media All and (min-width:800px) {/  * We invert order of first sidebar and main * and tell the   Main element to take twice as much width as the and the other sidebars    */  . main {flex:2 0px;}  . Aside-1 {order:1;}  . Main    {order:2;}  . Aside-2 {order:3;}  . Footer  {order:4;}}
    1. Set child elements from left to right, beyond line breaks (flex-flow:row wrap;).
    2. By default, all child elements have a scale of 1 (flex-grow:1) and a scaling ratio of 100% (flex-basis:100%).
    3. Greater than 800px,. main expands to 2. The scaling value is 0 (flex-basis:0px), and the sidebar aside-1 is arranged in the first position, main in the second position, and Aside-2 in the third Position.
    4. Greater than 600: the aside element has an expansion ratio of 1 (flex-grow:1), and the scaling ratio is auto (flex-basis:auto).

Demo: FLEXBOX5

FLEXBOX-CSS3 Flexible Box Model Flexbox full version tutorial

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.