当前位置:   article > 正文

grid布局的最全用法_grid布局平分六分

grid布局平分六分

grid布局的类型转换

display:grid;

分列grid-template-colums:

分行grid-template-rows:

属性值:200px 200px 200px;相当于repeat(3,200px);

               100px auto 100px;(auto部分系统自动补齐)

                1fr  2fr  3fr(将行/列按照fr比例划分,总共划分六部分,按比例占用)

                repeat(4,1fr);平分成四等分

                20% 30% 50%;(按百分比划分)

                repeat(auto-fill,30%);将按30%划分,盛放不下的则不划

                100px minmax(min,max);条件允许用最大值,最小不能超过最小值

行间距        grid-row-gap: 20px;

列间距        grid-colums-gap: 20px;

复合属性        grid-gap:行间距 列间距

代码示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  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>Document</title>
  8. <style>
  9. .wrapper{
  10. margin: 60px;
  11. /* 声明一个容器 */
  12. display: grid;
  13. grid-template-columns: 200px 200px 200px;
  14. grid-template-rows: 100px 200px;
  15. grid-gap: 20px 10px;
  16. }
  17. .item{
  18. text-align: center;
  19. font-size: 200%;
  20. color: #fff;
  21. }
  22. .one{
  23. background-color:#b8e8e0 ;
  24. }
  25. .two{
  26. background-color: #8CC7B5;
  27. }
  28. .three{
  29. background-color:#efe3bf ;
  30. }
  31. .four{
  32. background-color: #BEE7E9;
  33. }
  34. .five{
  35. background-color: #E6CEAC;
  36. }
  37. .six{
  38. background-color: #ECAD9E;
  39. }
  40. </style>
  41. </head>
  42. <body>
  43. <div class="wrapper">
  44. <div class="one item">One</div>
  45. <div class="two item">Two</div>
  46. <div class="three item">Three</div>
  47. <div class="four item">Four</div>
  48. <div class="five item">Five</div>
  49. <div class="six item">Six</div>
  50. </body>
  51. </html>

效果图

 

 容器属性显示方向        grid-auto-flow: row/clumn;横/纵向显示

 项目对齐方式:

         justify-items:start/center/end;水平对齐

         align-items:start/center/end;垂直对齐

复合属性:place-items:  align-items justify-items;

格子对齐方式

        justify-content: start/center/end/space-around/space-between;水平对齐

        align-content: start/center/end/space-around/space-between;垂直对齐

复合属性:place-content:  align-content justify-content;

grid区域布局(注意区域是必须正方格子才能正常使用,若区域不让使用则用 . 代替)

grid-template-areas: 

代码示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  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>Document</title>
  8. <style>
  9. * {
  10. margin: 0;
  11. padding: 0;
  12. }
  13. #box {
  14. width: 500px;
  15. height: 500px;
  16. margin: auto;
  17. border: 1px solid black;
  18. display: grid;
  19. grid-template-areas: 'a a b'
  20. 'c f f'
  21. 'e f f';
  22. }
  23. #box .one {
  24. grid-area:a;
  25. background-color: blanchedalmond;
  26. }
  27. #box .two{
  28. grid-area:b;
  29. background-color: cadetblue;
  30. }
  31. .three{
  32. grid-area:c;
  33. background-color: chartreuse;
  34. }
  35. .four{
  36. grid-area:d;
  37. background-color: coral;
  38. }
  39. .five{
  40. grid-area: e;
  41. background-color: cornflowerblue;
  42. }
  43. .six{
  44. grid-area: f;
  45. background-color: cyan;
  46. }
  47. </style>
  48. </head>
  49. <body>
  50. <div id="box">
  51. <div class="one">1</div>
  52. <div class="two">2</div>
  53. <div class="three">3</div>
  54. <!-- <div class="four">4</div> -->
  55. <div class="five">5</div>
  56. <div class="six">6</div>
  57. <!-- <div class="one">7</div>
  58. <div class="one">8</div>
  59. <div class="one">9</div> -->
  60. </div>
  61. </body>
  62. </html>

效果图

 网格线布局

如下图所示,grid布局将方块分为 几个网格,网格边界的为网格线

 纵向网格线        grid-column-start/ grid-column-end:

                           复合属性:grid-column: 2/4;(从第2列开始到第4列)

  横向网格线        grid-row-start/ grid-row-end:

                           复合属性:grid-row: 1/2;(从第1列开始到第2列)

代码示例

  1. <!DOCTYPE html>
  2. <html lang="en">
  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>Document</title>
  8. <style>
  9. .wrapper{
  10. margin: 60px;
  11. /* 声明一个容器 */
  12. display: grid;
  13. grid-template-columns: 200px 200px 200px;
  14. grid-template-rows: 100px 200px;
  15. grid-gap: 20px 10px;
  16. }
  17. .item{
  18. text-align: center;
  19. font-size: 200%;
  20. color: #fff;
  21. }
  22. .one{
  23. background-color:#b8e8e0 ;
  24. grid-column: 2/4;
  25. grid-row: 1/2;
  26. }
  27. .two{
  28. background-color: #8CC7B5;
  29. grid-column: 1/2;
  30. grid-row: 1/3;
  31. }
  32. .three{
  33. background-color:#efe3bf ;
  34. grid-column: 2/4;
  35. grid-row: 2/3;
  36. }
  37. .four{
  38. background-color: #BEE7E9;
  39. }
  40. .five{
  41. background-color: #E6CEAC;
  42. }
  43. .six{
  44. background-color: #ECAD9E;
  45. }
  46. </style>
  47. </head>
  48. <body>
  49. <div class="wrapper">
  50. <div class="one item">One</div>
  51. <div class="two item">Two</div>
  52. <div class="three item">Three</div>
  53. <!-- <div class="four item">Four</div>
  54. <div class="five item">Five</div>
  55. <div class="six item">Six</div> -->
  56. </body>
  57. </html>

效果图

 

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

闽ICP备14008679号