CSS Grid
布局,是一個(gè)基于網(wǎng)格的二維布局系統(tǒng),目的是用來(lái)優(yōu)化用戶(hù)界面設(shè)計(jì)。Grid
布局的,但幸運(yùn)的是,我們可以設(shè)置Chrome、Opera或者Firefox的特殊標(biāo)志來(lái)啟用它。在Chrome或Opera中,在地址欄中輸入chrome://flags(opera://flags)
,然后將experimental web platform features
選項(xiàng)設(shè)置為enable
;在Firefox中,將layout.css.grid.enabled
選項(xiàng)設(shè)置為可用。Grid
布局,所以,現(xiàn)在完全有必要開(kāi)始學(xué)習(xí)Grid
布局。Grid
布局的資料已經(jīng)很多了,本文只是用來(lái)記錄一下Grid布局的相關(guān)屬性和用法,防止別人的網(wǎng)站在某一天掛掉。display
屬性來(lái)定義一個(gè)網(wǎng)格容器,它的grid
值決定了容器展現(xiàn)為塊級(jí)還是內(nèi)聯(lián)形式。一旦啟用網(wǎng)格容器,它的所有子元素都進(jìn)入grid
文檔流,稱(chēng)為網(wǎng)格子項(xiàng)。display: grid | inline-grid | subgrid
grid
:定義一個(gè)塊級(jí)的網(wǎng)格容器inline-grid
:定義一個(gè)內(nèi)聯(lián)的網(wǎng)格容器subgrid
:定義一個(gè)繼承其父級(jí)網(wǎng)格容器的行和列的大小的網(wǎng)格容器,它是其父級(jí)網(wǎng)格容器的一個(gè)子項(xiàng)。column, float, clear和vertical-align
對(duì)網(wǎng)格容器沒(méi)有效果。grid-template-columns: <track-size> ... | <line-name> <track-size> ...;
grid-template-rows: <track-size> ... | <line-name> <track-size> ...;
<track-size>
:定義網(wǎng)格單元的寬高,其單位可以是一個(gè)長(zhǎng)度(如px、em、rem、vw、vh)或百分比,也可以是網(wǎng)格中自由空間的份數(shù)(單位為fr)。<line-name>
:定義網(wǎng)格線(xiàn)的名稱(chēng),它不是必須值??梢砸粋€(gè)你選擇的任意名字,當(dāng)沒(méi)有顯示設(shè)定時(shí),它的名字以數(shù)字表示。.container{
grid-template-columns: 40px 50px auto 50px 40px;
grid-template-rows: 25% 100px auto; }
.container{
grid-template-columns: [first] 40px [line2] 50px [line3] auto [col4-start] 50px [five] 40px [end];
grid-template-rows: [row1-start] 25% [row1-end] 100px [third-line] auto [last-line];
}
.container{
grid-template-rows: [row1-start] 25% [row1-end row2-start] 25% [row2-end];
}
repeat()
方法來(lái)生成多個(gè)相同值:.container{
grid-template-columns: repeat(3, 20px [col-start]) 5%;
}
/* 等價(jià)于 */
.container{
grid-template-columns: 20px [col-start] 20px [col-start] 20px [col-start] 5%;
}
.container{
grid-template-columns: 1fr 1fr 1fr;
}
.container{
grid-template-columns: 1fr 50px 1fr 1fr;
}
fr
單位的總和但不包括50px。grid-area
定義一個(gè)顯式的網(wǎng)格區(qū)域。grid-template-areas
定義網(wǎng)格區(qū)域,然后使用grid-area
調(diào)用聲明好的網(wǎng)格區(qū)域名稱(chēng)來(lái)放置對(duì)應(yīng)的網(wǎng)格項(xiàng)目。grid-template-areas: "<grid-area-name> | . | none | ..." "..."
<grid-area-name>
:在grid-area
中指定的網(wǎng)格區(qū)域名字.
:一個(gè)句點(diǎn)表示一個(gè)空的網(wǎng)格單元none
:沒(méi)有網(wǎng)格區(qū)域被定義<div class="container">
<div class="item-a"></div>
<div class="item-b"></div>
<div class="item-c"></div>
<div class="item-d"></div>
</div>
.item-a{
grid-area: header;
}
.item-b{
grid-area: main;
}
.item-c{
grid-area: sidebar;
}
.item-d{
grid-area: footer;
}
.container{
width: 300px;
height:200px;
display:grid;
grid-template-columns: 1fr 1fr 1fr 1fr;
grid-template-rows: auto;
grid-template-areas: "header header header header"
"main main . sidebar"
"header footer header footer";
}
grid-column-gap: <line-size>
grid-row-gap: <line-size>
<line-size>
:長(zhǎng)度值grid-column-gap
和grid-row-gap
的簡(jiǎn)稱(chēng):grid-gap: <grid-column-gap> <grid-row-gap>
grid-row-gap
的值將和grid-column-gap
一樣。.container{
display:grid;
grid-template-columns: 100px 50px 100px;
grid-template-rows: 80px auto 80px;
grid-column-gap: 10px;
grid-row-gap: 15px;
}
align-items
則相反,是和行軸對(duì)齊),這個(gè)值對(duì)容器里面的所有網(wǎng)格子項(xiàng)都有用。justify-items: start | end | center | stretch
start
:內(nèi)容和網(wǎng)格區(qū)域的左邊對(duì)齊end
:內(nèi)容和網(wǎng)格區(qū)域的右邊對(duì)齊center
:內(nèi)容和網(wǎng)格區(qū)域的中間對(duì)齊stretch
:填充整個(gè)網(wǎng)格區(qū)域的寬度(默認(rèn)值)align-items: start | end | center | stretch;
start
:內(nèi)容和網(wǎng)格區(qū)域的頂部對(duì)齊end
:內(nèi)容和網(wǎng)格區(qū)域的底部對(duì)齊center
:內(nèi)容和網(wǎng)格區(qū)域的中間對(duì)齊stretch
:填充整個(gè)網(wǎng)格區(qū)域的高度(默認(rèn)值)justify-content: start | end | center | stretch | space-around | space-between | space-evenly ;
start
:左對(duì)齊end
:右對(duì)齊center
:居中對(duì)齊stretch
:填充網(wǎng)格容器space-around
:在每個(gè)網(wǎng)格子項(xiàng)中間放置均等的空間,在始末兩端只有一半大小space-between
:兩邊對(duì)齊,在每個(gè)網(wǎng)格子項(xiàng)中間放置均等的空間,在始末兩端沒(méi)有空間space-evenly
:網(wǎng)格間隔相等,包括始末兩端align-content: start | end | center | stretch | space-around | space-between | space-evenly
start
:頂部對(duì)齊end
:底部對(duì)齊center
:居中對(duì)齊stretch
:填充網(wǎng)格容器space-around
:在每個(gè)網(wǎng)格子項(xiàng)中間放置均等的空間,在始末兩端只有一半大小space-between
:上下對(duì)齊,在每個(gè)網(wǎng)格子項(xiàng)中間放置均等的空間,在始末兩端沒(méi)有空間space-evenly
:在每個(gè)網(wǎng)格子項(xiàng)中間放置均等的空間,包括始末兩端grid-auto-columns: <track-size>
grid-auto-rows: <track-size>
<track-size>
:可以是一個(gè)長(zhǎng)度,百分比或者是一個(gè)網(wǎng)格中自由空間的份數(shù)(通過(guò)使用fr單位).container{
grid-template-columns: 60px 60px;
grid-template-rows: 90px 90px
}
grid-column和grid-row
來(lái)定位你的網(wǎng)格子項(xiàng),就像這樣:.item-a{
grid-column: 1 / 2;
grid-row: 2 / 3;
}
.item-b{
grid-column: 5 / 6;
grid-row: 2 / 3;
}
grid-auto-columns
和grid-auto-rows
來(lái)指定這些隱式網(wǎng)格軌跡的寬度:.container{
grid-auto-columns: 60px;
}
grid-auto-flow: row | column | row dense | column dense
row
:按照行依次從左到右排列column
:按照列依次從上到下排列dense
:按先后順序排列<section class="container">
<div class="item-a">item-a</div>
<div class="item-b">item-b</div>
<div class="item-c">item-c</div>
<div class="item-d">item-d</div>
<div class="item-e">item-e</div>
</section>
grid-auto-flow:row
:.container{
display: grid;
grid-template-columns: 60px 60px 60px 60px 60px;
grid-template-rows: 30px 30px;
grid-auto-flow: row;
}
.item-a{
grid-column: 1;
grid-row: 1 / 3;
}
.item-e{
grid-column: 5;
grid-row: 1 / 3;
}
grid-auto-flow:row
,item-b、item-c和item-d在行上是從左到右排列,如下:grid-auto-flow: column
,結(jié)果如下:grid: none | <grid-template-rows> / <grid-template-columns> | <grid-auto-flow> [<grid-auto-rows> [ / <grid-auto-columns>] ];
3.1 grid-column-start/grid-column-end/grid-row-start/grid-row-end/grid-column/grid-row
通過(guò)網(wǎng)格線(xiàn)來(lái)定義網(wǎng)格項(xiàng)的位置。grid-column-start
、grid-row-start
定義網(wǎng)格項(xiàng)的開(kāi)始位置,grid-column-end
、grid-row-end
定義網(wǎng)格項(xiàng)的結(jié)束位置。
grid-column-start: <number> | <name> | span <number> | span <name> | auto ;
grid-column-end: <number> | <name> | span <number> | span <name> | auto ;
grid-row-start: <number> | <name> | span <number> | span <name> | auto ;
grid-row-end: <number> | <name> | span <number> | span <name> | auto ;
<number> | <name>
:可以是一個(gè)數(shù)字以適用被標(biāo)記了數(shù)字號(hào)的網(wǎng)格線(xiàn),或者是一個(gè)名字以適用命名了的網(wǎng)格線(xiàn)span <number>
:子項(xiàng)將跨越指定數(shù)字的網(wǎng)格軌跡span <name>
:子項(xiàng)將跨越指定名字之前的網(wǎng)格線(xiàn)auto
:自動(dòng)布局,自動(dòng)跨越或者默認(rèn)跨越一個(gè)。實(shí)例:
.item-a{
grid-column-start: 2;
grid-column-end: five;
grid-row-start: row1-start
grid-row-end: 3
}
.item-b{
grid-column-start: 1;
grid-column-end: span col4-start;
grid-row-start: 2
grid-row-end: span 2
}
grid-column是grid-column-start
和grid-column-end
的簡(jiǎn)稱(chēng);grid-row
是grid-row-start
和grid-row-end
的簡(jiǎn)稱(chēng)。
grid-column: <start-line> / <end-line> | <start-line> / span <value>;
grid-row: <start-line> / <end-line> | <start-line> / span <value>;
實(shí)例:
.item-c{
grid-column: 3 / span 2;
grid-row: third-line / 4;
}
3.2 grid-area
給網(wǎng)格子項(xiàng)取一個(gè)名字以讓它被由grid-template-areas
屬性創(chuàng)建的模板引用。同時(shí),這個(gè)屬性還可以用來(lái)更簡(jiǎn)短地表示grid-row-start+ grid-column-start + grid-row-end+ grid-column-end
。
grid-area: <name> | <row-start> / <column-start> / <row-end> / <column-end>;
<name>
:選擇的名字<row-start> / <column-start> / <row-end> / <column-end>
– 可以是網(wǎng)格線(xiàn)的數(shù)字或名字實(shí)例:
作為分配一個(gè)名字給網(wǎng)格子項(xiàng)的一種方式:
.item{
.item-d{
grid-area: header
}
作為grid-row-start+ grid-column-start + grid-row-end+ grid-column-end的一種簡(jiǎn)寫(xiě):
.item-d{
grid-area: 1 / col4-start / last-line / 6
}
3.3 justify-self/align-self
(1)justify-self
讓網(wǎng)格子項(xiàng)的內(nèi)容以列軸對(duì)齊(與之相反align-self
是跟行軸對(duì)齊),這個(gè)值可以應(yīng)用在單個(gè)網(wǎng)格子項(xiàng)的內(nèi)容中。
justify-self: start | end | center | stretch
start
– 讓內(nèi)容在網(wǎng)格區(qū)域左對(duì)齊end
– 讓內(nèi)容在網(wǎng)格區(qū)域右對(duì)齊center
– 讓內(nèi)容在網(wǎng)格區(qū)域中間對(duì)齊stretch
– 填充著呢個(gè)網(wǎng)絡(luò)區(qū)域的寬度(默認(rèn)值)
(2)align-self
讓網(wǎng)格子項(xiàng)的內(nèi)容以行軸對(duì)齊(與之相反justify-self
是跟列軸對(duì)齊),這個(gè)值可以應(yīng)用在單個(gè)網(wǎng)格子項(xiàng)的內(nèi)容中。
align-self: start | end | center | stretch
start
– 讓內(nèi)容在網(wǎng)格區(qū)域上對(duì)齊end
– 讓內(nèi)容在網(wǎng)格區(qū)域下對(duì)齊center
– 讓內(nèi)容在網(wǎng)格區(qū)域中間對(duì)齊stretch
– 填充著呢個(gè)網(wǎng)絡(luò)區(qū)域的高度(默認(rèn)值)
參數(shù)文章:
如有錯(cuò)誤,歡迎指正!
更多建議: