justify-content 属性定义了项目在主轴上的对齐方式
注意:使用这个属性之前一定要确定好主轴是哪个

属性值 说明
flex-start 默认值从头部开始如果主轴是x轴,则从左到右
flex-end 从尾部开始排列
center 在主轴居中对齐(如果主轴是x轴则水平居中)
space-around 平分剩余空间
space-between 先两边贴边再平分剩余空间(重要)

代码

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  6. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  7. <title>Document</title>
  8. <style>
  9. div {
  10. display: flex;
  11. width: 500px;
  12. height: 500px;
  13. background-color: blue;
  14. flex-direction: row;
  15. justify-content: flex-start;
  16. /* justify-content: flex-end; */
  17. /* justify-content: center; */
  18. /* justify-content: space-around; */
  19. /* justify-content: space-between; */
  20. }
  21. div span {
  22. width: 100px;
  23. height: 100px;
  24. background-color: red;
  25. }
  26. </style>
  27. </head>
  28. <body>
  29. <div>
  30. <span>1</span>
  31. <span>2</span>
  32. <span>3</span>
  33. <span>4</span>
  34. </div>
  35. </body>
  36. </html>