auto-fit
與 auto-fill
的差異minmax()
實現彈性網格repeat()
自動生成欄位grid-area
命名區塊,提高可讀性repeat()
自動化產生minmax()
寬度範圍控制auto-fit
與 auto-fill
→ ✅ fit
會自動壓縮、fill
會保留空格grid-template-areas
統一規劃repeat(auto-fit, minmax())
製作自動調整的卡片排版grid-template-areas
來控制區域auto-fill
與 auto-fit
的不同效果body {
font-family: "Microsoft JhengHei", sans-serif;
background: #f4f6f8;
margin: 40px;
}
h1 {
text-align: center;
color: #34495e;
margin-bottom: 20px;
}
.grid-container {
display: grid;
grid-template-areas:
"header header"
"sidebar content"
"footer footer";
grid-template-columns: 1fr 3fr;
gap: 10px;
}
.item {
background: #3498db;
color: white;
padding: 20px;
border-radius: 8px;
font-weight: bold;
text-align: center;
}
.header { grid-area: header; }
.sidebar { grid-area: sidebar; background: #9b59b6; }
.content { grid-area: content; background: #1abc9c; }
.footer { grid-area: footer; background: #2ecc71; }
repeat()
自動化產生minmax()
寬度範圍控制auto-fit
與 auto-fill
→ ✅ fit
會自動壓縮、fill
會保留空格grid-template-areas
統一規劃repeat(auto-fit, minmax())
製作自動調整的卡片排版grid-template-areas
來控制區域auto-fill
與 auto-fit
的不同效果今天的 Grid 進階課 讓我真正體會到它的強大。以前做 RWD 時,總要靠 Media Query
一一調整區塊,現在用 auto-fit
搭配 minmax()
,就能自動伸縮,排版變得靈活又整潔。
我特別喜歡 grid-template-areas
的語法,像在畫設計稿一樣,只要命名好區域,就能清楚知道每個區塊的位置。這讓整個版面結構更直覺,也更容易維護。
相較於 Flexbox 注重「內容驅動」,Grid 更像是「版面驅動」的思維。它讓我第一次覺得排版是一種規劃的藝術,而不只是堆元素。
到這裡我終於明白,HTML 是骨架,CSS 是靈魂。
學會 Grid,就像掌握了排版世界的地圖。