博客列表 >flex布局理解

flex布局理解

▽空城旧梦
▽空城旧梦原创
2021年03月26日 11:48:56566浏览

flex布局 多行

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>弹性盒子</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. html {
  15. font-size: 10px;
  16. }
  17. body {
  18. font-size: 1.6rem;
  19. }
  20. .container {
  21. display: flex;
  22. height: 20rem;
  23. /* width: 5em; */
  24. boreder: 1px solid black;
  25. }
  26. .container > .item {
  27. width: 10rem;
  28. background-color: chartreuse;
  29. border: 1px solid black;
  30. }
  31. /* 默认 */
  32. .container {
  33. flex-flow: row wrap;
  34. }
  35. /* .container {
  36. flex-flow: row wrap;
  37. } */
  38. </style>
  39. </head>
  40. <body>
  41. <div class="container">
  42. <item class="item">item1</item>
  43. <item class="item">item2</item>
  44. <item class="item">item3</item>
  45. <item class="item">item4</item>
  46. <item class="item">item5</item>
  47. <item class="item">item6</item>
  48. <item class="item">item7</item>
  49. <item class="item">item8</item>
  50. </div>
  51. </body>
  52. </html>

列布局

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>弹性盒子</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. html {
  15. font-size: 10px;
  16. }
  17. body {
  18. font-size: 1.6rem;
  19. }
  20. .container {
  21. display: flex;
  22. height: 20rem;
  23. /* width: 5em; */
  24. boreder: 1px solid black;
  25. }
  26. .container > .item {
  27. width: 10rem;
  28. height: 5rem;
  29. background-color: chartreuse;
  30. border: 1px solid black;
  31. }
  32. /* 默认 */
  33. .container {
  34. flex-flow: column wrap;
  35. }
  36. /* .container {
  37. flex-flow: row wrap;
  38. } */
  39. </style>
  40. </head>
  41. <body>
  42. <div class="container">
  43. <item class="item">item1</item>
  44. <item class="item">item2</item>
  45. <item class="item">item3</item>
  46. <item class="item">item4</item>
  47. <item class="item">item5</item>
  48. <item class="item">item6</item>
  49. <item class="item">item7</item>
  50. <item class="item">item8</item>
  51. </div>
  52. </body>
  53. </html>

手机页面

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>手机页面布局</title>
  8. </head>
  9. <style>
  10. * {
  11. margin: 0;
  12. padding: 0;
  13. box-sizing: border-box;
  14. }
  15. html {
  16. font-size: 10px;
  17. }
  18. body {
  19. font-size: 1.6rem;
  20. }
  21. .container {
  22. display: flex;
  23. height: 100vh;
  24. border: 1px solid #000;
  25. flex-flow: column nowrap;
  26. }
  27. .container > .item {
  28. height: 5rem;
  29. width: 10rem;
  30. border: 1px solid #000;
  31. background-color: red;
  32. }
  33. .container > .item:nth-of-type(1),
  34. .container > .item:nth-of-type(3) {
  35. height: 5vh;
  36. }
  37. .container > .item:nth-of-type(2) {
  38. height: 90vh;
  39. }
  40. </style>
  41. <body>
  42. <div class="container">
  43. <div class="item">item1</div>
  44. <div class="item">item2</div>
  45. <div class="item">item3</div>
  46. </div>
  47. </body>
  48. </html>

项目对齐方式

1.整体居中对齐

2.整体末尾对齐

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>flex容器属性</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. :root {
  15. font-size: 10px;
  16. }
  17. body {
  18. font-size: 1.6rem;
  19. }
  20. .container {
  21. display: flex;
  22. height: 20rem;
  23. border: 1px solid blue;
  24. justify-content: end;
  25. /* justify-content: center; */
  26. }
  27. .container .item {
  28. width: 5rem;
  29. background-color: red;
  30. }
  31. </style>
  32. </head>
  33. <body>
  34. <div class="container">
  35. <div class="item">item1</div>
  36. <div class="item">item2</div>
  37. <div class="item">item3</div>
  38. <div class="item">item4</div>
  39. </div>
  40. </body>
  41. </html>

交叉轴对齐方式

  1. <!DOCTYPE html>
  2. <html lang="zh-CN">
  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>flex容器属性</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. box-sizing: border-box;
  13. }
  14. :root {
  15. font-size: 10px;
  16. }
  17. body {
  18. font-size: 1.6rem;
  19. }
  20. .container {
  21. display: flex;
  22. height: 20rem;
  23. border: 1px solid blue;
  24. justify-content: space-evenly;
  25. /* justify-content: end; */
  26. /* justify-content: center; */
  27. align-items: center;
  28. }
  29. .container .item {
  30. /* width: 5rem; */
  31. background-color: red;
  32. }
  33. </style>
  34. </head>
  35. <body>
  36. <div class="container">
  37. <div class="item">item1</div>
  38. <div class="item">item2</div>
  39. <div class="item">item3</div>
  40. <div class="item">item4</div>
  41. </div>
  42. </body>
  43. </html>
  44. 3.多行文本布局
  45. ![](https://img.php.cn/upload/image/138/224/814/1616730514845755.png)
  46. ```html
  47. <!DOCTYPE html>
  48. <html lang="zh-CN">
  49. <head>
  50. <meta charset="UTF-8" />
  51. <meta http-equiv="X-UA-Compatible" content="IE=edge" />
  52. <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  53. <title>flex容器属性</title>
  54. <style>
  55. * {
  56. margin: 0;
  57. padding: 0;
  58. box-sizing: border-box;
  59. }
  60. :root {
  61. font-size: 10px;
  62. }
  63. body {
  64. font-size: 1.6rem;
  65. }
  66. .container {
  67. display: flex;
  68. flex-flow: row wrap;
  69. height: 20rem;
  70. border: 1px solid blue;
  71. /* justify-content: space-evenly; */
  72. /* justify-content: end; */
  73. /* justify-content: center; */
  74. /* align-items: center; */
  75. align-content: flex-end;
  76. align-content: flex-start;
  77. align-content: center;
  78. }
  79. .container .item {
  80. /* height: 3rem; */
  81. width: 5rem;
  82. background-color: red;
  83. }
  84. </style>
  85. </head>
  86. <body>
  87. <div class="container">
  88. <div class="item">item1</div>
  89. <div class="item">item2</div>
  90. <div class="item">item3</div>
  91. <div class="item">item5</div>
  92. <div class="item">item6</div>
  93. <div class="item">item7</div>
  94. <div class="item">item8</div>
  95. <div class="item">item9</div>
  96. <div class="item">item10</div>
  97. <div class="item">item11</div>
  98. <div class="item">item12</div>
  99. </div>
  100. </body>
  101. </html>
声明:本文内容转载自脚本之家,由网友自发贡献,版权归原作者所有,如您发现涉嫌抄袭侵权,请联系admin@php.cn 核实处理。
全部评论
文明上网理性发言,请遵守新闻评论服务协议