我們今天要做一個常見的四區塊版型:
----------------------
| Header |
----------------------
| Sidebar | Main |
| | Content |
----------------------
| Footer |
----------------------
<body>
<div class="container">
<header class="header">Header</header>
<aside class="sidebar">Sidebar</aside>
<main class="main">Main Content</main>
<footer class="footer">Footer</footer>
</div>
</body>
/* 外層用 Grid 做主要排版 */
.container {
display: grid;
grid-template-columns: 200px 1fr; /* 左側 Sidebar 固定寬度,右側自適應 */
grid-template-rows: auto 1fr auto; /* Header & Footer 自動高度,中間填滿 */
grid-template-areas:
"header header"
"sidebar main"
"footer footer";
min-height: 100vh;
}
/* 區塊位置 */
.header {
grid-area: header;
background: #4cafef;
color: white;
padding: 20px;
font-size: 20px;
text-align: center;
}
.sidebar {
grid-area: sidebar;
background: #333;
color: white;
padding: 20px;
}
.main {
grid-area: main;
background: #f5f5f5;
padding: 20px;
}
/* Main 裡面再用 Flexbox 排內容 */
.main {
display: flex;
flex-direction: column;
gap: 15px;
}
.footer {
grid-area: footer;
background: #222;
color: white;
text-align: center;
padding: 15px;
}
@media (max-width: 768px) {
.container {
grid-template-columns: 1fr; /* 只有一欄 */
grid-template-areas:
"header"
"main"
"sidebar"
"footer";
}
}
1.把 Sidebar 改成導航列,裡面放一組連結(用 Flexbox 排成直向)。
2.在 Main 裡面放三個卡片,並用 Flexbox等間距排列。
3.嘗試在 Footer 加上三個小連結(例如隱私權、關於我們、聯絡方式),讓它置中排列。
👉下一步(Day 10),我們會進入CSS 動畫與過渡效果,讓畫面更有互動感。