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

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

什么是交叉轴?
如果主轴为x轴,则交叉轴为y轴
如果主轴为y轴,则交叉轴为x轴

总结
判断主轴和交叉轴的依据是看子元素是水平排列还是竖向排列。
判断的顺序为:子元素的排列方向 — 主轴 — 交叉轴

属性值 作用
flex-start 位于窗口的开头
flex-end 位于容器的结尾
center 居中显示

center示例:

  1. <style>
  2. #div0{
  3. width:400px;
  4. background-color: violet;
  5. display:flex;
  6. height:400px;
  7. align-items: center; /* 交叉轴对齐方式为居中 */
  8. }
  9. #div0 div{
  10. width: 100px;
  11. height: 100px;
  12. background-color: yellow;
  13. }
  14. </style>
  15. </head>
  16. <body>
  17. <div id="div0">
  18. <div>1</div>
  19. <div>2</div>
  20. <div>3</div>
  21. <div>4</div>
  22. </div>
  23. </body>

效果:

flex-end效果:

改变主轴为y轴,则x轴为交叉轴
示例:

  1. <style>
  2. #div0{
  3. width:400px;
  4. background-color: violet;
  5. display:flex;
  6. height:400px;
  7. flex-direction: column; /* 子元素竖向排列,则x轴为交叉轴 */
  8. align-items: flex-end; /* 交叉轴对齐方式x轴居右显示 */
  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>

效果:


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