Blogger Information
Blog 52
fans 0
comment 3
visits 42622
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
html学习:第11章 grid布局基础知识
王小飞
Original
607 people have browsed it

创建grid容器

代码:

  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>创建grid容器</title>
  7. <style>
  8. .container {
  9. /* 容器大小 */
  10. width: 500px;
  11. height: 500px;
  12. /* 背景颜色 */
  13. background-color: wheat;
  14. /* 创建grid容器 */
  15. display: grid;
  16. /* 设置项目在网格中的填充方案, 默认行优先 */
  17. grid-auto-flow: row;
  18. /* grid-auto-flow: column; 这个是列优先*/
  19. /* 显式地划分行与列, 三列二行 */
  20. /* grid-template网格模板columns列 值 第一列 第二列 第三列*/
  21. grid-template-columns: 100px 100px 100px;
  22. /* grid-template网格模板rows行 值 第一行 第二行*/
  23. grid-template-rows: 100px 100px 100px;
  24. /* 对于放置不下的项目,会隐式生成单元格 */
  25. grid-auto-rows: auto; /*默认*/
  26. grid-auto-rows: 150px; /*自定义放不下的项目*/
  27. }
  28. .item {
  29. /* 背景颜色 */
  30. background-color: lightblue;
  31. /* 文字2倍大 */
  32. font-size: 2rem;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <div class="container">
  38. <div class="item item1">1</div>
  39. <div class="item item2">2</div>
  40. <div class="item item3">3</div>
  41. <div class="item item4">4</div>
  42. <div class="item item5">5</div>
  43. <div class="item item6">6</div>
  44. <div class="item item7">7</div>
  45. <div class="item item7">8</div>
  46. <div class="item item7">9</div>
  47. </div>
  48. </body>
  49. </html>

效果:

设置单元格的数量与大小

代码

  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>设置单元格的数量与大小</title>
  7. <style>
  8. .container {
  9. /* 容器大小 */
  10. width: 400px;
  11. height: 400px;
  12. background-color: wheat;
  13. /* 创建grid容器 */
  14. display: grid;
  15. /* 固定值 */
  16. grid-template-columns: 100px 100px 100px;
  17. grid-template-rows: 100px 100px 100px;
  18. /* 百分比 */
  19. grid-template-columns: 20% 30% auto;
  20. grid-template-rows: 100px auto 100px;
  21. /* 比例 */
  22. grid-template-columns: 1fr 2fr 2fr;
  23. grid-template-rows: 1fr auto 2fr;
  24. /* 重复设置 */
  25. grid-template-columns: repeat(3, 100px);
  26. grid-template-rows: repeat(3, 100px);
  27. /* 按分组来设置: (50px-100px) */
  28. /* 50px 100px 50px 100px */
  29. grid-template-columns: repeat(2, 50px 100px);
  30. /* 弹性 */
  31. grid-template-columns: repeat(2, minmax(50px, 100px));
  32. grid-template-rows: repeat(3, minmax(150px, 1fr));
  33. /* 自动填充 */
  34. grid-template-columns: repeat(auto-fill, 120px);
  35. grid-template-rows: repeat(auto-fill, 120px);
  36. }
  37. .item {
  38. background-color: lightblue;
  39. font-size: 2rem;
  40. }
  41. </style>
  42. </head>
  43. <body>
  44. <div class="container">
  45. <div class="item item1">1</div>
  46. <div class="item item2">2</div>
  47. <div class="item item3">3</div>
  48. <div class="item item4">4</div>
  49. <div class="item item5">5</div>
  50. <div class="item item6">6</div>
  51. <div class="item item7">7</div>
  52. </div>
  53. </body>
  54. </html>

效果:

使用默认的网格线来划分单元格

代码:

  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>使用默认的网格线来划分单元格</title>
  7. <style>
  8. .container {
  9. /* 容器大小 */
  10. width: 400px;
  11. height: 400px;
  12. background-color: wheat;
  13. /* 创建grid容器 */
  14. display: grid;
  15. grid-template-columns: repeat(4, 1fr);
  16. grid-template-rows: repeat(4, 1fr);
  17. }
  18. .item {
  19. font-size: 2rem;
  20. }
  21. .item.item1 {
  22. background-color: lightgreen;
  23. grid-row-start: 1;
  24. grid-row-end: 3;
  25. grid-column-start: 1;
  26. grid-column-end: 3;
  27. }
  28. /* 简写 */
  29. .item.item2 {
  30. background-color: lightpink;
  31. /* grid-row-start: 1;
  32. grid-row-end: 3;
  33. grid-column-start: 3;
  34. grid-column-end: 5; */
  35. grid-row: 1 / 3;
  36. grid-column: 3 / 5;
  37. }
  38. /* 使用偏移量来简化, 将第三个移动到左下角 */
  39. .item.item3 {
  40. background-color: yellow;
  41. grid-row: 3 / span 2;
  42. grid-column: 1 / span 2;
  43. }
  44. .item.item4 {
  45. background-color: lightgrey;
  46. /* grid-row-start: 3; */
  47. grid-row-end: span 2;
  48. /* grid-column-start: 3; */
  49. grid-column-end: span 2;
  50. }
  51. </style>
  52. </head>
  53. <body>
  54. <div class="container">
  55. <div class="item item1">1</div>
  56. <div class="item item2">2</div>
  57. <div class="item item3">3</div>
  58. <div class="item item4">4</div>
  59. </div>
  60. </body>
  61. </html>

效果:

使用命名网格线来划分单元格

代码:

  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>使用命名网格线来划分单元格</title>
  7. <style>
  8. .container {
  9. /* 容器大小 */
  10. width: 400px;
  11. height: 400px;
  12. background-color: wheat;
  13. /* 创建grid容器 */
  14. display: grid;
  15. grid-template-columns: [c1-start] 100px [c1-end c2-start] 100px [c2-end c3-start] 100px [c3-end];
  16. grid-template-rows: [r1-start] 100px [r1-end r2-start] 100px [r2-end r3-start] 100px [r3-end];
  17. }
  18. .item {
  19. font-size: 2rem;
  20. }
  21. .item.item1 {
  22. background-color: lightgreen;
  23. /* 默认就是跨越一行/一列,所以可以省略 */
  24. grid-row-start: r2-start;
  25. grid-row-start: r1-end;
  26. grid-column-start: c3-start;
  27. }
  28. /* 简写 */
  29. .item.item2 {
  30. background-color: lightpink;
  31. /* grid-row: r1-start / r2-start;
  32. grid-column: c1-start / c3-end; */
  33. grid-column-end: span 3;
  34. }
  35. /* 使用偏移量来简化, 将第三个移动到左下角 */
  36. .item.item3 {
  37. background-color: yellow;
  38. grid-row-end: span 2;
  39. grid-column-end: span 2;
  40. }
  41. .item.item4 {
  42. background-color: lightgrey;
  43. }
  44. </style>
  45. </head>
  46. <body>
  47. <div class="container">
  48. <div class="item item1">1</div>
  49. <div class="item item2">2</div>
  50. <div class="item item3">3</div>
  51. <div class="item item4">4</div>
  52. </div>
  53. </body>
  54. </html>

效果:

默认网格区域

代码:

  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>默认网格区域</title>
  7. <style>
  8. .container {
  9. /* 容器大小 */
  10. width: 400px;
  11. height: 400px;
  12. background-color: wheat;
  13. /* 创建grid容器 */
  14. display: grid;
  15. grid-template-columns: repeat(4, 1fr);
  16. grid-template-rows: repeat(4, 1fr);
  17. }
  18. .item {
  19. font-size: 2rem;
  20. }
  21. .item.item1 {
  22. background-color: lightgreen;
  23. /* grid-area: 1 / 1 / 2 / 5; */
  24. /* 用偏移量进行简化 */
  25. /* grid-area: 1 / 1 / span 1 / span 4; */
  26. /* 是从当前位置开始的填充 */
  27. grid-area: span 1 / span 4;
  28. }
  29. /* 简写 */
  30. .item.item2 {
  31. background-color: lightpink;
  32. /* grid-area: 2 / 1 / 4 / 2; */
  33. /* grid-area: span 2 / span 1; */
  34. /* 默认就是偏移一行/一列 */
  35. grid-area: span 2;
  36. }
  37. /* 使用偏移量来简化, 将第三个移动到左下角 */
  38. .item.item3 {
  39. background-color: yellow;
  40. }
  41. .item.item4 {
  42. background-color: lightgrey;
  43. /* grid-area: row-start / col-start / row-end / col-end; */
  44. }
  45. .item.item5 {
  46. background-color: violet;
  47. }
  48. </style>
  49. </head>
  50. <body>
  51. <div class="container">
  52. <div class="item item1">1</div>
  53. <div class="item item2">2</div>
  54. <div class="item item3">3</div>
  55. <div class="item item4">4</div>
  56. <div class="item item5">5</div>
  57. </div>
  58. </body>
  59. </html>

效果:

命名网格区域

代码:

  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>命名网格区域</title>
  7. <style>
  8. .container {
  9. /* 容器大小 */
  10. width: 400px;
  11. height: 400px;
  12. background-color: wheat;
  13. /* 创建grid容器 */
  14. display: grid;
  15. grid-template-columns: 80px 1fr 80px;
  16. grid-template-rows: 40px 1fr 40px;
  17. /* 设置命名网格区域, 相同名称的命名区域会合并 */
  18. grid-template-areas:
  19. "hello hello hello"
  20. "left main right"
  21. "footer footer footer";
  22. }
  23. .item {
  24. font-size: 2rem;
  25. }
  26. .item.item1 {
  27. background-color: lightgreen;
  28. grid-area: hello;
  29. }
  30. /* 简写 */
  31. .item.item2 {
  32. background-color: lightpink;
  33. grid-area: left;
  34. }
  35. /* 使用偏移量来简化, 将第三个移动到左下角 */
  36. .item.item3 {
  37. background-color: yellow;
  38. grid-area: main;
  39. }
  40. .item.item4 {
  41. background-color: lightgrey;
  42. grid-area: right;
  43. }
  44. .item.item5 {
  45. background-color: violet;
  46. grid-area: footer;
  47. }
  48. </style>
  49. </head>
  50. <body>
  51. <div class="container">
  52. <div class="item item1">1</div>
  53. <div class="item item2">2</div>
  54. <div class="item item3">3</div>
  55. <div class="item item4">4</div>
  56. <div class="item item5">5</div>
  57. </div>
  58. </body>
  59. </html>

效果:

网格区域线的默认名称

代码:

  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>网格区域线的默认名称</title>
  7. <style>
  8. .container {
  9. /* 容器大小 */
  10. width: 400px;
  11. height: 400px;
  12. background-color: wheat;
  13. /* 创建grid容器 */
  14. display: grid;
  15. grid-template-columns: 80px 1fr;
  16. grid-template-rows: 40px 1fr 40px;
  17. /* 设置命名网格区域, 相同名称的命名区域会合并 */
  18. grid-template-areas:
  19. "header header"
  20. ". . "
  21. "footer footer";
  22. }
  23. .item {
  24. font-size: 2rem;
  25. }
  26. .item.item1 {
  27. background-color: lightgreen;
  28. grid-area: header-start / header-start / header-end / header-end;
  29. }
  30. /* 简写 */
  31. .item.item2 {
  32. background-color: lightpink;
  33. /* 多余 */
  34. /* grid-area: left; */
  35. }
  36. /* 使用偏移量来简化, 将第三个移动到左下角 */
  37. .item.item3 {
  38. background-color: yellow;
  39. /* grid-area: main; */
  40. }
  41. .item.item4 {
  42. background-color: lightgrey;
  43. grid-area: footer-start / footer-start / footer-end / footer-end;
  44. }
  45. </style>
  46. </head>
  47. <body>
  48. <div class="container">
  49. <div class="item item1">1</div>
  50. <div class="item item2">2</div>
  51. <div class="item item3">3</div>
  52. <div class="item item4">4</div>
  53. </div>
  54. </body>
  55. </html>

效果:

设置单元格在容器中的对齐方式

代码:

  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>设置单元格在容器中的对齐方式</title>
  7. <style>
  8. .container {
  9. /* 容器大小 */
  10. width: 400px;
  11. height: 400px;
  12. background-color: wheat;
  13. /* 创建grid容器 */
  14. display: grid;
  15. grid-template-columns: repeat(3, 50px);
  16. grid-template-rows: repeat(3, 50px);
  17. justify-content: end;
  18. align-content: end;
  19. justify-content: center;
  20. align-content: center;
  21. justify-content: space-between;
  22. justify-content: space-around;
  23. justify-content: space-evenly;
  24. align-content: space-between;
  25. align-content: space-around;
  26. align-content: space-evenly;
  27. /* justify-content: stretch;
  28. grid-template-columns: repeat(3, auto);
  29. align-content: stretch;
  30. grid-template-rows: repeat(3, 1fr); */
  31. /* place-content: 垂直对齐 水平对齐; */
  32. place-content: center start;
  33. place-content: center center;
  34. place-content: center;
  35. }
  36. .item {
  37. background-color: violet;
  38. font-size: 2rem;
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <div class="container">
  44. <div class="item item1">1</div>
  45. <div class="item item2">2</div>
  46. <div class="item item3">3</div>
  47. <div class="item item4">4</div>
  48. <div class="item item1">5</div>
  49. <div class="item item2">6</div>
  50. <div class="item item3">7</div>
  51. <div class="item item4">8</div>
  52. <div class="item item4">9</div>
  53. </div>
  54. </body>
  55. </html>

效果:

设置项目在单元格中的对齐方式

代码:

  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>设置项目在单元格中的对齐方式</title>
  7. <style>
  8. .container {
  9. /* 容器大小 */
  10. width: 400px;
  11. height: 400px;
  12. background-color: wheat;
  13. /* 创建grid容器 */
  14. display: grid;
  15. grid-template-columns: repeat(3, 1fr);
  16. grid-template-rows: repeat(3, 1fr);
  17. justify-items: stretch;
  18. align-items: stretch;
  19. justify-items: start;
  20. align-items: center;
  21. justify-items: center;
  22. align-items: center;
  23. /* place-items: 垂直 水平; */
  24. place-items: start end;
  25. place-items: center center;
  26. place-items: center;
  27. }
  28. .item {
  29. width: 50px;
  30. height: 50px;
  31. background-color: violet;
  32. font-size: 2rem;
  33. }
  34. </style>
  35. </head>
  36. <body>
  37. <div class="container">
  38. <div class="item item1">1</div>
  39. <div class="item item2">2</div>
  40. <div class="item item3">3</div>
  41. <div class="item item4">4</div>
  42. <div class="item item1">5</div>
  43. <div class="item item2">6</div>
  44. <div class="item item3">7</div>
  45. <div class="item item4">8</div>
  46. <div class="item item4">9</div>
  47. </div>
  48. </body>
  49. </html>

效果:

设置某个项目在单元格中的对齐方式

代码:

  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>设置某个项目在单元格中的对齐方式</title>
  7. <style>
  8. .container {
  9. /* 容器大小 */
  10. width: 400px;
  11. height: 400px;
  12. background-color: wheat;
  13. /* 创建grid容器 */
  14. display: grid;
  15. grid-template-columns: repeat(3, 1fr);
  16. grid-template-rows: repeat(3, 1fr);
  17. justify-items: stretch;
  18. align-items: stretch;
  19. justify-items: start;
  20. align-items: center;
  21. justify-items: center;
  22. align-items: center;
  23. /* place-items: 垂直 水平; */
  24. place-items: start end;
  25. place-items: center center;
  26. place-items: center;
  27. }
  28. .item {
  29. width: 50px;
  30. height: 50px;
  31. background-color: violet;
  32. font-size: 2rem;
  33. }
  34. .item.item5 {
  35. justify-self: end;
  36. align-self: end;
  37. place-self: center end;
  38. }
  39. </style>
  40. </head>
  41. <body>
  42. <div class="container">
  43. <div class="item item1">1</div>
  44. <div class="item item2">2</div>
  45. <div class="item item3">3</div>
  46. <div class="item item4">4</div>
  47. <div class="item item5">5</div>
  48. <div class="item item6">6</div>
  49. <div class="item item7">7</div>
  50. <div class="item item8">8</div>
  51. <div class="item item9">9</div>
  52. </div>
  53. </body>
  54. </html>

效果:

Correcting teacher:天蓬老师天蓬老师

Correction status:unqualified

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