Flex Layout Syntax Tutorials

Source: Internet
Author: User
Page layout is a key application of CSS.

The traditional solution for layout, based on the box model, depends on the Display property + Position property + Float property. It is very inconvenient for those special layouts, for example, vertical centering is not easy to achieve.

In 2009, the website proposed a new solution----flex layout that enables easy, complete, responsive implementation of various page layouts. Currently, it has been supported by all browsers, which means that this feature is now safe to use.

The flex layout becomes the preferred solution for future layouts. This article describes its syntax, and the next article gives the flex notation for common layouts.

The following two articles are mainly referenced below: A complete guides to Flexbox and a Visual guide to CSS3 Flexbox Properties.

First, what is the flex layout?

Flex is the abbreviation for flexible box, meaning "resilient layout", which provides maximum flexibility for box-shaped models.

Any container can be specified as a flex layout.

. box{  Display:flex;}

Inline elements can also use the flex layout.

. box{  Display:inline-flex;}

WebKit the kernel browser, you must add the-webkit prefix.

. box{  Display:-webkit-flex; */Safari */  Display:flex;}

Note that when set to flex layout, the float, clear, and vertical-align properties of the child elements are invalidated.

Ii. Basic Concepts

Elements that use flex layouts, called Flex container, are referred to as "containers." All of its child elements automatically become container members, known as Flex items (Flex Item), or "project" for short.

The container has two axes by default: the horizontal spindle (main axis) and the vertical intersection axis (cross axis). The starting position of the spindle (the intersection with the border) is called main start, the end position is called the main end, the start position of the crossing axis is called Cross start, and the end position is called Cross end.

The project is arranged by default along the spindle. The main axis space occupied by a single item is called the main size, and the occupied intersection space is called cross size.

Third, the container properties

The following 6 properties are set on a container.

Flex-direction

Flex-wrap

Flex-flow

Justify-content

Align-items

Align-content

3.1 Flex-direction Properties

The Flex-direction property determines the direction of the spindle (that is, the direction in which the item is arranged).

. box {  Flex-direction:row | row-reverse | column | Column-reverse;}

It may have a value of 4.

Row (default): The spindle is horizontal and the starting point is on the left side.

Row-reverse: The spindle is in the horizontal direction, starting at the right end.

Column: The spindle is in the vertical direction, starting at the top edge.

Column-reverse: The spindle is in the vertical direction, starting at the bottom edge.

3.2 Flex-wrap Properties

By default, items are lined up in a line (also known as the "axis"). The Flex-wrap property defines how to wrap a line if one of the axes does not fit.

. box{  Flex-wrap:nowrap | wrap | wrap-reverse;}

It may take three values.

(1) nowrap (default): No Line break.

(2) Wrap: Wrap, the first line is above.

(3) Wrap-reverse: line break, the first line is below.

3.3 Flex-flow

The Flex-flow property is a shorthand for the Flex-direction property and Flex-wrap property, and the default value is row nowrap.

. box {  flex-flow: <flex-direction> | | <flex-wrap>;}

3.4 Justify-content Properties

The Justify-content property defines how the item is aligned on the spindle.

. box {      Justify-content:flex-start | flex-end | center | space-between | Space-around;}

It may take 5 values, and the exact alignment is related to the direction of the axis. The following assumes that the spindle is from left to right.

Flex-start (default): Left Justified

Flex-end: Right-justified

Center: Center

Space-between: Justified, the interval between items is equal.

Space-around: The intervals on each side of the item are equal. Therefore, the interval between items is one times larger than the interval between items and borders.

3.5 Align-items Properties

The Align-items property defines how the item is aligned on the intersection axis.

. box {  Align-items:flex-start | flex-end | center | baseline | stretch;}

It may take 5 values. The specific alignment is related to the direction of the cross axis, which assumes that the intersection axis is from top to bottom.

Flex-start: The start alignment of the intersection axis.

Flex-end: The end alignment of the intersection axis.

Center: The midpoint alignment of the intersection axis.

Baseline: The baseline alignment of the first line of text for the item.

Stretch (default): If the item is not set to height or auto, it fills the height of the entire container.

3.6 Align-content Properties

The Align-content attribute defines the alignment of multiple axes. If the item has only one axis, this property does not work.

. box {  Align-content:flex-start | flex-end | center | space-between | space-around | stretch;}

This property may take 6 values.

Flex-start: Aligns with the starting point of the intersection axis.

Flex-end: Aligns with the end of the intersection axis.

Center: Aligns with the midpoint of the intersection axis.

Space-between: aligned with the intersection axis and evenly spaced between the axes.

Space-around: The intervals between each axis are equal. Therefore, the spacing between the axes is one times larger than the interval between the axes and the border.

Stretch (default): The axis fills the entire cross axis.

Iv. Properties of the project

The following 6 properties are set on a project.

Order

Flex-grow

Flex-shrink

Flex-basis

Flex

Align-self

4.1 Order Property

The Order property defines the order in which the items are arranged. The smaller the number, the higher the alignment, and the default is 0.

. Item {  order: <integer>;}

4.2 Flex-grow Properties

The Flex-grow property defines the magnification of the item, which defaults to 0, that is, if there is space left, it is not zoomed in.

. Item {  Flex-grow: <number>;/* Default 0 */}

If all items have a Flex-grow property of 1, they will divide the remaining space (if any). If the Flex-grow property of one project is 2 and the other items are 1, the former occupies more than the remaining space.

4.3 Flex-shrink properties

The Flex-shrink property defines the scaling of the project, which defaults to 1, which means that if there is not enough space, the project shrinks.

. Item {  Flex-shrink: <number>;/* Default 1 */}

If all items have a Flex-shrink property of 1, they will be scaled down when there is not enough space. If the Flex-shrink property of an item is 0 and the other items are 1, the former does not shrink when there is not enough space.

A negative value is not valid for this property.

4.4 Flex-basis Properties

The Flex-basis property defines the spindle space (main size) that the item occupies before allocating the extra space. Based on this property, the browser calculates whether the spindle has extra space. Its default value is auto, which is the original size of the project.

. Item {  flex-basis: <length> | auto;/* Default auto */}

It can be set to the same value as the width or height property (such as 350px), then the item will occupy a fixed space.

4.5 Flex Properties

Flex properties are shorthand for Flex-grow, Flex-shrink, and flex-basis, and the default value is 0 1 auto. The latter two properties are optional.

. item {  Flex:none | [< ' Flex-grow ' > < ' Flex-shrink ';? | | < ' flex-basis ' >]}

This property has two shortcut values: Auto (1 1 Auto) and none (0 0 Auto).

It is recommended that you use this property instead of writing three separate properties separately, because the browser calculates the relevant values.

4.6 Align-self Properties

The Align-self property allows a single item to be aligned differently from other items, overriding the Align-items property. The default value is auto, which represents the Align-items property that inherits the parent element, and, if there is no parent element, is equivalent to stretch.

. item {  Align-self:auto | flex-start | flex-end | center | baseline | stretch;}

  • Related Article

    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.