Use flex elastic layout, tutorials records

 

First, what Flex layout?

Flex is a Flexible Box acronym, meaning "elastic layout", to provide maximum flexibility to the box model.

Any container can be specified as Flex layout.

 

. 1  .box1 {
 2   
. 3  the display: Flex;
 . 4   
. 5  }
 . 6   
. 7  .box2 {
 . 8   
. 9 the display: inline-Flex; <span style = "font-Family: Arial, Helvetica, Sans-serif;"> / * </ span > <span style = "font- family: Arial, Helvetica, sans-serif;"> inline elements may also be used Flex layout. * / </ Span>
 10   
. 11  }
 12 is   
13 is  .box3 {
 14   
15 the display: -webkit-Flex; / * Safari, the Webkit browser kernel must be added -webkit prefix. * / 
16   
. 17  the display: Flex;
 18 is   
. 19 }

 

Note that, in the future to Flex layout, sub-element float, clear, and vertical-align attribute will fail.

Second, the basic concept

Using Flex layout elements, called Flex container (flex container), referred to as "container." It's all child elements automatically become a member of a container, called a Flex project (flex item), referred to as "project."
Default container exists two axes: a horizontal spindle (main axis) and vertical axis intersect (cross axis). The start position of the spindle (intersection with border) is called the main start, end position called the main end; cross-axis start position is called cross start, the end position is called the cross end.
The default project along the spindle arrangement. Single project called the space occupied by the spindle main size, the space occupied by the cross-axis is called the cross size.

Third, the properties of the container

  • flex-direction
  • flex-wrap
  • flex-flow
  • justify-content
  • align-items
  • align-content

3.1 flex-direction property

flex-direction property determines the main direction (i.e., the arrangement direction of the project).
  1. 1 .box {
    2  
    3 flex-direction: row | row-reverse | column | column-reverse;
    4  
    5 }

     

  2.  
  • row (default value): The spindle is horizontal, the starting point at the left end.
  • row-reverse: the spindle horizontal direction, the starting point at the right end.
  • column: main vertical direction, the starting point on the edge.
  • column-reverse: the spindle in the vertical direction, along the lower starting point.
 

 3.2flex-wrap property

By default, items are ranked on a line (also known as "axis"). flex-wrap attribute definition, if a row axis less than how newline
box{
 
flex-wrap: nowrap | wrap | wrap-reverse;
 
}
(1) nowrap (default): Do not wrap.
(2) wrap: wrap, over the first line.
(3) wrap-reverse: wraps, below the first line.
 

3.3 flex-flow

flex-flow property is shorthand for flex-direction property and flex-wrap property, the default value row nowrap.
 
  1.  
    .box {
     
    flex-flow: <flex-direction> || <flex-wrap>;
     
    }


3.4 justify-content property

justify-content attribute defines the alignment of the items on the spindle.
 
 
  1.  
    1 .box {
    2  
    3 justify-content: flex-start | flex-end | center | space-between | space-around;
    4  
    5 }

     

  2.  
  • flex-start (default): Left
  • flex-end: the right alignment
  • center: center
  • space-between: justification, the spacing between the items are equal.
  • space-around: at equal intervals on both sides of each item. Therefore, the gap between the project twice the size of the project and the interval of the border.

3.5 align-items property

 

align-items attribute defines how items in a cross-axis alignment
.box {
 
    align-items: flex-start | flex-end | center | baseline | stretch;
 
}
  • flex-start:交叉轴的起点对齐。
  • flex-end:交叉轴的终点对齐。
  • center:交叉轴的中点对齐。
  • baseline: 项目的第一行文字的基线对齐。
  • stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。

3.6 align-content属性
align-content属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。
 
  1.  
    .
    1 box {
    2  
    3 align-content: flex-start | flex-end | center | space-between | space-around | stretch;
    4  
    5 }

     

  2.  
  • flex-start:与交叉轴的起点对齐。
  • flex-end:与交叉轴的终点对齐。
  • center:与交叉轴的中点对齐。
  • space-between:与交叉轴两端对齐,轴线之间的间隔平均分布。
  • space-around:每根轴线两侧的间隔都相等。所以,轴线之间的间隔比轴线与边框的间隔大一倍。
  • stretch(默认值):轴线占满整个交叉轴。

四、项目的属性
以下6个属性设置在项目上。
 
  • order
  • flex-grow
  • flex-shrink
  • flex-basis
  • flex
  • align-self

4.1 order属性

 
order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0。
  1.  
    1 .item {
    2  
    3 order: <integer>;
    4  
    5 }

     

  2.  

4.2 flex-grow属性 flex-grow属性定义项目的放大比例,默认为0,即如果存在剩余空间,也不放大。

  1.  
    1 .item {
    2  
    3 flex-grow: <number>; /* default 0 */
    4  
    5 }

     

  2.  
如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。

4.3 flex-shrink属性

flex-shrink属性定义了项目的缩小比例,默认为1,即如果空间不足,该项目将缩小。

  1.  
    1 .item {
    2  
    3 flex-shrink: <number>; /* default 1 */
    4  
    5 }
  2.  

如果所有项目的flex-shrink属性都为1,当空间不足时,都将等比例缩小。如果一个项目的flex-shrink属性为0,其他项目都为1,则空间不足时,前者不缩小。
负值对该属性无效。

4.4 flex-basis属性

flex-basis属性定义了在分配多余空间之前,项目占据的主轴空间(main size)。浏览器根据这个属性,计算主轴是否有多余空间。它的默认值为auto,即项目的本来大小。

  1.  
    1 .item {
    2  
    3 flex-basis: <length> | auto; /* default auto */
    4  
    5 }

它可以设为跟width或height属性一样的值(比如350px),则项目将占据固定空间。

4.5 flex属性

flex属性是flex-grow, flex-shrink 和 flex-basis的简写,默认值为0 1 auto。后两个属性可选。

  1.  
    1 .item {
    2  
    3 flex: none | [ <'flex-grow'> <'flex-shrink'>? || <'flex-basis'> ]
    4  
    5 }

该属性有两个快捷值:auto (1 1 auto) 和 none (0 0 auto)。
建议优先使用这个属性,而不是单独写三个分离的属性,因为浏览器会推算相关值。

4.6 align-self属性

align-self属性允许单个项目有与其他项目不一样的对齐方式,可覆盖align-items属性。默认值为auto,表示继承父元素的align-items属性,如果没有父元素,则等同于stretch。

  1.  
    1 .item {
    2  
    3 align-self: auto | flex-start | flex-end | center | baseline | stretch;
    4  
    5 }
  2.  

该属性可能取6个值,除了auto,其他都与align-items属性完全一致。
 

Guess you like

Origin www.cnblogs.com/ajaxlu/p/11512121.html