登录  /  注册
博主信息
博文 26
粉丝 0
评论 0
访问量 20775
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
grid布局及简写
default
原创
1864人浏览过

grid 简单用法

  • grid-template-rows: 100px 100px 100px; 这个是grid的横向布局
  • grid-template-columns: 100px 100px 100px; 这个是grid的纵向布局
  • grid-template-columns: repeat(3,1fr); repeat(3,1fr)里面有两个参数 第一个是复制几次 第二个是占几份 当然也可以写auto 自动分配比例
  • grid-template-rows: repeat(3,1fr); 这是横向 原理同上
  • 发现一个简写办法 grid-template: repeat(3,1fr)/ repeat(3,1fr); 用斜线隔开 第一个是横向 第二个是纵向
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. .con{
  8. width: 400px;
  9. height: 400px;
  10. background: lightblue;
  11. display: grid;
  12. grid-template-rows: 100px 100px 100px;
  13. grid-template-columns: 100px 100px 100px;
  14. grid-template: repeat(3,1fr)/ repeat(3,1fr);
  15. border: 1px solid #ddd;
  16. gap: 1px 10px;
  17. }
  18. .items{
  19. border: 1px solid #ddd;
  20. background: #dddddd;
  21. }
  22. </style>
  23. </head>
  24. <body>
  25. <div class="con">
  26. <div class=" items items1">1</div>
  27. <div class=" items items2">2</div>
  28. <div class=" items items3">3</div>
  29. <div class=" items items4">4</div>
  30. <div class=" items items5">5</div>
  31. <div class=" items items6">6</div>
  32. <div class=" items items7">7</div>
  33. <div class=" items items8">8</div>
  34. <div class=" items items9">9</div>
  35. </div>
  36. </body>
  37. </html>

grid 的多参数写法

  • 这边用简写 grid-template: 100px 100px 100px / 100px 100px 100px; 可以用px写
  • grid-template: 20% 40% auto /100px auto 100px; 可以用百分比和px,auto 混搭
  • grid-template: 1fr 1fr 2fr/1fr 1fr 1fr; 按照比例写
  • grid-template:repeat(3,1fr)/repeat(3,1fr) ;可以用重复的办法写
  • grid-template:repeat(1,50px 100px 10px 30px 40px)/repeat(3,1fr) ;分组来填写
  • grid-template: repeat(2,minmax(50px ,100px)) /repeat(3,minmax(150px,1fr));弹性设置 第一个参数是最小值 第二是最大值 适用于手机
  • grid-template:repeat(auto-fill,100px)/repeat(auto-fill,100px) ;auto-fill 自动填充

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>设置单元格的数量和大小</title>
    6. <style>
    7. .con{
    8. width: 400px;
    9. height: 400px;
    10. background: darkcyan;
    11. display: grid;
    12. /*固定值*/
    13. /*grid-template-columns: 100px 100px 100px;*/
    14. /*grid-template-rows:100px 100px 100px;*/
    15. /*!*百分比*!*/
    16. /*grid-template-columns: 20% 40% auto;*/
    17. /*grid-template-rows:100px auto 100px;*/
    18. /*!*按比例*!*/
    19. /*grid-template-columns: 1fr 1fr 2fr;*/
    20. /*grid-template-rows:1fr 1fr 1fr;*/
    21. /*!*重复设置*!*/
    22. /*grid-template-columns: repeat(3,1fr) ;*/
    23. /*grid-template-rows:repeat(3,1fr);*/
    24. /*按分组来设置*/
    25. /*grid-template-columns: repeat(1,50px 100px 10px 30px 40px) ;*/
    26. /*grid-template-rows:repeat(3,1fr);*/
    27. /*弹性设置*/
    28. /*grid-template-columns:repeat(2,minmax(50px ,100px));*/
    29. /*grid-template-rows:repeat(3,minmax(150px ,1fr));*/
    30. /*自动填充 auto-fill*/
    31. grid-template-columns:repeat(auto-fill,100px);
    32. grid-template-rows:repeat(auto-fill,100px);
    33. }
    34. .item{
    35. font-size: 2rem;
    36. background: lightgreen;
    37. }
    38. </style>
    39. </head>
    40. <body>
    41. <div class="con">
    42. <div class="item">1</div>
    43. <div class="item">2</div>
    44. <div class="item">3</div>
    45. <div class="item">4</div>
    46. <div class="item">5</div>
    47. <div class="item">6</div>
    48. <div class="item">7</div>
    49. </div>
    50. </body>
    51. </html>

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

  • grid-row-start: 1; grid-row-end: 3; grid-column-end: 1;grid-column-start: 3; 操作子元素的操作办法
  • grid-row: 1/3; grid-column: 3/5;简写
  • grid-row: 3/span 2; grid-column: 1/span 2; 使用偏移量 span 是隔几个
  • grid-row-end: span 2; grid-column-end: span 2; 用偏移量 省去代码
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. .con{
  8. width: 400px;
  9. height: 400px;
  10. background: lightblue;
  11. display: grid;
  12. /*!*grid-template-rows: 100px 100px 100px;*!*/
  13. /*!*grid-template-columns: 100px 100px 100px;*!*/
  14. /*grid-template-columns: repeat(3,auto);*/
  15. /*grid-template-rows: repeat(3,1fr);*/
  16. grid-template: repeat(4,1fr)/ repeat(4,1fr);
  17. gap: 1px 1px;
  18. }
  19. .items{
  20. border: 1px solid #ddd;
  21. background: #dddddd;
  22. }
  23. .items1{
  24. background: lightgreen;
  25. grid-row-start: 1;
  26. grid-row-end: 3;
  27. grid-column-start: 1;
  28. grid-column-end: 3;
  29. }
  30. .items2{
  31. background: lightpink;
  32. grid-row: 1/3;
  33. grid-column: 3/5;
  34. }
  35. .items3{
  36. background: lightyellow;
  37. grid-row: 3/span 2;
  38. grid-column: 1/span 2;
  39. }
  40. .items4{
  41. background: lightcoral;
  42. grid-row-end: span 2;
  43. grid-column-end: span 2;
  44. }
  45. </style>
  46. </head>
  47. <body>
  48. <div class="con">
  49. <div class=" items items1">1</div>
  50. <div class=" items items2">2</div>
  51. <div class=" items items3">3</div>
  52. <div class=" items items4">4</div>
  53. <!--<div class=" items items5">5</div>-->
  54. <!--<div class=" items items6">6</div>-->
  55. <!--<div class=" items items7">7</div>-->
  56. <!--<div class=" items items8">8</div>-->
  57. <!--<div class=" items items9">9</div>-->
  58. </div>
  59. </body>
  60. </html>

设置命名网格区域

  • grid-template-areas: “header header header””left main right” “footer footer footer”;
  • 在子元素 grid-area: header; 用来命名子元素
  • 简写就是 grid-template-areas: “header header header””…” “footer footer footer”;因为在中间这个本身就是自然分配所以可以用点点点来显示然后 在子元素grid-area: left;删掉即可
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Title</title>
  6. <style>
  7. .con{
  8. width: 400px;
  9. height: 400px;
  10. background: lightblue;
  11. display: grid;
  12. grid-template: 80px auto 80px/ 40px auto 40px;
  13. grid-template-areas: "header header header"
  14. "left main right"
  15. "footer footer footer";
  16. gap: 1px 1px;
  17. }
  18. .items{
  19. border: 1px solid #ddd;
  20. background: #dddddd;
  21. }
  22. .items1{
  23. background: lightgreen;
  24. grid-area: header;
  25. }
  26. .items2{
  27. background: lightpink;
  28. grid-area: left;
  29. }
  30. .items3{
  31. background: lightyellow;
  32. grid-area: main;
  33. }
  34. .items4{
  35. background: lightcoral;
  36. grid-area:right ;
  37. }
  38. .items5{
  39. background: lavender;
  40. grid-area: footer;
  41. }
  42. </style>
  43. </head>
  44. <body>
  45. <div class="con">
  46. <div class=" items items1">1</div>
  47. <div class=" items items2">2</div>
  48. <div class=" items items3">3</div>
  49. <div class=" items items4">4</div>
  50. <div class=" items items5">5</div>
  51. <!--<div class=" items items6">6</div>-->
  52. <!--<div class=" items items7">7</div>-->
  53. <!--<div class=" items items8">8</div>-->
  54. <!--<div class=" items items9">9</div>-->
  55. </div>
  56. </body>
  57. </html>

子元素在容器中的对齐方式

  • 和felx 差不多行的使用方式
    justify-content: end;
    justify-content: start;
    justify-content: center;
    justify-content: space-between;
    align-content: end;
    align-content: start;
    align-content: center;
    align-content: space-between;

    简写方式为
    /place-content: 垂直对齐 水平对齐;/
    place-content: center start;

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>设置单元格在容器中的对齐方式</title>
    6. <style>
    7. .con{
    8. width: 400px;
    9. height: 400px;
    10. background: darkcyan;
    11. display: grid;
    12. grid-template-columns: repeat(3,50px);
    13. grid-template-rows:repeat(3,50px);
    14. justify-content: end;
    15. justify-content: start;
    16. align-content: end;
    17. align-content: start;
    18. align-content: center;
    19. justify-content: center;
    20. justify-content: space-between;
    21. align-content: space-between;
    22. grid-template-columns: repeat(3,auto);
    23. align-content: stretch;
    24. /*简写*/
    25. /*place-content: 垂直对齐 水平对齐;*/
    26. place-content: center start;
    27. }
    28. .item{
    29. font-size: 2rem;
    30. background: lightgreen;
    31. }
    32. </style>
    33. </head>
    34. <body>
    35. <div class="con">
    36. <div class="item item1">1</div>
    37. <div class="item item2">2</div>
    38. <div class="item item3">3</div>
    39. <div class="item item4">4</div>
    40. <div class="item item5">5</div>
    41. <div class="item item6">6</div>
    42. <div class="item item7">7</div>
    43. <div class="item item8">8</div>
    44. <div class="item item9">9</div>
    45. </div>
    46. </body>
    47. </html>

    子元素在容器中的对齐方式

  • place-items: center end; 操纵所有子元素的方法
  • justify-items: stretch; align-items: stretch; justify-content: start; align-items: center; 这些方法使用方式和flex一样 写法不同而已

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>设置项目在单元格中的对齐方式</title>
    6. <style>
    7. .con{
    8. width: 400px;
    9. height: 400px;
    10. background: darkcyan;
    11. display: grid;
    12. grid-template-columns: repeat(3,1fr);
    13. grid-template-rows:repeat(3,1fr);
    14. /*justify-items: stretch;*/
    15. /*align-items: stretch;*/
    16. /*justify-content: start;*/
    17. /*align-items: center;*/
    18. place-items: center end;
    19. }
    20. .item{
    21. width: 50px;
    22. height: 50px;
    23. font-size: 2rem;
    24. background: lightgreen;
    25. }
    26. </style>
    27. </head>
    28. <body>
    29. <div class="con">
    30. <div class="item item1">1</div>
    31. <div class="item item2">2</div>
    32. <div class="item item3">3</div>
    33. <div class="item item4">4</div>
    34. <div class="item item5">5</div>
    35. <div class="item item6">6</div>
    36. <div class="item item7">7</div>
    37. <div class="item item8">8</div>
    38. <div class="item item9">9</div>
    39. </div>
    40. </body>
    41. </html>

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

  • justify-self: end; align-self: center; 单独子元素的排列方式
  • place-self: center; 简写

    1. <!DOCTYPE html>
    2. <html lang="en">
    3. <head>
    4. <meta charset="UTF-8">
    5. <title>设置某个项目在单元格中的对齐方式</title>
    6. <style>
    7. .con{
    8. width: 400px;
    9. height: 400px;
    10. background: darkcyan;
    11. display: grid;
    12. grid-template-columns: repeat(3,1fr);
    13. grid-template-rows:repeat(3,1fr);
    14. }
    15. .item{
    16. width: 50px;
    17. height: 50px;
    18. font-size: 2rem;
    19. background: lightgreen;
    20. }
    21. .item5{
    22. justify-self: end;
    23. align-self: center;
    24. /*简写*/
    25. place-self: center;
    26. }
    27. </style>
    28. </head>
    29. <body>
    30. <div class="con">
    31. <div class="item item1">1</div>
    32. <div class="item item2">2</div>
    33. <div class="item item3">3</div>
    34. <div class="item item4">4</div>
    35. <div class="item item5">5</div>
    36. <div class="item item6">6</div>
    37. <div class="item item7">7</div>
    38. <div class="item item8">8</div>
    39. <div class="item item9">9</div>
    40. </div>
    41. </body>
    42. </html>

    grid的间距

  • 用gap就行很简单
    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>设置某个项目在单元格中的对齐方式</title>
    <style>

    1. .con{
    2. width: 400px;
    3. height: 400px;
    4. background: darkcyan;
    5. display: grid;
    6. grid-template-columns: repeat(3,1fr);
    7. grid-template-rows:repeat(3,1fr);
    8. /*列间距*/
    9. column-gap: 5px;
    10. /*行间距*/
    11. row-gap: 5px;
    12. /*简写*/
    13. gap: 5px 10px;
    14. /*超级简写*/
    15. gap: 5px;
    16. }
    17. .item{
    18. font-size: 2rem;
    19. background: lightgreen;
    20. }
  1. </style>

</head>

<body>

<div class="con">
<div class="item item1">1</div>
<div class="item item2">2</div>
<div class="item item3">3</div>
<div class="item item4">4</div>
<div class="item item5">5</div>
<div class="item item6">6</div>
<div class="item item7">7</div>
<div class="item item8">8</div>
<div class="item item9">9</div>
</div>
</body>
</html>

批改老师:天蓬老师天蓬老师

批改状态:合格

老师批语:grid属性太多了, 可以先从最基本的, 最常用的几个先入手
本博文版权归博主所有,转载请注明地址!如有侵权、违法,请联系admin@php.cn举报处理!
全部评论 文明上网理性发言,请遵守新闻评论服务协议
0条评论
作者最新博文
关于我们 免责申明 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习
PHP中文网抖音号
发现有趣的

Copyright 2014-2024 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号

  • 登录PHP中文网,和优秀的人一起学习!
    全站2000+教程免费学