4.弹性盒子容器属性.html 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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>Document</title>
  7. <style>
  8. #box {
  9. width: 800px;
  10. height: 800px;
  11. background: #ccc;
  12. /* 开启弹性布局 */
  13. display: flex;
  14. flex-wrap: wrap;
  15. /* flex-direction: column; */
  16. /* flex-wrap: wrap-reverse; */
  17. /* flex-flow: row nowrap; */
  18. /* justify-content: flex-end; */
  19. /* align-items: baseline; */
  20. align-content: stretch;
  21. }
  22. #box4{
  23. width: 100px;
  24. height: 50px;
  25. background: #00f;
  26. }
  27. #box5 {
  28. width: 120px;
  29. height: 100px;
  30. background: #0f0;
  31. }
  32. #box6 {
  33. width: 100px;
  34. height: 100px;
  35. background: #f00;
  36. }
  37. /* #box1,#box2,#box3 {
  38. width: 200px;
  39. height: 200px;
  40. }
  41. #box1 {
  42. background-color: #f00;
  43. }
  44. #box2 {
  45. background-color: #ff0;
  46. }
  47. #box3 {
  48. background-color: #00f;
  49. } */
  50. </style>
  51. </head>
  52. <body>
  53. <!-- 弹性盒子 -->
  54. <!--
  55. Flex 弹性布局
  56. Flex属性分为两部分,一部分作用于容器称""容器属性"",另一部分作用于项目称为""项目属性"";
  57. display:flex; 开启弹性盒
  58. display:-webkit-flex; 开启对应内核浏览器的弹性盒
  59. display:inline-flex; 开启行内的弹性盒
  60. 注意:设置为flex布局后,子元素的float、clear、vertical-align属性将失效;
  61. 容器属性:
  62. 1.flex-direction
  63. 决定主轴的方向(项目的排列方向)
  64. .box {
  65. flex-direction:row|row-reverse|column|column-reverse;
  66. }
  67. row(默认) 从左向右排列,主轴为水平方向,起点在左端
  68. row-reverse 从右向左排列,主轴为水平方向,起点在右端
  69. column 从上向下排列,起点在上沿
  70. column-reverse 从下向上排列,起点在下沿
  71. 2.flex-wrap
  72. 默认情况下,Flex项目都排在一条线上,又称"轴线"上;
  73. 我们可以通过flex-wrap属性设置,让Flex项目换行排列;
  74. .box {
  75. flex-wrap:nowrap|wrap|warp-reverse;
  76. }
  77. nowrap(默认):不换行;所有Flex项目单行排列:
  78. wrap:换行,第一行在上方;所有Flex项目多行排列,按从上到下的顺序;
  79. wrap-reverse:换行,第一行在下方;所有Flex项目多行排列,按从下到上的顺序;
  80. 3. flex-flow
  81. 是flex-direction属性和flex-wrap属性的间歇性是,默认值为row nowrap;
  82. .box {
  83. flex-flow:<flex-direction><flex-wrap>;
  84. }
  85. 4. justify-content属性定义了项目在主轴上的对齐方式;
  86. .box {
  87. justify-content:flex-start|flex-end|center|space-between|space-around;
  88. }
  89. flex-start(默认):左对齐;
  90. flex-end:右对齐;
  91. center:居中;
  92. space-between:两端对齐,项目之间间隔相等;
  93. space-around:项目均匀分布,每一个项目两侧有相同的留白空间,相邻项目之间的距 离是两个项目之间留白的和;
  94. space-evenly:项目均匀分布,所有项目之间及项目与边框之间距离相等;
  95. 5. align-items
  96. 属性定义项目在交叉轴上的对齐方式。
  97. .box {
  98. align-items: stretch | flex-start | flex-end | center | baseline;
  99. }
  100. stretch(默认值):如果项目未设置高度或设为auto,将占满整个容器的高度。
  101. flex-start:交叉轴的起点对齐。
  102. flex-end:交叉轴的终点对齐。
  103. center:交叉轴的中点对齐。
  104. baseline: 项目的第一行文字的基线对齐。
  105. 6. align-content
  106. 属性定义了多根轴线的对齐方式。如果项目只有一根轴线,该属性不起作用。
  107. .box {
  108. align-content: flex-start | flex-end | center | space-between | space-around | stretch;
  109. }
  110. stretch (默认):拉伸显示
  111. flex-start:从启点线开始顺序排列
  112. flex-end:相对终点线顺序排列
  113. center:居中排列
  114. space-between:项目均匀分布,第一项在启点线,最后一项在终点线
  115. space-around:项目均匀分布,每一个项目两侧有相同的留白空间,相邻项目之间的距离是两个项目之间留白的和
  116. 注意: Internet Explorer 10 及更早版本浏览器不支持 align-content 属性。
  117. 注意: Safari 7.0 及更新版本通过 -webkit-align-content 属性支持该属性。
  118. 注意:Internet Explorer, Firefox, 和 Safari 浏览器不支持 align-content 属性。
  119. -->
  120. <div id="box">
  121. <!-- <div id="box1">1</div>
  122. <div id="box2">2</div>
  123. <div id="box3">3</div> -->
  124. <!-- <div id="box1">1</div>
  125. <div id="box2">2</div>
  126. <div id="box3">3</div>
  127. <div id="box1">1</div>
  128. <div id="box2">2</div>
  129. <div id="box3">3</div> -->
  130. <div id="box4"></div>
  131. <div id="box5"></div>
  132. <div id="box6"></div>
  133. <div id="box4"></div>
  134. <div id="box5"></div>
  135. <div id="box6"></div>
  136. <div id="box4"></div>
  137. <div id="box5"></div>
  138. <div id="box6"></div>
  139. <div id="box4"></div>
  140. <div id="box5"></div>
  141. <div id="box6"></div>
  142. </div>
  143. </body>
  144. </html>