04.弹性容器的样式二.html 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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. * {
  10. margin: 0;
  11. padding: 0;
  12. list-style: none;
  13. }
  14. ul {
  15. width: 600px;
  16. height: 800px;
  17. border: 10px red solid;
  18. /* 设置ul为弹性容器 */
  19. display: flex;
  20. /*
  21. flex-wrap:
  22. 设置弹性元素是否在弹性容器中自动换行
  23. 可选值:
  24. nowrap 默认值,元素不会自动换行
  25. wrap 元素沿着辅轴方向自动换行
  26. wrap-reverse 元素沿着辅轴反方向换行
  27. */
  28. /* flex-wrap: wrap; */
  29. /* flex-wrap: wrap-reverse; */
  30. /* flex-flow wrap 和 direction的简写属性,并且两个值没有顺序要求 */
  31. flex-flow: row wrap;
  32. /*
  33. justify-content
  34. -如何分配主轴上的空白空间(即主轴上的元素如何排列)
  35. -可选值:
  36. flex-start 表示元素沿着主轴的起边排列
  37. flex-end 表示元素沿着主轴的终边排列
  38. center 表示元素居中排列
  39. space-around 空白分布到元素两侧
  40. space-between 空白均匀分布到元素间
  41. space-evenly 空白分布到元素的单侧
  42. */
  43. /* justify-content: flex-start; */
  44. /* justify-content: flex-end; */
  45. /* justify-content: center; */
  46. /* justify-content: space-around; */
  47. /* justify-content: space-evenly; */
  48. /* justify-content: space-between; */
  49. /*
  50. align-items
  51. -元素在辅轴上如何对齐
  52. -元素间的关系
  53. -可选值
  54. stretch 默认值,将元素的长度设置为相同的值
  55. flex-start 元素不会拉伸,沿着辅轴起边对齐
  56. flex-end 沿着辅轴的终边对齐
  57. center 居中对齐
  58. baseline 基线对齐
  59. 要想设置元素居中,可以使用:
  60. justify-content: center;
  61. align-items: center;
  62. 来实现
  63. */
  64. /* align-items: stretch; */
  65. align-items: flex-start;
  66. /* align-items: flex-end; */
  67. /* align-items: center; */
  68. /* align-items: baseline; */
  69. /* align-content:辅轴空白空间的分布 */
  70. /* align-content: center; */
  71. /* align-content: flex-start; */
  72. /* align-content: flex-end; */
  73. /* align-content: space-around; */
  74. /* align-content: space-between; */
  75. align-content: space-evenly;
  76. }
  77. li {
  78. width: 200px;
  79. background-color: #bfa;
  80. font-size: 50px;
  81. text-align: center;
  82. line-height: 100px;
  83. flex-shrink: 0;
  84. }
  85. li:nth-child(1){
  86. /* align-self:用来覆盖当前弹性元素上的align-items */
  87. /* align-self: center; */
  88. /* align-self: flex-start; */
  89. /* align-self: flex-end; */
  90. align-self: stretch;
  91. }
  92. li:nth-child(2) {
  93. background-color: pink;
  94. }
  95. li:nth-child(3) {
  96. background-color: orange;
  97. }
  98. li:nth-child(4){
  99. background-color: yellow;
  100. }
  101. li:nth-child(5){
  102. background-color: chocolate;
  103. }
  104. </style>
  105. </head>
  106. <body>
  107. <ul>
  108. <li>1</li>
  109. <li>
  110. 2
  111. <div>2</div>
  112. </li>
  113. <li>
  114. 3
  115. <div>3</div>
  116. <div>3</div>
  117. </li>
  118. <li>1</li>
  119. <li>
  120. 2
  121. <div>2</div>
  122. </li>
  123. </ul>
  124. </body>
  125. </html>