Blogger Information
Blog 36
fans 1
comment 0
visits 29322
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
HTML之grid布局
Jason
Original
1228 people have browsed it

grid布局

grid布局是什么?

简单的说,就是在flex布局的基础上加了一个轴,flex同时只能设置一个方向,grid布局却能同时设置两个方向,就好比我们现实生活中的坐标轴,flex布局一次只能走一条线,要么x,要么y,非常的单一,在布局一些单行或单列的布局时,非常的占优势,而grid布局,能够两条线一起走,同时进行,在对页面的整体布局时,优势就体现出来了,一个好的页面,一定是将两者合二为一,互相取长补短,才能算是一个好的前端。下面来用案例来介绍一下grid的属性。

1. 设置grid容器

  1. /* 设置显示为grad */
  2. display: grid;
  3. /* 默认显示为行 */
  4. grid-auto-flow: row;
  5. /* 修改为列显示 */
  6. grid-auto-flow: column;
  7. /* 显示为三行三列 */
  8. grid-template-columns: 100px 100px 100px;
  9. grid-template-rows: 200px 200px;
  10. /* 对于放不下的项目,会掩式生成单元格 */
  11. grid-auto-rows: auto;
  12. /* grid-auto-rows: auto; */

效果图

2.设置单元格大小

  1. /* 显示为三行三列 */
  2. grid-template-columns: 100px 100px 100px;
  3. grid-template-rows: 100px 100px 100px;
  4. /* 用百分比来设置 */
  5. grid-template-columns: 20% 30% auto;
  6. grid-template-rows: 100px auto 100px;
  7. /* 用比例来设置 */
  8. grid-template-columns: 1fr 1fr 1fr;
  9. grid-template-rows: 2fr 1fr 1fr;
  10. /* 重复设置 */
  11. grid-template-columns: repeat(3, 100px);
  12. grid-template-rows: repeat(3, 100px);
  13. /* 按分组来设置 */
  14. grid-template-columns: repeat(2, 50px 100px);
  15. /* 弹性设置大小 */
  16. grid-template-columns: repeat(3, minmax(20px,200px));
  17. grid-template-rows: repeat(3, minmax(20,200px));
  18. /* 自动填充 */
  19. grid-template-columns: repeat(auto-fill, 120px);
  20. grid-template-rows: repeat(auto-fill, 100px);

效果图

  1. ![](https://img.php.cn/upload/image/306/657/508/1586880483267108.jpg)

3.用网格线划分单元格

  1. .item.item1 {
  2. grid-row-start: 1;
  3. grid-row-end: 3;
  4. grid-column-start:1;
  5. grid-column-end: 3;
  6. grid-row-start: -1;
  7. grid-row-end: -4;
  8. grid-column-start: -1;
  9. grid-column-end: -3;
  10. grid-row-start: 2;
  11. grid-row-end: 4;
  12. grid-column-start:2;
  13. grid-column-end:4;
  14. /* 简写 */
  15. grid-row: 1 / 3;
  16. grid-column: 1 / 2;
  17. }
  18. .item.item4{
  19. background-color:maroon;
  20. grid-column: 2 / span 3;
  21. }
  22. .item.item5 {
  23. background-color:limegreen;
  24. grid-row-start: span 1;
  25. grid-column-end: span 2;
  26. }

效果图

4.网格线命名划分单元格

  1. .container {
  2. width: 400px;
  3. height: 400px;
  4. background-color: lightblue;
  5. display: grid;
  6. grid-template-columns: [c1-start] 100px [c1-end c2-start] 100px [c3-start c3-end] 100px;
  7. grid-template-rows: [r1-start] 100px [r1-end r2-start] 100px [r2-end r3-start] 100px;
  8. }
  9. .item {
  10. /* 设置字体是默认字体的两倍 */
  11. font-size: 2rem;
  12. }
  13. .item.item1 {
  14. background-color: lightpink;
  15. grid-column-start: c2-start;
  16. grid-column-end: c3-start;
  17. grid-row-start: r1-start;
  18. grid-row-end: r2-end;
  19. }
  20. .item.item3 {
  21. background-color: lightsalmon;
  22. grid-row-end: span 2;
  23. grid-column-end: span 2;
  24. }
  25. .item.item4 {
  26. background-color: lightslategray;
  27. }
  28. }

效果图

5.默认网格区域

  1. .container {
  2. /* 容器大小 */
  3. width: 400px;
  4. height: 400px;
  5. background-color: wheat;
  6. /* 创建grid容器 */
  7. display: grid;
  8. grid-template-columns: repeat(4, 1fr);
  9. grid-template-rows: repeat(4, 1fr);
  10. }
  11. .item {
  12. font-size: 2rem;
  13. }
  14. .item.item1 {
  15. background-color: lightgreen;
  16. /* grid-area: 1 / 1 / 2 / 5; */
  17. /* 用偏移量进行简化 */
  18. /* grid-area: 1 / 1 / span 1 / span 4; */
  19. /* 是从当前位置开始的填充 */
  20. grid-area: span 1 / span 4;
  21. }
  22. /* 简写 */
  23. .item.item2 {
  24. background-color: lightpink;
  25. /* grid-area: 2 / 1 / 4 / 2; */
  26. /* grid-area: span 2 / span 1; */
  27. /* 默认就是偏移一行/一列 */
  28. grid-area: span 2;
  29. }
  30. /* 使用偏移量来简化, 将第三个移动到左下角 */
  31. .item.item3 {
  32. background-color: yellow;
  33. }
  34. .item.item4 {
  35. background-color: lightgrey;
  36. /* grid-area: row-start / col-start / row-end / col-end; */
  37. }
  38. .item.item5 {
  39. background-color: violet;
  40. }

效果图

6.划分网格区域

  1. .container {
  2. /* 设置容器大小 */
  3. width: 600px;
  4. height: 600px;
  5. background-color: lightslategray;
  6. /* 创建grid容器 */
  7. display: grid;
  8. grid-template-columns: 100px 1fr 100px;
  9. grid-template-rows: 40px 1fr 40px;
  10. grid-template-areas:
  11. "header header header"
  12. "left main right"
  13. "footer footer footer";
  14. }

效果图

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

  1. .container {
  2. /* 设置容器大小 */
  3. width: 800px;
  4. height: 800px;
  5. background-color: lightslategray;
  6. /* 创建grid容器 */
  7. display: grid;
  8. grid-template-columns: repeat( 3 , 200px);
  9. grid-template-rows: repeat( 3 , 200px);
  10. /* 设置对齐方式 */
  11. justify-content: end;
  12. align-content:end;
  13. justify-content: center;
  14. align-content: center;
  15. justify-content: space-between;
  16. justify-content: space-around;
  17. justify-content: space-evenly;
  18. align-content: space-between;
  19. align-content: space-around;
  20. align-content: space-evenly;
  21. /* 垂直对齐 水平对齐 */
  22. place-content: center start;
  23. place-content: center center;
  24. place-content: center;
  25. }

效果图

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

  1. .container {
  2. /* 设置容器大小 */
  3. width: 600px;
  4. height: 600px;
  5. background-color: lightslategray;
  6. /* 创建grid容器 */
  7. display: grid;
  8. grid-template-columns: repeat(3,1fr);
  9. grid-template-rows: repeat(3,1fr);
  10. justify-items: stretch;
  11. align-items: stretch;
  12. justify-items: end;
  13. align-items: center;
  14. justify-items: center;
  15. align-items: center;
  16. /* 垂直 水平 */
  17. place-items: start end;
  18. place-items: center center;
  19. place-items: center;
  20. }

效果图

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

  1. .item {
  2. /* 设置字体是默认字体的两倍 */
  3. font-size: 2rem;
  4. }
  5. .item.item5{
  6. justify-self: end;
  7. align-self: end;
  8. place-self: center end;
  9. }

效果图

10.设置单元格间距

  1. .container {
  2. /* 设置容器大小 */
  3. width: 400px;
  4. height: 400px;
  5. background-color: lightslategray;
  6. /* 创建grid容器 */
  7. display: grid;
  8. grid-template-columns: repeat(3,1fr);
  9. grid-template-rows: repeat(3,1fr);
  10. /* 调整间距 */
  11. column-gap: 5px;
  12. row-gap: 15px;
  13. /* gap: 列间距 行间距 */
  14. gap: 15px 5px;
  15. gap: 5px;
  16. }

效果图

总结

总的来说用grid来搭建框架,非常的方便快捷,像一些市面上的网站,基本上都可以用grid布局来实现,配合着flex,两个可以说是无敌了,可以把99%的网站页面做出来,虽然只讲了grid的基本属性,但配合着案例,让我见识到了它的强大,学习了grid框架,也只是万里长征的第一步,还有更多的知识等着我去学习。去向更好出发。

Correcting teacher:天蓬老师天蓬老师

Correction status:qualified

Teacher's comments:不是99%, 是100%, 任何页面在grid面前都是浮云
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!