14-2 操作一下横轴- 使用justify-content属性设置横轴排列方式
本节编程练习不计算学习进度,请电脑登录imooc.com操作

操作一下横轴- 使用justify-content属性设置横轴排列方式

这一章节我们来学习justify-content属性,本属性定义了项目在主轴上的对齐方式。结合上一节的布局例子进行理解,属性值分别为:

 justify-content: flex-start | flex-end | center | space-between | space-around;

flex-start:交叉轴的起点对齐

 .box {
        background: blue;
        display: flex;
        justify-content: flex-start;
    }

实现效果:

flex-end:右对齐

 .box {
        background: blue;
        display: flex;
        justify-content: flex-end;
    }

实现效果:

center: 居中

 .box {
        background: blue;
        display: flex;
        justify-content: center;
    }

实现效果:

space-between:两端对齐,项目之间的间隔都相等。

 .box {
        background: blue;
        display: flex;
        justify-content: space-between;
    }

实现效果:

space-around:每个项目两侧的间隔相等。所以,项目之间的间隔比项目与边框的间隔大一倍。

.box {
        background: blue;
        display: flex;
        justify-content: space-around;
    }

实现效果:

任务

自己按照上面的代码测试,感受下各个属性值实现的样式。

  1. <!DOCTYPE html>
  2. <html>
  3.  
  4. <head>
  5. <meta charset="UTF-8">
  6. <title>justify-content</title>
  7. <style type="text/css">
  8. .box {
  9. background: blue;
  10. display: flex;
  11.  
  12. }
  13.  
  14. .box div {
  15.  
  16. width: 200px;
  17. height: 200px;
  18. }
  19.  
  20. .box1 {
  21. background: red;
  22. }
  23.  
  24. .box2 {
  25. background: orange;
  26. }
  27.  
  28. .box3 {
  29. background: green;
  30. }
  31. </style>
  32. </head>
  33.  
  34. <body>
  35. <div class="box">
  36. <div class="box1"></div>
  37. <div class="box2"></div>
  38. <div class="box3"></div>
  39. </div>
  40. </body>
  41. </html>
下一节