Original text: A Visual Guide to CSS3 Flexbox Properties
Flex layout is officially called the CSS Flexble Box layout model is CSS3 has a new layout model that improves the alignment, direction, and order of elements within a container, even if they are dynamic or of indeterminate size. The main feature of the Flex container is the ability to adjust its child elements to fill the appropriate space in the most suitable way on different screen sizes.
Many designers and developers find Flex layout easy to use. It is simple to position elements so many complex layouts can be implemented with very little code, leading to a simpler development process. The Flex layout algorithm is based on direction, which is different from horizontal or vertical block and inline layout. This Flex layout can be used in small application components, while the CSS3 grid layout model is emerging to handle large-scale layout.
This article mainly explains how each attribute of flex vividly affects the layout.
Before starting to describe the Flexbox properties, let’s briefly introduce the Flexbox model. The Flex layout is called by the parent container A Flex container (flex-container) and its direct child elements are called flex items (flex-item), which are referred to as "containers" and "items" below.
In the image above you can see the attributes and terms, you can read the official W3C documentation to understand what they mean.
The flex container has two reference axes: the horizontal main axis and the cross axis.
Flexbox is a new layout solution proposed by W3C in 2009. The latest standard in October 2014 is used here, and its latest browser support is
2. Usage:
Or if you want it to be like an inline element, you can write it like this:
.flex-container { display: -webkit-flex; /* Safari */ display: flex;}
.flex-container { display: -webkit-inline-flex; /* Safari */ display: inline-flex;}
This property specifies how Flex items are arranged in the flex container. Set flex The main axis of the container, the two main directions in which they (items) are arranged, horizontally like a row or vertically like a column.
flex-direction:row(默认值) | row-reverse | column | column-reverse;
values:
The row direction indicates that flex items are stacked in the container from left to right in a row.
.flex-container { -webkit-flex-direction: row; /* Safari */ flex-direction: row;}
The row-reverse direction indicates that flex items are stacked from right to left.
.flex-container { -webkit-flex-direction: row-reverse; /* Safari */ flex-direction: row-reverse;}
.flex-container { -webkit-flex-direction: column; /* Safari */ flex-direction: column;}
.flex-container { -webkit-flex-direction: column-reverse; /* Safari */ flex-direction: column-reverse;}
The column-reverse direction specifies that flex items are stacked in a column from bottom to top
Initially, the idea of Flexbox is that all items are arranged on a line (the axis). The flex-wrap property controls whether the container arranges its items in one or more rows, and controls the direction in which new rows are stacked.
flex-wrap:nowrap(默认值) | wrap | wrap-reverse;
values:
The flex items will be arranged in a line and they will scale to fit the width of the container.
.flex-container { -webkit-flex-wrap: nowrap; /* Safari */ flex-wrap: nowrap;}
.flex-container { -webkit-flex-wrap: wrap; /* Safari */ flex-wrap: wrap;}
.flex-container { -webkit-flex-wrap: wrap-reverse; /* Safari */ flex-wrap: wrap-reverse;}
flex-flow: <flex-direction> || <flex-wrap>;
values:
.flex-container { -webkit-flex-flow: <flex-direction> || <flex-wrap>; /* Safari */ flex-flow: <flex-direction> || <flex-wrap>;}
Default value: row nowarp
justify-content:flex-start(默认值) | flex-end | center | space-between | space-around;
Defines the alignment of the items on the main axis of the container. When the items in the container are all in one row and are inelastic, or when the items are elastic but have reached their minimum width, this attribute can define the remaining space in the container. Allocation of space.
.flex-container { -webkit-justify-content: flex-start; /* Safari */ flex-start;}
Align all items to the right of the container
.flex-container { -webkit-justify-content: flex-end; /* Safari */ justify-content: flex-end;}
All items are centered in the container
.flex-container { -webkit-justify-content: center; /* Safari */ justify-content: center;}
.flex-container { -webkit-justify-content: space-between; /* Safari */ justify-content: space-between;}
.flex-container { -webkit-justify-content: space-around; /* Safari */ justify-content: space-around;}
align-items:flex-start | flex-end | center | baseline | stretch(默认值);
定义项目在交叉轴上的对齐方式,交叉轴与当前的轴线有关系,与justify-content很相似,只不过是垂直方向的;这属性为所有的项目设置默认的交叉轴上的对齐方式,包括匿名的。
values:
.flex-container { -webkit-align-items: stretch; /* Safari */ align-items: stretch;}
.flex-container { -webkit-align-items: flex-start; /* Safari */ align-items: flex-start;}
项目会堆放在容器交叉轴的起始位置(cross start )。
.flex-container { -webkit-align-items: flex-end; /* Safari */ align-items: flex-end;}
.flex-container { -webkit-align-items: center; /* Safari */ align-items: center;}
.flex-container { -webkit-align-items: baseline; /* Safari */ align-items: baseline;}
基线?不知道基线是什么请戳这里-->基线是什么?
align-content:flex-start | flex-end | center | space-between | space-around | stretch(默认值);
values:
.flex-container { -webkit-align-content: stretch; /* Safari */ align-content: stretch;}
.flex-container { -webkit-align-content: flex-start; /* Safari */ align-content: flex-start;}
.flex-container { -webkit-align-content: flex-end; /* Safari */ align-content: flex-end;}
.flex-container { -webkit-align-content: center; /* Safari */ align-content: center;}
.flex-container { -webkit-align-content: space-between; /* Safari */ align-content: space-between;}
.flex-container { -webkit-align-content: space-around; /* Safari */ align-content: space-around;}
注意:这个属性仅仅当容器中有多行的项目时有效,如果所有项目仅仅占一行,那这个属性对布局没有任何影响。
<span style="font-size:14px; font-family: Arial, Helvetica, sans-serif;"> order: <integer></span>
values:
values:.flex-item { -webkit-order: <integer>; /* Safari */ order: <integer>;}
<span style="font-size:14px;">flex-grow: <number></span>
.flex-item { -webkit-flex-grow: <number>; /* Safari */ flex-grow: <number>;}
当所有的项目的flex-grow值相等的时候它们的size相同。
第二个项目占用了更多的剩余空间。
默认值是:0
注意:负数在这个属性中是没有用的
flex-shrink: <number>
values:
.flex-item { -webkit-flex-shrink: <number>; /* Safari */ flex-shrink:;}
默认情况下,所有的项目都会收缩,但是当我们设置该属性的值为0的时候,项目会保持原有的大小。
默认值是:1
注意:负数在这个属性中是没有用的
flex-basis:auto | <width>
values:
.flex-item { -webkit-flex-basis: auto | <width>; /* Safari */ flex-basis: auto | <width>;}
flex:none | auto | [ <flex-grow> <flex-shrink>? || <flex-basis> ]
values:
.flex-item { -webkit-flex: none | auto | [ <flex-grow> <flex-shrink>? || <flex-basis> ]; /* Safari */ flex: none | auto | [ <flex-grow> <flex-shrink>? || <flex-basis> ];}
align-self:auto | flex-start | flex-end | center | baseline | stretch;
.flex-item { -webkit-align-self: auto | flex-start | flex-end | center | baseline | stretch; /* Safari */ align-self: auto | flex-start | flex-end | center | baseline | stretch;}
注意:auto 表示项目会使用父元素(容器)的align-items的值,如果该项目没有父元素的话align-self的值是stretch。
flex items值得注意的是:float、clear、vertical-align这些属性对于项目(flex item)会失效。