CSS creation date: update date:

CSS GRID 實作小抄

CSS Grid 是一種強大的二維佈局系統,能夠幫助我們更靈活的控制網頁元素的排列和大小。

它允許設計者在行(row)和列(column)上進行精確的控制,進而創建複雜的佈局,讓我們可以不需要依賴於浮動或定位等傳統技術。

這裡分享我閱讀 CSS Grid Layout Guide 這篇文章整理的 CSS GRID 實作筆記。

Table of Contents

Properties for the Parent (Grid Container)

宣告

display

網格

grid-template-columns / grid-template-rows

grid-template-areas

grid-auto-rows / grid-auto-columns

grid-auto-flow

以下舉例說明:

我們定義了一個具有五列和兩行的網格,並且將 grid-auto-flow 設置為 row(這也是預設值):

在將 items 放置到網格上時,我們只為其中兩個 items 指定了位置:

因為我們將 grid-auto-flow 設置為 row,所以網格將如下所示:

如果我們改成將 grid-auto-flow 設置為 column,則 item-b、item-c 和 item-d 將沿著列向下排列:

容器整體

align-content

justify-content

place-content

容器內

align-items

justify-items

place-items

網格間空隙

row-gap / column-gap

gap

Properties for the Children(Grid Items)

grid-column-start / grid-column-end / grid-row-start / grid-row-end

grid-column / grid-row

grid-area

align-self

justify-self

place-self

Reference

CSS Grid Layout Guide | CSS-Tricks