Day 103th: Flex layout in CSS3 (Flexible layout)

Source: Internet
Author: User

One, 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 : When set to Flex layout , the float, clear, and vertical-align properties of 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, and the start position of the intersection axis is called Cross . Start, the end position is called Cross end. The cross axis is also called the side axis .

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.

Three, container properties The following 6 properties are set on the 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 has 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 ranked on a single 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;}

     

This property may take 6 values, except auto, and the other is exactly the same as the Align-items property.

Day 103th: Flex layout in CSS3 (Flexible layout)

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.