Blogger Information
Blog 5
fans 0
comment 1
visits 2734
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
自己整一个练习flex做骰子做熟慢慢就理解flex布局
收银系统数据化的博客
Original
646 people have browsed it

废话不多说,直接整代码,注释很详细方便复习

  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>flex布局</title>
  7. <style>
  8. * {
  9. margin: 0;
  10. padding: 0;
  11. box-sizing: border-box;
  12. }
  13. body {
  14. display: flex;
  15. }
  16. [class$="face"] {
  17. display: flex;
  18. margin: 16px;
  19. padding: 4px;
  20. background-color: #e7e7e7;
  21. width: 104px;
  22. height: 104px;
  23. box-shadow: inset 0 5px white, inset 0 -5px #bbb, inset 5px 0 #d7d7d7,
  24. inset -5px 0 #d7d7d7;
  25. border-radius: 10%;
  26. object-fit: contain;
  27. }
  28. .pip {
  29. width: 24px;
  30. height: 24px;
  31. /* 设置圆角 */
  32. border-radius: 50%;
  33. /* 设置阴影 */
  34. box-shadow: inset 0 3px #111, inset 0 -3px #555;
  35. background-color: #333;
  36. color: white;
  37. text-align: center;
  38. }
  39. /* 处理第一个让他中部居中 */
  40. .first-face {
  41. display: flex;
  42. /* 设置在主轴上居中对齐 */
  43. justify-content: center;
  44. /* 在交叉轴上居中对齐,这样就形成了中部居中 */
  45. align-items: center;
  46. }
  47. /* 第二个让两个项目一个角一个*/
  48. .second-face {
  49. display: flex;
  50. /* 首先让项目在主轴上两端对齐 */
  51. justify-content: space-between;
  52. }
  53. /* 再让第二个项目出现在交叉轴结束处 */
  54. .second-face > .pip:nth-of-type(2) {
  55. /* 允许该项目跟别的项目有不一样的对齐方式 */
  56. align-self: flex-end;
  57. }
  58. /* 现在处理第三个 */
  59. .third-face {
  60. display: flex;
  61. /* 先让所有项目两端对齐 */
  62. justify-content: space-between;
  63. }
  64. /* 再把第二个拉到居中位置 */
  65. .third-face > .pip:nth-of-type(2) {
  66. /* 再次使用让第二个与其它有不一样的对齐方式 */
  67. align-self: center;
  68. }
  69. /* 让第三个也使用不一样的对齐方式,仍然使用align-self */
  70. .third-face > .pip:nth-of-type(3) {
  71. /* 这次是放在交叉轴的结束位置 */
  72. align-self: flex-end;
  73. }
  74. /* 现在处理第四个时出现了列的概念每个列是单独盒子 */
  75. .fourth-face {
  76. display: flex;
  77. /* 让fourth-face做个一个大盒子两端对齐 */
  78. justify-content: space-between;
  79. }
  80. .fourth-face > .column {
  81. display: flex;
  82. /* 把列的小盒子中方向改为列显示 */
  83. flex-direction: column;
  84. /* 再来一次两端对齐这样四个项目就分部在大项目的角上了 */
  85. justify-content: space-between;
  86. }
  87. /* 第五个用的是三列了 */
  88. .fivth-face {
  89. display: flex;
  90. justify-content: space-between;
  91. }
  92. .fivth-face > .column {
  93. display: flex;
  94. /* 让这三个列统一主轴方向变列 */
  95. flex-direction: column;
  96. /* 让这三个列统一两端对齐 */
  97. justify-content: space-between;
  98. }
  99. .fivth-face > .column:nth-of-type(2) {
  100. /* 让第二列在主轴居中 */
  101. justify-content: center;
  102. }
  103. /* 第六个用两列就可以了 */
  104. .sixth-face {
  105. display: flex;
  106. justify-content: space-between;
  107. }
  108. .sixth-face > .column {
  109. display: flex;
  110. justify-content: space-between;
  111. /* 主轴方向调整成列,根上面第五个一样 */
  112. flex-direction: column;
  113. }
  114. </style>
  115. </head>
  116. <body>
  117. <div class="first-face">
  118. <div class="pip">1</div>
  119. </div>
  120. <div class="second-face">
  121. <div class="pip">1</div>
  122. <div class="pip">2</div>
  123. </div>
  124. <div class="third-face">
  125. <div class="pip">1</div>
  126. <div class="pip">2</div>
  127. <div class="pip">3</div>
  128. </div>
  129. <div class="fourth-face">
  130. <div class="column">
  131. <div class="pip">1</div>
  132. <div class="pip">2</div>
  133. </div>
  134. <div class="column">
  135. <div class="pip">3</div>
  136. <div class="pip">4</div>
  137. </div>
  138. </div>
  139. <div class="fivth-face">
  140. <div class="column">
  141. <div class="pip">1</div>
  142. <div class="pip">2</div>
  143. </div>
  144. <div class="column">
  145. <div class="pip">3</div>
  146. </div>
  147. <div class="column">
  148. <div class="pip">4</div>
  149. <div class="pip">5</div>
  150. </div>
  151. </div>
  152. <div class="sixth-face">
  153. <div class="column">
  154. <div class="pip">1</div>
  155. <div class="pip">2</div>
  156. <div class="pip">3</div>
  157. </div>
  158. <div class="column">
  159. <div class="pip">4</div>
  160. <div class="pip">5</div>
  161. <div class="pip">6</div>
  162. </div>
  163. </div>
  164. </body>
  165. </html>

上个运行效果看看


The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!