Grid 與 Flex 布局有一定的相似性,但是功能更加強(qiáng)大,學(xué)習(xí)起來(lái)也有不少難度,不過(guò)相信下面的內(nèi)容會(huì)幫你更快的掌握 Grid。
1. 官方定義
通過(guò)設(shè)置 display: grid;
可以定義一個(gè) CSS 網(wǎng)格。然后使用 grid-template-rows
和 grid-template-columns
屬性來(lái)定義網(wǎng)格的 columns
和 rows
。
使用這些屬性定義的網(wǎng)格被描述為 顯式網(wǎng)格 (explicit grid)。
參考文獻(xiàn):MDN
2. 慕課解釋
Grid 是一個(gè)二維網(wǎng)格布局,它有行 grid-template-rows
(橫排)、 列 grid-template-columns
(豎排),內(nèi)部的項(xiàng)目就分布在其中,而網(wǎng)格線就是行和列劃分出來(lái)的。
基本屬于解釋:
容器:上面代碼中,最外層的<div>元素demo
就是容器。
項(xiàng)目:內(nèi)層的三個(gè)<div>元素item
就是項(xiàng)目。
行:把 row
即橫向稱為行,
列:把column
即縱向稱為列。
單元格:它們的交叉區(qū)域cell
也就是單元格。
網(wǎng)格線:grid line
網(wǎng)格線就是由行和列劃分出來(lái)的。
3. 語(yǔ)法
- 塊級(jí)的網(wǎng)格。
.demo{
display:grid
}
- 內(nèi)聯(lián)級(jí)的網(wǎng)格。
.demo{
display:inline-grid;
}
容器包含屬性如下
屬性名 | 值 | 說(shuō)明 |
---|---|---|
grid-template-columns | length | 列和每列寬度 |
grid-template-rows | length | 行和每行的高度 |
grid-row-gap | length | 行和行之間的距離 |
grid-column-gap | length | 列與列之間距離 |
grid-gap | row column | 行、列間距的合并寫(xiě)法 |
grid-template-areas | string | 用來(lái)指定區(qū)域 |
grid-auto-flow | row | column | 默認(rèn)是 row ,用來(lái)指定排列優(yōu)先級(jí) |
justify-items | start | end | center | stretch | 水平方向內(nèi)容的位置 |
align-items | start | end | center | stretch | 垂直方向內(nèi)容的位置 |
place-items | align justify | 垂直和水平位置合并寫(xiě)法 |
justify-content | start | end | center | stretch | space-around | space-between | space-evenly | 水平方向整個(gè)內(nèi)容區(qū)域的位置 |
align-content | start | end | center | stretch | space-around | space-between | space-evenly | 垂直方向整個(gè)內(nèi)容區(qū)域的位置 |
place-content | align justify | 垂直和水平方向的合并寫(xiě)法 |
grid-auto-columns | length | 多于的網(wǎng)格列寬定義 |
grid-auto-rows | length | 多于的網(wǎng)格行高的定義 |
grid-template
是 grid-template-columns
、grid-template-rows
、 grid-template-areas
縮寫(xiě)。
grid
是 grid-template-rows
、grid-template-columns
、grid-template-areas
、 grid-auto-rows
、grid-auto-columns
、grid-auto-flow
的合并縮寫(xiě)。
提示:gird 屬性很復(fù)雜因此不推薦 grid
的縮寫(xiě)
項(xiàng)目包含屬性介紹
屬性名 | 值 | 說(shuō)明 |
---|---|---|
grid-column-start | number | areaName | span number | 項(xiàng)目開(kāi)始位置在左邊框所在的第幾根垂直網(wǎng)格線 |
grid-column-end | number | areaName | span number | 項(xiàng)目開(kāi)始位置在右邊框所在的第幾根垂直網(wǎng)格線 |
grid-row-start | number | areaName | span number | 項(xiàng)目開(kāi)始位置在上邊框所在的第幾根水平網(wǎng)格線 |
grid-row-end | number | areaName | span number | 項(xiàng)目開(kāi)始位置在下邊框所在的第幾根水平網(wǎng)格線 |
grid-column | number / number | grid-column-start 和 grid-column-end 的合并 |
grid-area | areaName | 指定項(xiàng)目放在哪一個(gè)區(qū)域 |
justify-self | start | end | center | stretch | 單元格內(nèi)容的水平方向位置 |
align-self | start | end | center | stretch | 單元格內(nèi)容的垂直方向位置 |
place-self | align-self justify-self | 單元格內(nèi)容的垂直和水平位置縮寫(xiě) |
4. 兼容性
IE | Edge | Firefox | Chrome | Safari | Opera | ios | android |
---|---|---|---|---|---|---|---|
No | 16+ | 52+ | 57+ | 10.1+ | 44+ | 10.3+ | 81 |
5. 實(shí)例
本小節(jié)暫時(shí)不對(duì)父容器和子容器內(nèi)的屬性進(jìn)行詳細(xì)的實(shí)例使用展示,僅對(duì) display
屬性進(jìn)行效果區(qū)分,可以從下一小節(jié)開(kāi)始其他內(nèi)容的學(xué)習(xí)。
- 創(chuàng)建一個(gè)塊級(jí)的 Gird 布局。
<div class="demo">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
通過(guò)下面的設(shè)置:
.demo{
display: grid;
grid-template-columns:100px 100px;
grid-template-rows:100px 100px;
border:1px solid #eee
}
.item:nth-of-type(1){
background: red;
}
.item:nth-of-type(2){
background: green;
}
.item:nth-of-type(3){
background: purple;
}
效果圖
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.demo{
display: grid;
grid-template-columns:100px 100px;
grid-template-rows:100px 100px;
border:1px solid #eee
}
.item:nth-of-type(1){
background: red;
}
.item:nth-of-type(2){
background: green;
}
.item:nth-of-type(3){
background: purple;
}
</style>
</head>
<body>
<div class="demo">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
慕課網(wǎng)學(xué)習(xí)
</body>
</html>
- 創(chuàng)建內(nèi)聯(lián)級(jí)的 Gird 布局。
<div class="demo">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
慕課網(wǎng)學(xué)習(xí)
.demo{
display: inline-grid;
grid-template-columns:100px 100px;
grid-template-rows:100px 100px;
border:1px solid #eee
}
.item:nth-of-type(1){
background: red;
}
.item:nth-of-type(2){
background: green;
}
.item:nth-of-type(3){
background: purple;
}
效果圖
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
.demo{
display: inline-grid;
grid-template-columns:100px 100px;
grid-template-rows:100px 100px;
border:1px solid #eee
}
.item:nth-of-type(1){
background: red;
}
.item:nth-of-type(2){
background: green;
}
.item:nth-of-type(3){
background: purple;
}
</style>
</head>
<body>
<div class="demo">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
<div class="item">4</div>
</div>
慕課網(wǎng)學(xué)習(xí)
</body>
</html>
6. 小結(jié)
- Grid 布局是二維布局原因就是項(xiàng)目所在的單元格是由行和列產(chǎn)生的。
- 網(wǎng)格線的開(kāi)始位置在容器的最頂端和最左邊。
- 使用區(qū)域命名之后會(huì)影響網(wǎng)格線的名稱會(huì)變成
區(qū)域名-star
、區(qū)域名-end
- 可以把
columns
理解為高度,rows
理解為寬度這樣便于理解。