商用網站架設實務

LESSON-05 · checkpoint

CSS 基礎:盒模型與間距

主要結構已經完成,只留下本堂一個核心缺口。先預測,再從指定檔案找出它。 這頁用 可觀察證據anchor 指出下一個動作。

Starter3 個小成果
Checkpoint一個核心缺口
Solution4 個差異

開始前確認

這一頁的結果:入口頁可以開啟

工作方式:先把這一階段複製到自己的工作資料夾,開啟 files/index.html,記下修改前的畫面、DOM、Console 或 Network狀態。

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

本堂焦點:contentpaddingbordermarginbox-sizing 控制服務卡片尺寸

照這個順序做

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

CHECKPOINT-1

讓宣告的 width 包含 padding 與 border,修正 390px 水平溢位的根因。

目的

讓宣告的 width 包含 paddingborder,修正 390px 水平溢位的根因。

修改檔案

style.css

程式碼位置

.service-card box-sizing

現在要做:請在 style.css 的「.service-card box-sizing」位置完成:把 .service-cardbox-sizing 改成 border-box

貼上位置:style.css.service-card box-sizing 區塊,先保留同一檔案的其他內容。

.service-card { box-sizing: border-box; }

預期結果:390px viewport 沒有水平捲軸;Computed 顯示 box-sizing:border-box

驗收證據:Computedbox-sizingborder-box,document.documentElement.scrollWidth <= document.documentElement.clientWidth。

卡住時先看這裡

常見錯誤:只加 overflow-x:hidden 把錯誤藏起來,或修改到 page-shell 而不是 service-card。

第一個檢查:Computed 點選 .service-card,先確認 box-sizingBox Model 四層數值。

階段檔案

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

開啟 checkpoint 的可執行入口

index.html入口檔案:建立 .service-list 與三張 .service-cardHTML 結構。

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

<!doctype html>
<html lang="zh-Hant">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>CSS 基礎:盒模型與間距</title>
  <link rel="icon" href="../../../assets/favicon.svg" type="image/svg+xml">
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <header class="site-header">
    <a class="brand" href="index.html">安心維護</a>
    <span class="single-page-label">本堂單頁練習</span>
  </header>
  <main class="page-shell">
    <p class="eyebrow">LESSON-05 · 用 content、padding、border、margin 與 box-sizing 控制服務卡片尺寸</p>
    <h1>CSS 基礎:盒模型與間距</h1>
    <p class="page-description">先建立服務卡片,再觀察 padding、border 與 box-sizing 如何影響實際寬度。</p>
    <section class="service-area" aria-labelledby="services-title">
      <h2 id="services-title">服務方案</h2>
      <p>每張卡片都要有清楚的內容、內距與邊框,窄版也要保持可讀。</p>
      <div class="service-list" aria-label="服務方案">
        <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.css呈現層:逐步觀察 contentpaddingbordergapbox-sizing

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

:root {
  font-family: system-ui, -apple-system, "Segoe UI", "Noto Sans TC", sans-serif;
  color: #17324d;
  background: #f5f8fb;
}
body { margin: 0; min-height: 100vh; line-height: 1.7; }
.site-header {
  width: min(100% - 2rem, 920px);
  margin: 1rem auto 0;
  padding: .75rem 0;
  display: flex;
  justify-content: space-between;
  gap: 1rem;
  align-items: center;
}
.site-header a { color: #155e75; }
.page-shell {
  box-sizing: border-box;
  width: min(100% - 2rem, 760px);
  margin: 2rem auto;
  padding: 2rem;
  border: 1px solid #cbd8e4;
  border-radius: 20px;
  background: #fff;
}
.eyebrow { color: #176b87; font-weight: 800; }
.page-description { color: #52657d; }
.service-area { margin-top: 2rem; }
.service-list { }
.service-card {
  width: 100%;
  padding: 1.25rem;
  border: 1px solid #b9cddd;
  background: #f7fbfc;
}
.service-list {
  display: grid;
  gap: 1rem;
}
/* CHECKPOINT-1:請在 style.css 的「.service-card box-sizing」位置完成:把 .service-card 的 box-sizing 改成 border-box */
/* 本堂缺口:目前 .service-card 使用 content-box,請修正為 border-box。 */
a:focus-visible { outline: 3px solid #1d4ed8; outline-offset: 3px; }
@media (max-width: 560px) {
  .site-header { align-items: flex-start; flex-direction: column; }
  .page-shell { margin: 1rem auto; padding: 1rem; }
}

完成驗收

下一步

Checkpoint 完成後,先說出你修正的原因,再前往 Solution 對照每個檔案差異。

前往 Solution,逐檔對照差異