当前位置:  首页>> 技术小册>> Web响应式布局入门到实战

当子元素为多行的时候,align-items不能同时处理多行,只能针对每一个子元素来处理。

如下面的示例,当父元素宽度不够,并且设置为wrap时,此时使用align-items,效果如下:

  1. <style>
  2. #div0{
  3. width:380px;
  4. background-color: violet;
  5. display:flex;
  6. flex-wrap: wrap;
  7. height:400px;
  8. align-items: center;
  9. }
  10. #div0 div{
  11. width: 100px;
  12. height: 100px;
  13. background-color: yellow;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="div0">
  19. <div>1</div>
  20. <div>2</div>
  21. <div>3</div>
  22. <div>4</div>
  23. </div>
  24. </body>

效果:

align-content:
作用 设置每个flex元素在交叉轴上的默认对齐方式

  1. <style>
  2. #div0{
  3. width:380px;
  4. background-color: violet;
  5. display:flex;
  6. flex-wrap: wrap;
  7. height:400px;
  8. align-content: center;
  9. }
  10. #div0 div{
  11. width: 100px;
  12. height: 100px;
  13. background-color: yellow;
  14. }
  15. </style>
  16. </head>
  17. <body>
  18. <div id="div0">
  19. <div>1</div>
  20. <div>2</div>
  21. <div>3</div>
  22. <div>4</div>
  23. </div>
  24. </body>

align-content将子元素作为一个整体,在交叉轴居中。

效果:

align-content属性值

属性值 作用
flex-start 默认值,子元素x轴方向,左对齐
flex-end 子元素x轴方向,右对齐
center 居中显示
space-between 当父元素有剩余空间时,子元素平均分布在该行上,两边不留间隔空间
space-around 当父元素有剩余空间时,子元素平均分布在该行上,两边留有一半的间隔空间

该分类下的相关小册推荐: