Blogger Information
Blog 25
fans 1
comment 0
visits 12883
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
grid网格布局主要的属性详解
xueblog9的进阶之旅
Original
698 people have browsed it

grid 网格布局详解

网格布局主要的 9 个属性

属性 参考值 定义解释
display grid 声明为 grid 盒子
grid-template-columns/rows 列数/行数 划网格定义例/行数,repeat(重复次数,重复值)
gird-auto-flow column/row 默认行优先,定义交叉轴/主轴方向上排列
grid-auto-rows/columns 宽高大小 定义隐藏网格的宽高,应用场景在项目数量大于网格数量时
grid-area 行列 格式:开始行/开始列/结束行(可 span 指定)/结束列(可 span 指定),用于网格合并操作,span 指定跨越的网格数
gap 行列间距 格式顺序:行间距 列间距,间距相等时可省略一个
place-content start,end,center 父盒子的剩余空间在子盒子中的分配方式,计算好父盒子与子盒子大小(加上 gap)之后,一般用不到
place-items start,end,center 项目在网格中的分布方式,一般采用 center,居中显示
place-self start,end,center 控制单个项目的在网格中的分布方式

实例

源码

  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>grid</title>
  8. </head>
  9. <body>
  10. <div class="boxda">
  11. <div class="box">子盒子1</div>
  12. <div class="box">子盒子2</div>
  13. <div class="box">子盒子3</div>
  14. <div class="box">子盒子4</div>
  15. <div class="box">子盒子5</div>
  16. <div class="box">子盒子6</div>
  17. <div class="box">子盒子7</div>
  18. <div class="box">子盒子8</div>
  19. <div class="box">子盒子9</div>
  20. <div class="box">子盒子10</div>
  21. </div>
  22. <style>
  23. * {
  24. font-size: 14px;
  25. margin: 0;
  26. padding: 0;
  27. box-sizing: border-box;
  28. }
  29. /* 划分格子,子盒子自动排列到格子中 */
  30. body .boxda {
  31. display: grid;
  32. width: 640px;
  33. height: 660px;
  34. border: 1px solid #999;
  35. grid-template-columns: repeat(3,100px);
  36. grid-template-rows: repeat(3,100px);
  37. /* 自动为多出来的子盒子添加高宽,
  38. grid-auto-flow:row/column
  39. (横竖排列方式决定,自动生成的宽高的选项) */
  40. /* grid-auto-columns: 200px; */
  41. grid-auto-rows: 100px;
  42. place-content: space-evenly;
  43. place-items: center;
  44. /* gap:20px; gap使用需要计算好父盒子与子盒子的宽高,这边为了演示
  45. place-content,故注释掉了gap*/
  46. }
  47. body .boxda .box {
  48. border: 1px solid #888;
  49. width: 80px;
  50. height: 80px;
  51. text-decoration: #999;
  52. }
  53. body .boxda .box:nth-of-type(9) {
  54. /* span指定跨越数量之间必须带空格 */
  55. grid-area: 3 / 3 / span 2 / span 1;
  56. place-self: center end;
  57. }
  58. </style>
  59. </body>
  60. </html>
Correcting teacher:PHPzPHPz

Correction status:qualified

Teacher's comments:
Statement of this Website
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!