Blogger Information
Blog 36
fans 1
comment 0
visits 26443
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
Grid网格布局常用属性与实例演示
早晨
Original
451 people have browsed it

容器属性:

属性名称 描述
display: grid 指定一个容器采用网格布局
display: inline-grid 指定该元素为行内元素,内部采用网格布局
grid-template-columns:10px 20px 30% 列宽,每个值和每列一一对应,可用像素或百分数等
grid-template-rows:10px 20px 30% 行高,每个值和每行一一对应,可用像素或百分数等
repeat() 简化重复的值,例:grid-template-rows: repeat(3, 100px);
auto-fill 关键字 自动填充个数,例:grid-template-columns: repeat(auto-fill, 100px);
fr 关键字 比例关系,例:grid-template-columns: 1fr 1fr;
row-gap:10px 行间距
column-gap: 20px 列间距
gap: 10px 20px 行间距和列间距的合并写法
grid-auto-flow: row 排列顺序,先行后列
grid-auto-flow: row dense 排列顺序,先行后列,紧密填满
grid-auto-flow: column 排列顺序,先列后行
grid-auto-flow: column dense 排列顺序,先列后行,紧密填满

项目属性:

属性名称 描述
grid-column-start: 2 指定项目的左边框所在的垂直网格线
grid-column-end: 3 指定项目的右边框所在的垂直网格线
grid-row-start: 4 指定项目的上边框所在的水平网格线
grid-row-end: 5 指定项目的下边框所在的水平网格线
span 关键字 表示”跨越“,即左右边框(上下边框)之间跨越多少个网格,例:grid-column-start: span 2;
grid-column 属性是grid-column-startgrid-column-end的合并简写形式,例:grid-column: 1 / 2;
grid-row 属性是grid-row-start属性和grid-row-end的合并简写形式,例:grid-row: 1 / 3;
grid-area 属性指定项目放在哪一个区域,例:grid-area: e;

代码:

  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. <style>
  10. .main{
  11. width: 30em;
  12. height: 30em;
  13. background-color: rgb(190, 190, 190);
  14. padding: 0;
  15. display: grid;
  16. grid-template-rows: 10em 10em 10em;
  17. grid-template-columns: 10em 10em 10em;
  18. gap: 0.5em;
  19. place-content: center;
  20. place-items: center;
  21. }
  22. .main > .item {
  23. width: 7em;
  24. height: 7em;
  25. background-color: aqua;
  26. border: 1px solid red;
  27. padding: 1px;
  28. }
  29. .main > .item:first-of-type{
  30. place-self: end;
  31. }
  32. </style>
  33. <body>
  34. <div class="main">
  35. <div class="item">1</div>
  36. <div class="item">2</div>
  37. <div class="item">3</div>
  38. <div class="item">4</div>
  39. <div class="item">5</div>
  40. <div class="item">6</div>
  41. <div class="item">7</div>
  42. <div class="item">8</div>
  43. <div class="item">9</div>
  44. </div>
  45. </body>
  46. </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>Grid网络容器</title>
  8. </head>
  9. <style>
  10. .main{
  11. width: 30em;
  12. height: 30em;
  13. background-color: rgb(190, 190, 190);
  14. padding: 0;
  15. display: grid;
  16. grid-template-rows: 10em 10em 10em;
  17. grid-template-columns: 10em 10em 10em;
  18. grid-template-columns: 1fr 2fr 1fr;
  19. }
  20. .main > .item {
  21. background-color: aqua;
  22. border: 1px solid red;
  23. padding: 1px;
  24. }
  25. </style>
  26. <body>
  27. <div class="main">
  28. <div class="item">1</div>
  29. <div class="item">2</div>
  30. <div class="item">3</div>
  31. </div>
  32. </body>
  33. </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