商用網站架設實務

LESSON-09 · solution

CSS 進階:Grid 服務型商家版面骨架

這一頁不是直接複製答案;先逐檔看差異、修改原因與驗收證據,再把需要的改動帶回自己的資料夾。 這頁用 可觀察證據anchor 指出下一個動作。

Starter4 個小成果
Checkpoint一個核心缺口
Solution5 個差異

開始前確認

這一頁的結果:所有相關檔案可開啟或閱讀;沒有 TODOCHECKPOINT 或 placeholder;能說明每個檔案為何改動

工作方式:先把這一階段複製到自己的工作資料夾,開啟 files/index.html,記下修改前的畫面、Elements、Styles、Computed 與 Grid overlay狀態。

會用到的檔案:index.htmlstyle.css

本堂焦點:CSS Grid 建立可伸縮的服務型商家版面

照這個順序做

一次只做一張卡。完成後先重新整理入口,再留下證據,才進入下一張。

差異 1

先在 HTML 建立 Grid container 與 Grid item 的父子關係。

目的

先在 HTML 建立 Grid containerGrid item 的父子關係。

修改檔案

index.html

程式碼位置

service-section

現在要做:先對照差異,再檢查 在 index.html 的「service-section」對照這項變更:<section class="service-section" aria-labelledby="services-title"> <h2 id="services-title">服務方案</h2> <div class="service-grid"> <article class="service-card"><h3>到府諮詢</h3><p>先釐清需求。</p></article> <article class="service-card"><h3>定期維護</h3><p>建立服務節奏。</p></article> <article class="service-card"><h3>空間整理</h3><p>讓內容更清楚。</p></article> <article class="service-card"><h3>上線檢查</h3><p>確認頁面可用。</p></article> </div> </section>

貼上位置:index.html 的 service-section 區塊

<section class="service-section" aria-labelledby="services-title">
  <h2 id="services-title">服務方案</h2>
  <div class="service-grid">
    <article class="service-card"><h3>到府諮詢</h3><p>先釐清需求。</p></article>
    <article class="service-card"><h3>定期維護</h3><p>建立服務節奏。</p></article>
    <article class="service-card"><h3>空間整理</h3><p>讓內容更清楚。</p></article>
    <article class="service-card"><h3>上線檢查</h3><p>確認頁面可用。</p></article>
  </div>
</section>

預期結果:Elements 看到一個 service-grid 與四個直接子層 service-card。

驗收證據:ElementsDOM 階層與 Grid overlay 的 item 數量一致。

卡住時先看這裡

常見錯誤:只複製完整答案,卻沒有先理解這一項差異的責任。

第一個檢查:只對照目前差異指定的檔案與 anchor。

差異 2

先用明確的列欄規則看懂 Grid 如何排列直接子元素。

目的

先用明確的列欄規則看懂 Grid 如何排列直接子元素。

修改檔案

style.css

程式碼位置

.service-grid

現在要做:先對照差異,再檢查 在 style.css 的「.service-grid」對照這項變更:.service-grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); }

貼上位置:style.css.service-grid 區塊

.service-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
}

預期結果:桌機畫面可看到三欄,Grid overlay 顯示欄線與服務卡位置。

驗收證據:Computed 的 display:grid、grid-template-columnsGrid overlay

卡住時先看這裡

常見錯誤:只複製完整答案,卻沒有先理解這一項差異的責任。

第一個檢查:只對照目前差異指定的檔案與 anchor。

差異 3

讓欄位依可用寬度排列,並用共同規則管理卡片間距。

目的

讓欄位依可用寬度排列,並用共同規則管理卡片間距。

修改檔案

style.css

程式碼位置

.service-grid 的間距與流動欄位

現在要做:先對照差異,再檢查 在 style.css 的「.service-grid 的間距與流動欄位」對照這項變更:.service-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); gap: 1rem; }

貼上位置:style.css.service-grid 的間距與流動欄位 區塊

.service-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem;
}

預期結果:桌機有多欄;390px 會自然降成可讀的單欄,且沒有水平捲軸。

驗收證據:390px 與桌機畫面、Computed 的欄位像素值與 document 寬度比較。

卡住時先看這裡

常見錯誤:只複製完整答案,卻沒有先理解這一項差異的責任。

第一個檢查:只對照目前差異指定的檔案與 anchor。

差異 4

用 Grid item 的 placement 表達主卡片的視覺主位。

目的

Grid item 的 placement 表達主卡片的視覺主位。

修改檔案

style.css

程式碼位置

.service-card--featured

現在要做:先對照差異,再檢查 在 style.css 的「.service-card--featured」對照這項變更:.service-card--featured { grid-column: 1 / -1; }

貼上位置:style.css.service-card--featured 區塊

.service-card--featured {
  grid-column: 1 / -1;
}

預期結果:推薦卡單獨佔滿一整列,其他卡片仍由父層自動排列。

驗收證據:Grid overlay 的起始/結束欄線,以及推薦卡的畫面位置。

卡住時先看這裡

常見錯誤:只複製完整答案,卻沒有先理解這一項差異的責任。

第一個檢查:只對照目前差異指定的檔案與 anchor。

差異 5

用 390px 的實際溢位證據回到真正控制欄數的 Grid container。

目的

用 390px 的實際溢位證據回到真正控制欄數的 Grid container

修改檔案

style.css

程式碼位置

CHECKPOINT-1:.service-grid 固定欄寬

現在要做:先對照差異,再檢查 在 style.css 的「CHECKPOINT-1:.service-grid 固定欄寬」對照這項變更:.service-grid { grid-template-columns: repeat(auto-fit, minmax(220px, 1fr)); }

貼上位置:style.css 的 CHECKPOINT-1:.service-grid 固定欄寬 區塊

.service-grid {
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
}

預期結果:修正前 390px 有水平捲軸;修正後欄位可縮小或換列,scrollWidth 不大於 clientWidth。

驗收證據:修正前後記錄 scrollWidth、clientWidth、Computed grid-template-columnsGrid overlay

卡住時先看這裡

常見錯誤:只複製完整答案,卻沒有先理解這一項差異的責任。

第一個檢查:只對照目前差異指定的檔案與 anchor。

階段檔案

完整檔案收在可展開的卡片中;先完成上方微步驟,再用這裡查閱與複製。檔案位置要和目前的相對路徑一致。

開啟 solution 的可執行入口

index.html入口 HTML:建立 service-grid 與四個 service-card 的父子結構。

這段要貼在哪裡:先在自己的工作資料夾建立同名相對位置,再複製內容。

<!doctype html>
<html lang="zh-Hant">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" href="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 16 16'%3E%3Crect width='16' height='16' rx='4' fill='%230f4c5c'/%3E%3C/svg%3E" type="image/svg+xml">
  <title>LESSON-09|CSS Grid 服務卡|solution</title>
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <main class="page-shell">
    <p class="eyebrow">LESSON-09 · CSS Grid</p>
    <h1>服務型網站的二維版面</h1>
    <p class="page-description">這份頁面示範可伸縮的 Grid 欄位與推薦卡跨欄結果。</p>
    <section class="intro" aria-labelledby="goal-title">
      <h2 id="goal-title">本階段觀察</h2>
      <ul>
        <li>先找出真正控制欄數的父層 .service-grid。</li>
        <li>用 Computed 與 Grid overlay 觀察 columns、tracks、lines 與 gap。</li>
        <li>約 390px 時確認卡片可讀,而且沒有水平捲軸。</li>
      </ul>
    </section>
    <section class="service-section" aria-labelledby="services-title">
      <h2 id="services-title">服務方案</h2>
      <div class="service-grid">
      <article class="service-card service-card--featured"><h3>推薦|網站健檢</h3><p>先整理頁面結構,再找出最需要修正的使用流程。</p></article>
      <article class="service-card"><h3>內容規劃</h3><p>把商家服務整理成訪客看得懂的資訊層級。</p></article>
      <article class="service-card"><h3>版面製作</h3><p>將內容轉成可閱讀、可操作的網頁。</p></article>
      <article class="service-card"><h3>上線檢查</h3><p>確認欄位、連結與不同寬度下的版面結果。</p></article>
    </div>
    </section>
  </main>
</body>
</html>
style.cssGrid 樣式:觀察 container、tracks、gap、流動欄位與 featured item 跨欄。

這段要貼在哪裡:先在自己的工作資料夾建立同名相對位置,再複製內容。

:root { font-family: system-ui, -apple-system, "Segoe UI", "Noto Sans TC", sans-serif; color: #17324d; background: #f5f8fb; }
* { box-sizing: border-box; }
body { margin: 0; min-height: 100vh; line-height: 1.7; }
.page-shell { width: min(100% - 2rem, 760px); margin: 0 auto; padding: 2rem 0; }
.eyebrow { color: #0f766e; font-weight: 800; letter-spacing: .05em; }
.page-description { color: #52657d; }
.intro, .service-card { padding: 1rem; border: 1px solid #cbd8e4; border-radius: .75rem; background: #fff; }
.service-section { margin-top: 2rem; }
.service-card { min-width: 0; }
.service-card h3 { margin-top: 0; }
.service-card--featured { border-color: #0f766e; background: #ecfdf5; }
.service-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 1rem;
}
.service-card--featured { grid-column: 1 / -1; }

完成驗收

下一步

Solution 對照完成後,回到 README 整理證據,並準備下一堂課的先備知識。

回到 README,整理本堂證據並銜接下一堂