当前位置:   article > 正文

CSS 实现平均分布,自适应换行,不限数量_css 换行均匀布局

css 换行均匀布局

 需求:内部元素大小是固定的宽高,如何均匀分布在容器中,并且换行不受影响

代码如下

  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. </head>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. list-style: none;
  13. }
  14. html,body {
  15. height: 100%;
  16. }
  17. .container {
  18. height: 100%;
  19. width: 100%;
  20. background: rgb(176, 227, 247);
  21. overflow-y: auto;
  22. }
  23. ul {
  24. display: grid;
  25. grid-gap: 50px; /* 每个网格之间的间距 */
  26. justify-content: space-evenly; /* 均匀平铺 */
  27. grid-template-columns: repeat(auto-fill, 400px); /* 自适应数量 个 宽度为400的列 */
  28. }
  29. ul li {
  30. height: 300px;
  31. background: rgb(252, 250, 251);
  32. }
  33. </style>
  34. <body>
  35. <div class="container">
  36. <ul>
  37. <li></li>
  38. <li></li>
  39. <li></li>
  40. <li></li>
  41. <li></li>
  42. <li></li>
  43. <li></li>
  44. <li></li>
  45. <li></li>
  46. <li></li>
  47. <li></li>
  48. <li></li>
  49. <li></li>
  50. <li></li>
  51. </ul>
  52. </div>
  53. </body>
  54. </html>

 关键点:

  1. ul {
  2. display: grid;
  3. grid-gap: 50px; /* 每个网格之间的间距 */
  4. justify-content: space-evenly; /* 均匀平铺 */
  5. grid-template-columns: repeat(auto-fill, 400px); /* 自适应数量 个 宽度为400的列 */
  6. }

使用grid布局,让内部元素在横向自动分配数量,使用 justify-content: space-evenly 使元素均匀分布。

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小小林熬夜学编程/article/detail/242044
推荐阅读
相关标签
  

闽ICP备14008679号