2.弹性布局.html 4.8 KB

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