Flex 布局练习

参考链接
A Complete Guide to Flexbox
这个青蛙游戏不错,可以试试
写给自己看的display: flex布局教程
http://www.ruanyifeng.com/blog/2015/07/flex-grammar.html
http://static.vgee.cn/static/index.html

容器的属性

1. flex-direction

flex-direction属性决定主轴的方向(即项目的排列方向)。
.box { flex-direction: row (默认值)| row-reverse | column | column-reverse; }

    
    .box {
      display: flex;
      flex-direction: row;
    }
    
  
1
2
3
4
    
      .box {
        display: flex;
        flex-direction: row-reverse;
      }
    
  
1
2
3
4
      
        .box {
          display: flex;
          flex-direction: column;
        }
      
    
1
2
3
4
        
          .box {
            display: flex;
            flex-direction: column-reverse;
          }
        
      
1
2
3
4

2. flex-wrap属性

默认情况下,项目都排在一条线(又称"轴线")上。flex-wrap属性定义,如果一条轴线排不下,如何换行。
.box{ flex-wrap: nowrap (默认值)| wrap | wrap-reverse; }

    
      .box {
        display: flex;
        flex-wrap: nowrap;
      }
    
  
1
2
3
4
      
        .box {
          display: flex;
          flex-wrap: wrap;
        }
      
    
1
2
3
4
        
          .box {
            display: flex;
            flex-wrap: wrap-reverse;
          }
        
      
1
2
3
4

3. flex-flow

flex-flow属性是flex-direction属性和flex-wrap属性的简写形式,默认值为row nowrap。
.box { flex-flow: <flex-direction> || <flex-wrap>; }

  
    .box {
      display: flex;
      flex-flow: row-reverse wrap-reverse;
    }
  
1
2
3
4

4. justify-content

justify-content属性定义了项目在主轴上的对齐方式。

.box { justify-content: flex-start(默认值) | flex-end | center | space-between | space-around; }

它可能取5个值,具体对齐方式与轴的方向有关。下面假设主轴为从左到右。

  
    flex-start(默认值):左对齐
    flex-end:右对齐
    center: 居中
    space-between:两端对齐,项目之间的间隔都相等。
    space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。
  
    
      .box {
        display: flex;
        justify-content: flex-start;
      }
    
  
1
2
3
4
      
        .box {
          display: flex;
          justify-content: flex-end;
        }
      
    
1
2
3
4
      
        .box {
          display: flex;
          justify-content: center;
        }
      
    
1
2
3
4
      
        .box {
          display: flex;
          justify-content: space-between;
        }
      
    
1
2
3
4
      
        .box {
          display: flex;
          justify-content: space-around;
        }
      
    
1
2
3
4

5. align-items

align-items属性定义项目在交叉轴上如何对齐。

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

它可能取5个值。具体的对齐方式与交叉轴的方向有关,下面假设交叉轴从上到下。

flex-start:交叉轴的起点对齐。 flex-end:交叉轴的终点对齐。 center:交叉轴的中点对齐。 baseline: 项目的第一行文字的基线对齐。 stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。

    
      .box {
        display: flex;
        align-items: flex-start;
      }
    
  
1
2
3
4
5
6
7
8
    
      .box {
        display: flex;
        align-items: flex-end;
      }
    
  
1
2
3
4
    
      .box {
        display: flex;
        align-items: center;
      }
    
  
1
2
3
4
    
      .box {
        display: flex;
        align-items: baseline;
      }
    
  
x
2
3
4
      
        .box {
          display: flex;
          align-items: stretch;
        }
      
    
1
2
3
4

6. align-content

aalign-content属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。

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

        
          .box {
            display: flex;
            flex-wrap: wrap;
            align-content: flex-start;
          }
        
      
1
2
3
4
5
6
7
8
        
          .box {
            display: flex;
            flex-wrap: wrap;
            align-content: flex-end;
          }
        
      
1
2
3
4
5
6
7
8
        
          .box {
            display: flex;
            flex-wrap: wrap;
            align-content: center;
          }
        
      
1
2
3
4
5
6
7
8
        
          .box {
            display: flex;
            flex-wrap: wrap;
            align-content: space-between;
          }
        
      
1
2
3
4
5
6
7
8
        
          .box {
            display: flex;
            flex-wrap: wrap;
            align-content: space-around;
          }
        
      
1
2
3
4
5
6
7
8
        
          .box {
            display: flex;
            flex-wrap: wrap;
            align-content: stretch;
          }
        
      
1
2
3
4
5

项目的属性

以下6个属性设置在项目上。


      order
      flex-grow
      flex-shrink
      flex-basis
      flex
      align-self

1. order

order属性定义项目的排列顺序。数值越小,排列越靠前,默认为0。

      
        .item {
         order: -1
        }
      
    
1
2
3
4

(order: -1)

2. flex-grow

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

如果所有项目的flex-grow属性都为1,则它们将等分剩余空间(如果有的话)。

如果一个项目的flex-grow属性为2,其他项目都为1,则前者占据的剩余空间将比其他项多一倍。

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

3. flex-shrink

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

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

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

4. flex-basis

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

这个讲flex-basis 和height的关系可以参考下 https://www.jianshu.com/p/17b1b445ecd4

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

5. flex

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

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

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

6. align-self

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

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

      
          .item {
            align-self: auto | flex-start | flex-end | center | baseline | stretch;
          }
      
    
        
            .item {
              align-self: flex-end;
            }
        
      
1
2
3
目录生成中...