弹性布局.html 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>弹性布局</title>
  7. <style>
  8. #box {
  9. width: 300px;
  10. height: 300px;
  11. background: #ddd;
  12. display: flex;
  13. flex-direction: column;
  14. justify-content: center;
  15. }
  16. #child {
  17. width: 300px;
  18. height: 100px;
  19. background: orange;
  20. }
  21. </style>
  22. </head>
  23. <body>
  24. <h1>弹性布局</h1>
  25. <pre>
  26. 这种方式也是首先给父元素设置 display:flex; 设置好之后改变主轴的方向 flex-direction: column; 该属性可能的取值有四个,分别如下:
  27.   
  28. row(该值为默认值):主轴为水平方向,起点在左端
  29. row-reverse:主轴为水平方向,起点在右端
  30. column:主轴为垂直方向,起点在上沿
  31. column-reverse:主轴为垂直方向,起点在下沿
  32. justify-content 属性定义了项目在主轴上的对齐方式,可能的取值有五个,分别如下(具体的对齐方式与主轴的方向有关,以下假定主轴方向为默认的从左到右):
  33. flex-start(该值是默认值):左对齐
  34. flex-end:右对齐
  35. center:居中对齐
  36. space-between:两端对齐,各个项目之间的间隔均相等
  37. space-around:各个项目两侧的间隔相等
  38. </pre>
  39. <div id="box">
  40. <div id="child"></div>
  41. </div>
  42. </body>
  43. </html>