grid网格布局

10 篇文章 0 订阅
9 篇文章 0 订阅

CSS3中的display:grid网格布局介绍

前端布局之网格gird布局(简单易懂)

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>网格gird布局</title>
    <style>
      .box {
        width: 300px;
        height: 600px;
        background-color: pink;
        display: grid;
        align-items: center;
        justify-content: space-between;
        grid-auto-flow: column;
      }
      .box > span {
        height: 100px;
        background: gray;
        text-align: center;
        font-size: 24px;
        border: 2px solid white;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <span>1</span>
      <span>2</span>
    </div>
  </body>
</html>

效果图
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>网格gird布局</title>
    <style>
      .box {
        width: 300px;
        height: 600px;
        background-color: pink;
        display: grid;
        align-content: center;
        justify-content: space-between;
        grid-auto-flow: column;
      }
      .box > span {
        height: 100px;
        background: gray;
        text-align: center;
        font-size: 24px;
        border: 2px solid white;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <span>1</span>
      <span>2</span>
    </div>
  </body>
</html>

效果图
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>网格gird布局</title>
    <style>
      .box {
        width: 300px;
        height: 600px;
        background-color: pink;
        display: grid;
        align-items: center;
        justify-content: space-between;
        grid-auto-flow: column;
        grid-template-columns: 100px 100px 100px;
        grid-template-rows: 50px 50px;
      }
      .box > span {
        background: gray;
        text-align: center;
        font-size: 24px;
        border: 2px solid white;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <span>1</span>
      <span>2</span>
      <span>3</span>
      <span>4</span>
      <span>5</span>
      <span>6</span>
    </div>
  </body>
</html>

效果图
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>网格gird布局</title>
    <style>
      .box {
        width: 300px;
        height: 600px;
        background-color: pink;
        display: grid;
        align-content: center;
        justify-content: space-between;
        grid-auto-flow: column;
        grid-template-columns: 100px 100px 100px;
        grid-template-rows: 50px 50px;
      }
      .box > span {
        background: gray;
        text-align: center;
        font-size: 24px;
        border: 2px solid white;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <span>1</span>
      <span>2</span>
      <span>3</span>
      <span>4</span>
      <span>5</span>
      <span>6</span>
    </div>
  </body>
</html>

效果图
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>网格gird布局</title>
    <style>
      .box {
        width: 300px;
        height: 600px;
        background-color: pink;
        display: grid;
        grid-auto-flow: column;
        grid-template-columns: 1fr 2fr 3fr;
        grid-template-rows: 1fr 2fr;
      }
      .box > span {
        background: gray;
        text-align: center;
        font-size: 24px;
        border: 2px solid white;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <span>1</span>
      <span>2</span>
      <span>3</span>
      <span>4</span>
      <span>5</span>
      <span>6</span>
    </div>
  </body>
</html>

效果图
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>网格gird布局</title>
    <style>
    /*写法1*/
      .box {
        width: 300px;
        display: grid;
        align-items: center;
        grid-template-columns: 100px 100px 100px;
        grid-template-rows: 100px 100px 100px;
      }
      .box > span {
        padding: 31.5px 0;
        background: gray;
        text-align: center;
        font-size: 24px;
        border: 2px solid white;
      }
      /*写法2*/
       .box {
        width: 300px;
        display: grid;
        grid-template-columns: 100px 100px 100px;
        grid-template-rows: 100px 100px 100px;
      }
      .box > span {
        background: gray;
        font-size: 24px;
        border: 2px solid white;
        display: grid;
        justify-items: center;
        align-items: center;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <span>1</span>
      <span>2</span>
      <span>3</span>
      <span>4</span>
      <span>5</span>
      <span>6</span>
    </div>
  </body>
</html>

效果图
在这里插入图片描述

.box {
        width: 300px;
        display: grid;
        grid-template-columns: 100px 100px 100px;
        grid-template-rows: 100px 100px 100px;
      }
      .box > span {
        background: gray;
        font-size: 24px;
        border: 2px solid white;
        display: grid;
        justify-content: center;
        align-content: center;
      }

效果图
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>网格gird布局</title>
    <style>
      .box {
        width: 300px;
        display: grid;
        grid-template-columns: 100px 100px 100px;
        grid-template-rows: 100px 100px 100px;
      }
      .box > span {
        background: gray;
        font-size: 24px;
        border: 2px solid white;
        display: grid;
        justify-items: center;
        align-items: center;
      }
      .item1 {
        grid-column-start: 1;
        grid-column-end: 4;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <span class="item1">1</span>
      <span class="item2">2</span>
      <span class="item3">3</span>
      <span class="item4">4</span>
      <span class="item5">5</span>
      <span class="item6">6</span>
    </div>
  </body>
</html>

效果图
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>网格gird布局</title>
    <style>
      .box {
        width: 300px;
        display: grid;
        grid-template-columns: 100px 100px 100px;
        grid-template-rows: 100px 100px 100px;
        grid-gap: 5px 20px;
      }
      .box > span {
        background: gray;
        font-size: 24px;
        border: 2px solid pink;
        display: grid;
        justify-items: center;
        align-items: center;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <span class="item1">1</span>
      <span class="item2">2</span>
      <span class="item3">3</span>
      <span class="item4">4</span>
      <span class="item5">5</span>
      <span class="item6">6</span>
    </div>
  </body>
</html>

效果图
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>网格gird布局</title>
    <style>
      .box {
        display: grid;
        grid-template-columns: repeat(12, 1fr);
        grid-template-rows: 50px 350px 50px;
        grid-gap: 5px;
        grid-template-areas:
          "h h h h h h h h h h h h"
          "m m c c c c c c c c c c"
          "f f f f f f f f f f f f";
      }
      .header {
        grid-area: h;
        background: greenyellow;
      }
      .menu {
        grid-area: m;
        background: blue;
      }
      .content {
        grid-area: c;
        background: coral;
      }
      .footer {
        grid-area: f;
        background: purple;
      }
      .box > div {
        font-size: 24px;
        text-align: center;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <div class="header">header</div>
      <div class="menu">menu</div>
      <div class="content">content</div>
      <div class="footer">footer</div>
    </div>
  </body>
</html>

效果图
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>网格gird布局</title>
    <style>
      #page {
        display: grid; /* 1.设置display为grid */
        width: 100%;
        height: 250px;
        grid-template-areas:
          "head head"
          "nav  main"
          "nav  foot"; /* 2.区域划分 当前为 三行 两列 */
        grid-template-rows: 50px 1fr 30px; /* 3.各区域 宽高设置 */
        grid-template-columns: 150px 1fr;
      }

      #page > header {
        grid-area: head; /* 4. 指定当前元素所在的区域位置, 从grid-template-areas选取值 */
        background-color: #8ca0ff;
      }

      #page > nav {
        grid-area: nav;
        background-color: #ffa08c;
      }

      #page > main {
        grid-area: main;
        background-color: #ffff64;
      }

      #page > footer {
        grid-area: foot;
        background-color: #8cffa0;
      }
    </style>
  </head>
  <body>
    <section id="page">
      <header>Header</header>
      <nav>Navigation</nav>
      <main>Main area</main>
      <footer>Footer</footer>
    </section>
  </body>
</html>

效果图
在这里插入图片描述


两种水平垂直居中对齐理解

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>网格gird布局</title>
    <style>
      .box {
        width: 300px;
        height: 600px;
        background-color: pink;
        display: grid;
        align-items: center;
        grid-auto-flow: column;
      }
      .box > span {
        height: 100px;
        background: gray;
        display: grid;
        /* 把自己当子格子看 */
        justify-items: center;
        align-items: center;
        /* 把内容当格子看 */
        /* justify-content: center;
        align-content: center; */

        font-size: 24px;
        border: 2px solid white;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <span>1</span>
      <span>2</span>
    </div>
  </body>
</html>

效果图
在这里插入图片描述

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>网格gird布局</title>
    <style>
      .box {
        width: 300px;
        height: 600px;
        background-color: pink;
        display: grid;
        align-items: center;
        grid-auto-flow: column;
      }
      .box > span {
        height: 100px;
        background: gray;
        display: grid;
        /* 把自己当子格子看 */
        /* justify-items: center;
        align-items: center; */
        /* 把内容当子格子看 */
        justify-content: center;
        align-content: center;

        font-size: 24px;
        border: 2px solid white;
      }
    </style>
  </head>
  <body>
    <div class="box">
      <span>1</span>
      <span>2</span>
    </div>
  </body>
</html>

效果图

在这里插入图片描述


grid-template-areas讲解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值