栅格化布局

栅格布局

兼容性

可以在https://caniuse.com/open in new window网站查看,所以在根据项目使用的场景决定是否使用栅格布局。

声明容器

  1. 块级容器: display: grid
  2. 行级容器: display: inline-grid

划分行列

栅格有点类似表格,行和列。使用grid-template-columns规则可划分列数,使用grid-template-rows划分行数。

固定宽度

下面是使用固定宽度划分两行三列的的示例,当容器宽度过大时将漏白。

* {
    padding: 0;
    margin: 0;
}
body {
    padding: 200px;
}
article {
    width: 300px;
    height: 200px;
    border: solid 5px silver;
    display: grid;
    grid-template-rows: 100px 100px;
    grid-template-columns: 100px 100px 100px;
}
article div {
    background: blueviolet;
    background-clip: content-box;
    padding: 10px;
    border: solid 1px #ddd;
}
<article>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
    <div></div>
</article>

百分比

可以使用使用百分比自动适就容器。

display: grid;
grid-template-rows: 50% 50%;
grid-template-columns: 25% 25% 25% 25%;

重复设置

使用repeat统一设置值,第一个参数为重复数量,第二个参数是重复值

grid-template-rows: repeat(2, 50%);
grid-template-columns: repeat(2, 50%);

可以设置多个值来定义重复,下面定义了四列,以 100%、20px 重复排列。

display: grid;
grid-template-rows: repeat(2, 50%);
grid-template-columns: repeat(2, 100px 50px);

自动填充

自动填充是根据容器尺寸,自动设置元素尺寸。

width: 300px;
height: 200px;
display: grid;
grid-template-rows: repeat(auto-fill, 100px);
grid-template-columns: repeat(auto-fill, 100px);

比例划分

使用fr单位设置元素在空间中所占的比例,下面按1份-2份 分成两组共四列

  1. 单位组合
width: 300px;
height: 200px;
display: grid;
grid-template-rows: 1fr 2fr;
grid-template-columns: 100px 1fr 2fr;
  1. 重复定义
width: 300px;
height: 100px;
display: grid;
grid-template-rows: repeat(2, 1fr);
grid-template-columns: repeat(2, 1fr 2fr);

自动空间

第二个栅格列使用auto来让其获取所有剩余空间

display: grid;
grid-template-rows: repeat(2, 1fr);
grid-template-columns: 20vw auto 30vw;

组合定义

grid-tempalte是grid-template-rows、grid-template-columns、grid-template-areas的三个属性的简写。

display: grid;
grid-template: 10vh 20vh 10vh/ 30vw 1fr;

minmax

使用minmax方法可以设置取值范围,下列在行高在最小100px ~ 最大1fr间取值。

width: 300px;
height: 300px;
display: grid;
grid-template-rows: 100px minmax(100px, 1fr);
grid-template-columns: 100px 1fr;

间距定义

  1. 行间距

使用row-gap设置行间距。

width: 300px;
height: 200px;
display: grid;
grid-template-rows: repeat(2, 1fr);
grid-template-columns: repeat(3, 1fr);
row-gap: 30px;
  1. 列间距

使用column-gap定义列间距。

width: 300px;
height: 200px;
display: grid;
grid-template-rows: repeat(2, 1fr);
grid-template-columns: repeat(3, 1fr);
column-gap: 20px;
  1. 组合定义

使用gap规则可以一次定义行、列间距,如果间距一样可以只设置一个值。

设置行列间距为20px与10px

width: 300px;
height: 200px;
display: grid;
grid-template-rows: repeat(2, 1fr);
grid-template-columns: repeat(3, 1fr);
gap: 20px 10px;

统一设置行列间距为20px,gap: 20px;

栅格命名

栅格线可以使用命名与编号找到,方便控制指定栅格,或将内容添加到指定栅格中。

  1. 独立命名

可以为每个栅格独立命名来进行调用。

* {
    padding: 0;
    margin: 0;
}
body {
    padding-top: 50px;
}
article {
    margin: 0 auto;
    width: 300px;
    height: 300px;
    border: solid 5px silver;
    display: grid;
    grid-template-rows: [r1-start] 100px [r1-end r2-start] 100px [r2-end r3-start] 100px [r3-end];

    grid-template-columns: [c1-start] 100px [c1-end c2-start] 100px [c2-start c3-start] 100px [c3-end];
}
div {
    background: blueviolet;
    background-clip: content-box;
    border: solid 1px blueviolet;
    padding: 10px;
    box-sizing: border-box;
    color: white;
}
div:first-child {
    grid-row-start: r2-start;
    grid-column-start: c1-end;
    grid-row-end: r2-end;
    grid-column-end: c3-start;
}
<article>
	<div>mkimq</div>
</article>
  1. 自动命名

对于重复设置的栅格系统会自动命名,使用时使用 c 1、c 2 的方式定位栅格。

article {
    margin: 0 auto;
    width: 300px;
    height: 300px;
    border: solid 5px silver;
    display: grid;
    grid-template-rows: repeat(3, [r-start] 100px [r-end]);
    grid-template-columns: repeat(3, [c-start] 100px [c-end]);
}
div {
    background: blueviolet;
    background-clip: content-box;
    border: solid 1px blueviolet;
    padding: 10px;
    box-sizing: border-box;
    color: white;
}
div:first-child {
    grid-row-start: r-start 2;
    grid-column-start: c-start 2;
    grid-row-end: r-start 2;
    grid-column-e
}
<article>
	<div>mkimq</div>
</article>

元素定位

样式属性说明
grid-row-start行开始栅格线
grid-row-end行结束栅格线
grid-column-start列开始栅格线
grid-column-end列结束栅格线

上面几个样式属性可以使用以下值

属性值说明
Line栅格络
span 数值栅格包含的栅格数量
span 区域名称栅格包含到指定的区域名称
auto自动设置,默认为一个网格宽度和高度

根据栅格线

通过设置具体的第几条栅格线来设置区域位置,设置的数值可以是正数和负数。

<style>
    * {
        padding: 0;
        margin: 0;
    }

    body {
        padding-left: 200px;
        padding-top: 200px;
    }

    article {
        border: solid 5px blueviolet;
        width: 400px;
        height: 400px;
        display: grid;
        grid-template-rows: repeat(4, 1fr);
        grid-template-columns: repeat(4, 1fr);
    }

    article div {
        background: blueviolet;
        grid-row-start: 2;
        grid-row-end: 4;
        grid-column-start: 2;
        grid-column-end: 4;
        display: flex;
        justify-content: center;
        align-items: center;
        font-size: 35px;
        color: white;
    }
</style>

<article>
    <div>mkimq</div>
</article>

根据栅格命名

<style>
    article {
        margin: 0 auto;
        width: 300px;
        height: 300px;
        border: solid 5px silver;
        display: grid;
        grid-template-rows: [r1-start] 100px [r1-end r2-start] 100px [r2-end r3-start] 100px [r3-end];
        grid-template-columns: [c1-start] 100px [c1-end c2-start] 100px [c2-start c3-start] 100px [c3-end];

    }

    div {
        background: blueviolet;
        background-clip: content-box;
        border: solid 1px blueviolet;
        padding: 10px;
        box-sizing: border-box;
    }

    div:first-child {
        grid-row-start: r1-end;
        grid-column-start: c2-start;
        grid-row-end: r3-start;
        grid-column-end: c3-start;
    }
</style>

<article>
	<div>mkimq</div>
</article>

根据自动命名

对于重复设置的栅格系统会自动命名,使用时使用 c 1、c 2 的方式定位栅格。

<style>
    article {
        margin: 0 auto;
        width: 300px;
        height: 300px;
        border: solid 5px silver;
        display: grid;
        grid-template-rows: repeat(3, [r-start] 100px [r-end]);
        grid-template-columns: repeat(3, [c-start] 100px [c-end]);
    }

    div {
        background: blueviolet;
        background-clip: content-box;
        border: solid 1px blueviolet;
        padding: 10px;
        box-sizing: border-box;
        color: white;
    }

    div:first-child {
        grid-row-start: r-start 2;
        grid-column-start: c-start 2;
        grid-row-end: r-start 2;
        grid-column-end: c-end 2;
    }
</style>

<article>
	<div>mkimq</div>
</article>

根据偏移量

使用span可以设置包含栅格的数量或包含到的区域名称。

示例说明
grid-row-end:2向下包含2行
grid-row-start:2向上包含2行
grid-column-end:2向右包含2行
grid-column-start:2向左包含2行
<style>
    article {
        margin: 0 auto;
        width: 300px;
        height: 300px;
        border: solid 5px silver;
        display: grid;
        grid-template-rows: repeat(3, 1fr);
        grid-template-columns: repeat(3, 1fr);
    }

    div {
        background: blueviolet;
        background-clip: content-box;
        border: solid 1px blueviolet;
        padding: 10px;
        box-sizing: border-box;
        color: white;
        font-size: 25px;
    }

    div:first-child {
        grid-row-start: 2;
        grid-column-start: 2;
        grid-row-end: span 1;
        grid-column-end: span 1;
    }
</style>

<article>
	<div></div>
</article>

简写模式

可以使用grid-row设置行开始栅格线,使用grid-column设置结束栅格线。

grid-row: 2/4;
grid-column: 2/4;
  1. grid-area

grid-area更加简洁是同时对 grid-row 与 grid-column 属性的组合声明。

语法结构如下:

grid-row-start/grid-column-start/grid-row-end/grid-column-end
<style>
    * {
        padding: 0;
        margin: 0;
    }

    body {
        width: 100vw;
        height: 100vh;
        display: grid;
        grid-template: repeat(3, 1fr)/repeat(3, 1fr);
    }

    header {
        grid-area: 2/2/3/3;
        background: #e67e22;
    }
</style>

<body>
    <header></header>
</body>

区域定位

通过 grid-area 属性可以将元素放在指定区域中。grid-area由grid-row-start、grid-column-start、grid-row-end、grid-column-end 的简写模式。

编号定位

下例中将元素放在容器的中心位置中的栅格中

<style>
    article {
        margin: 0 auto;
        width: 400px;
        height: 400px;
        border: solid 5px silver;
        display: grid;
        grid-template-rows: repeat(4, 100px);
        grid-template-columns: repeat(4, 100px);
    }

    div {
        background: blueviolet;
        background-clip: content-box;
        padding: 10px;
        border: solid 1px blueviolet;
        font-size: 30px;
        color: white;
    }

    article div:first-child {
        grid-area: 2/2/4/4;
    }
</style>
...

<article class="container">
    <div>1</div>
</article>

命名定位

同样是上面的例子可以使用栅格线命名来附加元素。

article {
    margin: 0 auto;
    width: 400px;
    height: 400px;
    border: solid 5px silver;
    display: grid;
    grid-template-rows: repeat(auto-fill, [r] 100px);
    grid-template-columns: repeat(auto-fill, [l] 100px);
}
article div {
    background: blueviolet;
    background-clip: content-box;
    padding: 10px;
    border: solid 1px blueviolet;
    font-size: 30px;
    color: white;
}
article div:first-child {
	grid-area: r 2/l 2/r 4/l 4;
}

区域声明

区域是由多个单元格构成,使用 grid-template-areas可以定义栅格区域,并且栅格区域必须是矩形的。

区域布局

下面是使用栅格区域布局移动端页面结构

<style>
    body {
        width: 100vw;
        height: 100vh;
        display: grid;
        grid-template-rows: 80px 1fr 50px;
        grid-template-columns: 100px 1fr 50px 60px;
        grid-template-areas: "header header header header"
            "nav main main aside"
            "footer footer footer footer";
    }

    main {
        /* 完整的写法,推荐使用下面的简写方式*/
        /* grid-area: main-start/main-start/main-end/main-end; */
        grid-area: main;
        background: #E9EEEF;
    }

    header {
        background: #2EC56C;
        grid-area: header;
    }

    nav {
        background: #E1732C;
        grid-area: nav;
    }

    aside {
        grid-area: aside;
        background: #EEBC31;
    }

    footer {
        grid-area: footer;
        background: #904FA9;
    }
</style>

<body>
    <header></header>
    <nav></nav>
    <main></main>
    <aside></aside>
    <footer></footer>
</body>

简写形式

使用grid-template进行栅格划分会更简洁。

语法格式为:

grid-template:
    '栅格名称 栅格名称 栅格名称 栅格名称' 行高
    '栅格名称 栅格名称 栅格名称 栅格名称' 行高
    '栅格名称 栅格名称 栅格名称 栅格名称' 行高/列宽 列宽 列宽 列宽;

下面是使用grid-template进行简写的示例

<style>
body {
    width: 100vw;
    height: 100vh;
    display: grid;
    grid-template:
      'header header header header' 80px
      'nav main main aside' auto
      'footer footer footer footer' 50px/100px auto 50px 60px;
}

main {
    /* 完整的写法,推荐使用下面的简写方式*/
    /* grid-area: main-start/main-start/main-end/main-end; */
    grid-area: main;
    background: #e9eeef;
}

header {
    background: #2ec56c;
    grid-area: header;
}

nav {
    background: #e1732c;
    grid-area: nav;
}

aside {
    grid-area: aside;
    background: #eebc31;
}

footer {
    grid-area: footer;
    background: #904fa9;
}
</style>

<body>
    <header></header>
    <nav></nav>
    <main></main>
    <aside></aside>
    <footer></footer>
</body>

区域命名

系统会为区域自动命名,上例中的会产生 header-start 水平与垂直同名的起始区域与 header-end水平与垂直同名的区域终止。

<style>
article {
    width: 100vw;
    height: 100vh;
    display: grid;
    grid-template-rows: 80px 1fr 50px;
    grid-template-columns: 80 1fr;
    grid-template-areas: "header header header"
        "nav main main"
        "footer footer footer";
}

div {
    background: blueviolet;
    background-clip: content-box;
    border: solid 1px blueviolet;
    padding: 10px;
    box-sizing: border-box;
    color: white;
    font-size: 25px;
}

div:nth-child(1) {
    grid-area: header-start/nav-start/main-end/main-end;
}

div:nth-child(2) {
    grid-area: footer-start/footer-start/footer-end/footer-end;
}
</style>

<article>
	<div></div>
	<div></div>
</article>

区域占位

使用一个或多个连续的. 定义区域占位。

<style>
* {
    padding: 0;
    margin: 0;
}

article {
    width: 100vw;
    height: 100vh;
    display: grid;
    grid-template-rows: repeat(3, 33.3%);
    grid-template-columns: repeat(3, 33.3%);
    grid-template-areas: "top . ."
        "top . ."
        "bottom bottom bottom";
}

.top {
    background: blueviolet;
    grid-area: top;
    font-size: 35px;
    display: flex;
    justify-content: center;
    align-items: center;
    color: white;
}

.bottom {
    background: orange;
    grid-area: bottom;
    text-align: center;
    display: flex;
    justify-content: center;
    align-items: center;
    font-size: 35px;
}
</style>

<article>
    <div class="top">
        mkimq.com
    </div>
    <div class="bottom">
        mkimq
    </div>
</article>

栅格流动

在容器中设置grid-auto-flow 属性可以改变单元格排列方式。

选项说明
column按列排序
row按行排列
dense元素使用前面空余栅格(下面有示例说明)

基本使用

下例将单元按列排序流动

<style>
article {
    width: 400px;
    height: 400px;
    display: grid;
    grid-template-rows: repeat(2, 1fr);
    grid-template-columns: repeat(2, 1fr);
    border: solid 5px silver;
    grid-auto-flow: column;
}

div {
    background: blueviolet;
    background-clip: content-box;
    padding: 10px;
    font-size: 35px;
    color: white;
}
</style>

<article>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
</article>

强制填充

当元素在栅格中放不下时,将会发生换行产生留白,使用grid-auto-flow: row dense; 可以执行填充空白区域操作。

<style>
    * {
        padding: 0;
        margin: 0;
    }

    body {
        padding-left: 200px;
        padding-top: 200px;
    }

    article {
        width: 600px;
        height: 600px;
        display: grid;
        grid-template-rows: repeat(3, 200px);
        grid-template-columns: repeat(3, 200px);
        border: solid 5px silver;
        grid-auto-flow: row dense;
    }

    div {
        background: blueviolet;
        background-clip: content-box;
        padding: 10px;
        font-size: 35px;
        color: white;
    }

    article div:nth-child(1) {
        grid-column: 1/span 2;
        background: #000;
    }

    article div:nth-child(2) {
        grid-column: 2/span 1;
    }
</style>

<article>
    <div>1</div>
    <div>2</div>
    <div>3</div>
    <div>4</div>
</article>

对齐管理

可以通过属性方便的定义栅格或元素的对齐方式

选项说明对象
justify-content所有栅格在容器中的水平对齐方式,容器有额外空间时 栅格容器
align-content有栅格在容器中的垂直对齐方式,容器有额外空间时 栅格容器
align-items栅格内所有元素的垂直排列方式 栅格容器
justify-items栅格内所有元素的横向排列方式 栅格容器
align-self元素在栅格中垂直对齐方式 栅格元素
justify-self元素在栅格中水平对齐方式 栅格元素

栅格对齐

justify-content与align-content用于控制栅格的对齐方式,比如在栅格的尺寸小于容器的心里时,控制栅格的布局方式。

justify-content属性的值如下

说明
start容器左边
end容器右边
center容器中间
stretch撑满容器
space-between第一个栅格靠左边,最后一个栅格靠右边,余下元素平均分配空间
space-around每个元素两侧的间隔相等。所以,栅格之间的间隔比栅格与容器边距的间隔大一倍
space-evenly栅格间距离完全平均分配

align-content属性的值如下

说明
start容器顶边
end容器底边
center容器垂直中间
stretch撑满容器
space-between第一个栅格靠左边,最后一个栅格靠右边,余下元素平均分配空间
space-around每个元素两侧的间隔相等。所以,栅格之间的间隔比栅格与容器边距的间隔大一倍
space-evenly栅格间距离完全平均分配
border: solid 5px silver;
width: 600px;
height: 600px;
display: grid;
grid-template-columns: 200px 200px;
grid-template-rows: 200px 200px;
justify-content: space-between;
align-content: space-evenly;

下面是栅格水平与垂直居中对齐的示例

<style>
main {
    display: grid;
    width: 100vw;
    height: 100vh;
    grid-template: 10vh 20vh / 30vw auto;
    justify-content: center;
    align-content: center;
}
div {
    background: blueviolet;
    border: solid 1px #ddd;
    color: white;
    padding: 5px;
    box-sizing: border-box;
}
div:nth-child(1) {
    background-color: #3498db;
}
div:nth-child(2) {
    background-color: #f1c40f;
}
div:nth-child(3) {
    background-color: #2ecc71;
}
div:nth-child(4) {
    background-color: #9b59b6;
}
</style>
<main>
    <div href="">在线文档</div>
    <div href="">mankeung</div>
    <div href="">mkimq</div>
    <div href="">mkimq.com</div>
</main>

元素对齐

justify-items与align-items用于控制所有栅格内元素的对齐方式

justify-items用于控制元素的水平对齐方式,可用的属性值如下

说明
start元素对齐栅格的左边
end元素对齐栅格的右边
center元素对齐栅格的中间
stretch水平撑满栅格

align-items用于控制元素的垂直对齐方式,可用的属性值如下

说明
start元素对齐栅格的顶边
end元素对齐栅格的底边
center元素对齐栅格的垂直中间
stretch垂直撑满栅格

下面是将元素在所在栅格中水平、垂直居中的示例

<style>
    main {
        display: grid;
        width: 100vw;
        height: 100vh;
        grid-template: 20vh / repeat(4, 1fr);
        justify-items: center;
        align-items: center;
    }
    div {
        background-clip: content-box;
        border: solid 1px #ddd;
        color: white;
        padding: 10px;
        box-sizing: border-box;
    }
    div:nth-child(1) {
        background-color: #3498db;
    }
    div:nth-child(2) {
        background-color: #f1c40f;
    }
    div:nth-child(3) {
        background-color: #2ecc71;
    }
    div:nth-child(4) {
        background-color: #9b59b6;
    }
</style>
<main>
    <div href="">在线文档</div>
    <div href="">mankeung</div>
    <div href="">mkimq</div>
    <div href="">mkimq.com</div>
</main>

下面是所有元素在所在栅格中居中对齐的示例

<style>
    main {
        display: grid;
        width: 100vw;
        height: 100vh;
        grid-template: 50vh 1fr / 50vw 1fr;
        /* justify-content: center; */
        /* align-content: center; */
        justify-items: center;
        align-items: center;
    }
    div {
        background: blueviolet;
        border: solid 1px #ddd;
        color: white;
        padding: 5px;
        box-sizing: border-box;
    }
    div:nth-child(1) {
        background-color: #3498db;
    }
    div:nth-child(2) {
        background-color: #f1c40f;
    }
    div:nth-child(3) {
        background-color: #2ecc71;
    }
    div:nth-child(4) {
        background-color: #9b59b6;
    }
</style>
<main>
    <div href="">在线文档</div>
    <div href="">mankeung</div>
    <div href="">mkimq</div>
    <div href="">mkimq.com</div>
</main>

元素独立控制

justify-self与align-self控制单个栅格内元素的对齐方式,属性值与justify-items和align-items是一致的。

div:first-child {
    justify-self: end;
    align-self: center;
}

div:nth-child(4) {
    justify-self: start;
    align-self: center;
}

组合简写

  1. place-content

用于控制栅格的对齐方式,语法如下:

place-content: <align-content> <justify-content>
  1. place-items

控制所有元素的对齐方式,语法结构如下:

place-items: <align-items> <justify-items>
  1. place-self

控制单个元素的对齐方式

place-self: <align-self> <justify-self>

自动排列

当栅格无法放置内容时,系统会自动添加栅格用于放置溢出的元素,我们需要使用以下属性控制自动添加栅格的尺寸。

属性说明

选项说明对象
grid-auto-rows控制自动增加的栅格行的尺寸,grid-auto-flow:row;为默认容器
grid-auto-columns控制自动增加的栅格列的尺寸,grid-auto-flow: column;容器

自动栅格行

下面定义了2X2的栅格,但有多个元素,系统将自动创建栅格用于放置额外元素。我们使用grid-auto-rows来控制增加栅格的行高。

<style>
  main {
    display: grid;
    grid-template-rows: repeat(2, 50px);
    grid-template-columns: repeat(2, 1fr);
    grid-auto-rows: 50px;
    grid-auto-columns: 200px;
  }
  div {
    background: blueviolet;
    background-clip: content-box;
    border: solid 1px #ddd;
    color: white;
  }
</style>
<main>
  <div href="">我的音乐</div>
  <div href="">西方音乐</div>
  <div href="">北方音乐</div>
  <div href="">mkmq</div>
  <div href="">在线文档</div>
  <div href="">mankeung</div>
</main>

自动行列

下面创建了2X2栅格,我们将第2个DIV设置的格栅已经超过了四个栅格,所以系统会自动创建栅格。

<style>
    main {
        display: grid;
        grid-template-rows: repeat(2, 50px);
        grid-template-columns: repeat(2, 1fr);
        grid-auto-columns: 10vw;
        grid-auto-rows: 10vh;
    }
    div {
        background: blueviolet;
        background-clip: content-box;
        border: solid 1px #ddd;
        color: white;
    }
    div:nth-child(2) {
        grid-area: 5/5/5/5;
    }
</style>
<main>
    <div href="">在线文档</div>
    <div href="">mkimq</div>
</main>

终级简写

  • grid是简写属性,可以用来设置:
    • 显式网格属性 grid-template-rows、grid-template-columns和 grid-template-areas,
    • 隐式网格属性 grid-auto-rows、grid-auto-columns 和 grid-auto-flow,
    • 间距属性 grid-column-gap和 grid-row-gap

使用语法:

<'grid-template'> | <'grid-template-rows'> / [ auto-flow && dense? ] <'grid-auto-columns'>? | [ auto-flow && dense? ] <'grid-auto-rows'>? / <'grid-template-columns'>

行列划分

下面使用grid布局内容,将body容器的栅格居中排列,将main容器内的栅格内的元素居中排列。

<style>
    body {
        display: grid;
        place-content: center center;
        width: 100vw;
        height: 100vh;
    }
    main {
        display: grid;
        grid: 10vh / repeat(4, 1fr);
        place-items: center center;
    }
    div {
        background-clip: content-box;
        border: solid 1px #ddd;
        color: white;
        padding: 10px;
        box-sizing: border-box;
    }
    div:nth-child(1) {
        background-color: #3498db;
    }
    div:nth-child(2) {
        background-color: #f1c40f;
    }
    div:nth-child(3) {
        background-color: #2ecc71;
    }
    div:nth-child(4) {
        background-color: #9b59b6;
    }
</style>
<main>
    <div href="">在线文档</div>
    <div href="">mankeung</div>
    <div href="">mkimq</div>
    <div href="">mkimq.com</div>
</main>

定义区域

使用grid也可以定义栅格区域

<style>
    main {
        width: 100vw;
        height: 100vh;
        display: grid;
        grid:
        'header header' 50px
        'nav main' auto
        'footer footer' 60px/100px auto;
    }
    div {
        border: solid 1px #ddd;
        color: white;
        padding: 10px;
        box-sizing: border-box;
    }
    div:nth-child(1) {
        background-color: #3498db;
        grid-area: header;
    }
    div:nth-child(2) {
        background-color: #f1c40f;
        grid-area: nav;
    }
    div:nth-child(3) {
        background-color: #2ecc71;
        grid-area: main;
    }
    div:nth-child(4) {
        background-color: #9b59b6;
        grid-area: footer;
    }
</style>
<main>
    <div href="">在线文档</div>
    <div href="">mankeung</div>
    <div href="">mkimq</div>
    <div href="">mkimq.com</div>
</main>
贡献者: mankueng