Grid 佈局是二維的基於網格的佈局系統,它能夠同時處理列和行(這是對比flex彈性盒模型佈局而言);第一個專門爲解決佈局問題而生的CSS模塊。瀏覽器
幾個基本概念:佈局
Grid Container,又叫作Grid容器,就是設置了display:grid的元素。flex
.main{ display:grid; }
Grid Item,又叫作Grid容器成員,Grid容器下面的直接子元素。spa
Gird Line,Grid容器行和列的網格線;它又分爲垂直網格線(column grid lines)和水平網格線(row grid lines)。code
Gird Track,兩個相鄰網格線之間的空間。blog
Grid Cell,兩個相鄰的行和兩個相鄰的列網格線之間的空間,基礎單元。it
Grid Area,四個網格線包圍的總空間,能夠由任意數量的Grid Cell組成。table
經常使用的屬性有14個:模板
.main { display: grid; } .main { display: inline-grid; }
.main{ grid-template-columns: [<Grid Line Name>] <Grid Track Size> ...; grid-template-rows: [<Grid Line Name>] <Grid Track Size> ...; }基本語法
.main { display: grid; grid-template-columns: [columns-1] 100px [columns-2] 200px auto; grid-template-rows: [rows-1] 100px [rows-2] 200px; }重複
.main { display: grid; grid-template-columns: repeat(4, 100px [columns]) auto; grid-template-rows: repeat(4, 100px [rows]); }自由空間,給定比例,自由分配空間
.main { display: grid; grid-template-columns: 100px 1fr 3fr; grid-template-rows: 100px 1fr 3fr; }
.header { grid-area: header; background: #8A469B; } .left { grid-area: left; background: #EA7F26; } .right { grid-area: right; background: #EA7F26; } .footer { grid-area: footer; background: #8A469B; } .main { height: 500px; display: grid; //縱向分紅了5個 grid-template-columns: 100px 100px auto 100px 100px; //橫向分紅3個 grid-template-rows: 100px auto 100px;; grid-template-areas: "header header header header header" "left left . . right" "footer footer footer footer footer"; }
.main { height: 500px; display: grid; grid-template: [title-left] "title title title" 80px [title-right] [content-left] "left content content" 1fr [content-right] [footer-left] "left footer footer" 80px [footer-right]/ 120px 1fr 120px; }
.main { display: grid; grid-template-columns: 100px 200px auto; grid-template-rows: 100px 200px; grid-column-gap: 20px; grid-row-gap: 20px; }
.main { display: grid; grid-template-columns: 100px 200px auto; grid-template-rows: 100px 200px; grid-gap: 20px 30px; }
.main { display: grid; justify-items: start; }
.main { display: grid; align-items: start; }
.main { display: grid; justify-content: start; }
.main { display: grid; align-content: start; }
.main { display: grid; grid-auto-columns: <Grid Track Size> ...; grid-auto-rows: <Grid Track Size> ...; }使用的狀況分爲兩種:不指定grid-auto-columns和指定grid-auto-columns。
.main { display: grid; grid-auto-flow: row; }
.main { grid: [row1-start] "header header header" 1fr [row1-end] [row2-start] "footer footer footer" 25px [row2-end]/ auto 50px auto; }
經常使用屬性有9個:容器
grid-column-start/end(根據網格線肯定Grid Item位置),它一共有四個值:number、name、span number(跨過幾條網格線)、span name(跨越到哪一根網格線)。 .main { display: grid; grid-template-columns: 100px 100px 100px; grid-template-rows: 100px 100px 100px; } .item { grid-column-start: 2; grid-column-end: span 4; }
grid-row-start/end(根據網格線肯定Grid Item位置)
.main { display: grid; grid-template-columns: 100px 100px 100px; grid-template-rows: 100px 100px 100px; } .item { grid-row-start: span 2; grid-row-end: 4; }
grid-column/row(grid-column/row-start/end簡寫)
.main { display: grid; grid-template-columns: 100px 100px 100px; grid-template-rows: 100px 100px 100px; } .item { grid-column: span 2 / 4; grid-row: 1 / 4; }
grid-area(建立區域),分爲兩種狀況:命名引用和直接定義。
命名引用 grid-area:name;
直接定義 grid-area: name | row-start | column-start | row-end | column-end
.main { display: grid; grid-template-columns: 100px 100px 100px; grid-template-rows: 100px 100px 100px; } .item { grid-area: item; grid-area: 3 / 1 / 4 / 6; }
justify-self(元素在Grid Cell橫軸上的對齊方式 - 只對單個元素),它一共有四個值:start、end、center、stretch。
.main { display: grid; grid-template-columns: 100px 100px 100px; grid-template-rows: 100px 100px 100px; } .item { justify-self: start; }
align-self(元素在Grid Cell縱軸上的對齊方式 - 只對單個元素生效),同上。
.main { display: grid; grid-template-columns: 100px 100px 100px; grid-template-rows: 100px 100px 100px; } .item { align-self: start; }