CSS3 's Flexible boxes detailed use tutorial

Source: Internet
Author: User

What's flexible box? Flexible is a telescopic box, which can be understood as a new box model-telescopic box model. Proposed by the CSS3 specification, this is in the original everyone very familiar with block, Inline-block, inline based on the extension of the new generation of layout mode.

Browser compatibility

As a very realistic developer, whether to focus on a new technology, first of all consider its browser compatibility. The browser compatibility of our telescopic box model looks pretty good.

Can be seen, the modern browser basically support, IE10 began to support (IE and safari respectively add-ms-and-webkit-prefix), mobile end of the support situation is also relatively good, the only platform does not support opera, we do not take him to play →_→

Therefore, comrade Obama said: "Telescopic box model is good and promising." (MM-hmm ~)

Basic concept of Telescopic box

The biggest feature of telescopic box or the advantage is that it takes into account the current high housing prices and the people's growing housing demand between the contradictions, the housing area is limited, but our telescopic box to the most reasonable and most efficient to share the house to everyone. The area is more, give the big one more points, the area is small, let everybody squeeze a few points, in a word will not let any one person to sleep on the street (overflow)!

Now that we mention the relationship between the house and the tenant, the arrangement of the occupants naturally needs to be in a certain direction. For block-level elements, the extension direction of the layout is Top-down, that is, portrait. For inline elements, the layout extends direction from left to right, which is horizontal. and telescopic box, it is the direction of the variable, both vertical and horizontal stretching, depending on your settings.

Basic terminology of Telescopic box model

The idea of the telescopic box model is quite different from the general layout of block-level elements and inline elements, and it introduces new concepts and terms, which are shown in the following picture:

Flex Container Telescopic Box container

This is used to divide the house, this is a magical house, to let it become magical, the display property declared as Flex or Inline-flex can ~

Flex Item Scaling Item

The residents of the house, they will occupy the area of their own due housing.

For the sake of image description, we use code to explain.

xml/html code to copy content to clipboard

  

  

Item 1

  

Item 2

  

Item 3

  

The CSS is set to:

CSS code copy content to clipboard

. container {

Display:flex;

width:300px;

height:100px;

...

}

Display:inline-flex here, it seems to be OK.

For the expansion of the elements, we need to give them prior arrangements for the proportion of housing area, we use the simplest and healthiest 1:1:1 it ~ we will be proportional to the statement in the Flex attribute

CSS code copy content to clipboard

. item-1 {

Flex:1;

...

}

. item-2 {

Flex:1;

...

}

. item-3 {

Flex:1;

...

}

Our big house is perfectly flat into three compartments, three share the rent!

If someone wants to live in a bigger house, we can change the flex ratio directly:

CSS code copy content to clipboard

. item-1 {

Flex:1;

...

}

. item-2 {

Flex:1;

...

}

. item-3 {

Flex:2;

...

}

Is it convenient?

Axes axis

As we can see, there are two axes in the diagram, with the spindle and the secondary axis (perpendicular to the spindle) respectively. But actually which one is the main axis is not certain, is by us to stipulate.

1. Flex-direction This property to specify which axis is the spindle.

2. Justify-content This property sets the arrangement of the retractable item in the spindle direction, which is explained later.

3. Align-items This property is relative to the justify-content above to indicate how the flex item is arranged on the secondary axis.

4. Align-self This property sets the layout of a particular telescopic element on the secondary axis, and setting the property on an element overrides its Align-items property. That is, this attribute will make an element more personality, not unusual way ~

Flex-direction

When we do not want to go along the default direction of the house, we can change the value of the Flex-direction property to change the spindle and direction, the property default value of row;

CSS code copy content to clipboard

. container {

Flex:row-reverse;

...

}

As the name suggests, this allows the alignment of the expansion items to be reversed:

When this property is set to column, the primary and secondary axes are reversed, and the direction of the elements is changed:

CSS code copy content to clipboard

. container {

Flex-direction:column;

...

}

As for the meaning of the flex:column-reverse, I don't need to say more.

Justify-contents

Sometimes, we live next to each other, there is no gap will be very uncomfortable, even the aisle is not, privacy can not guarantee it. This time, we can change the distribution policy, no longer proportional distribution, but a quota allocation, each person's area is determined. The extra housing area is converted into a public area.

CSS code copy content to clipboard

. Item {

width:80px;

...

}

...

Set the Width property also remember to remove the Flex attribute statement Oh, otherwise the effect of flex properties will still cover the width of the ~

At the same time, if the Width property is not set, the width of the elements will be displayed as the width of the content, which means when there is no content inside the expansion, will not be rendered, its performance and display:none;

At this point, the Justify-content property is declared on the container so that the position of the retractable item can be arranged:

CSS code copy content to clipboard

. Content {

Justify-content:flex-start | Flex-end | Center | Space-between | Space-around;

}

Align-items

This property changes the way the elements are arranged on the secondary axis, and for this example the height of the element in the secondary axis direction is height:100%, and the height of the Align-items property is changed when the attribute is set.

CSS code copy content to clipboard

. Content {

Align-items:flex-start | Flex-end | Center | Baselinebaseline | Stretch

}

Here, telescopic concrete performance can be imagined, imagine not to come out on their own hands to try it ~

Order

Declaring this property on a scaling item allows you to follow the order of the HTML structure and flex-direction in order from small to large. Like what:

CSS code copy content to clipboard

. item-1 {

Order:3;

...

}

. item-2 {

order:1;

...

}

. item-3 {

Order:2;

...

}

Flex-wrap

The default value for this property is nowrap, that is, ignore the width of the expansion, how much you want to the housing area, all in accordance with the flex attributes agreed to assign, do not change lines.

CSS code copy content to clipboard

. container {

Flex-wrap:nowrap | Wrap | Wrap-reverse

}

. Item {

width:150px;

}

Flex-grow,flex-shrink and Flex-basis

The flex attribute mentioned above is actually shorthand for these three attributes. These three properties are similar in that they represent a relative proportional relationship between an item and an item's allocated space, except that:

1. Flex-grow property: The value of the property is the ratio of the space occupied by the expansion item to other scalable items (items that declare flex-related properties).

2. Flex-shrink property: The ratio of the telescopic term to the narrowing of other scales, that is to say, when Flex-shrink:3, the item occupies 1/3 of the other items.

3. Flex-basis Property: The property value is the percentage of space that the item occupies as a container.

Note: For the Flex-basis property, when the property values of all items are added <=100%, they are strictly rendered according to the percent value. When the attribute value is added >100%, the element does not overflow, but behaves as if the space size between 22 follows the percentage ratio of each other. That is, when there are three telescopic items and the Flex-basis value is 50%, the performance behavior and three items are flex:1.

The flexible boxes layout pattern is especially useful in response development, and it will become a very simple matter to set the spatial distribution relationship between elements for different terminals. Telescopic box layout and response layout of the popular fluid layout which is better, or can be combined to see the developers to play their intelligence!

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.