Communitygithub.com

responsive-layout

建立從手機到寬螢幕桌面皆能順暢運作的頁面佈局,利用 CSS Grid、Flexbox、容器查詢與流體尺寸,涵蓋卡片網格、Bento 網格、分割區塊、固定側邊欄及可折疊導覽列。每當有人說網站「在手機上壞掉」、要求回應式網格、Bento 佈局、行動選單、修復橫向捲軸或溢位,或是在設計任何多欄區段時,均可使用此技能。

responsive-layout 是什麼?

此技能提供一套全面且經過實戰測試的 CSS 模式,用於建立真正從手機到寬螢幕桌面都能完美適應的頁面佈局。其核心哲學是讓佈局本身具備自適應能力,僅在內容實際破壞時才加入中斷點,避免常見的「先做桌面版,再向下修補」所導致的行動端錯誤。技能包含多種現成可用的模式:無中斷點的卡片網格、輸入與媒體分割區塊、Bento 網格(附手機版中斷點)、長內容旁的固定側邊欄、以及含可折疊選單的頁首。此外,還深入說明元件層級的回應式設計(容器查詢)、以 `em` 為單位的最佳中斷點建議、橫向捲軸溢位問題的系統化除錯方法(含 JavaScript 代碼片段),以及觸控與 viewport 的細部調整。此技能適用於任何支援 CSS 的 Web 開發專案,並可透過 Claude Code、Cursor 或 Codex 等 AI 輔助工具直接套用。

相容平台~Claude Code~Codex CLI~Cursor
npx skills add https://github.com/imperfec-t/webcraft/tree/main/skills/responsive-layout

在你喜歡的 AI 中提問

開啟一個已預先載入此 Agent Skill 的新對話。

說明文件

回應式佈局

撰寫佈局時應使其能自行適應,然後只在內容真正斷掉的地方加入中斷點。先做桌面版再向下修補產生的佈局,是大多數行動端錯誤的來源。

基礎

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; }
img, video, svg { max-width: 100%; height: auto; display: block; }

.container { width: min(100% - 2 * var(--gutter, 1.25rem), var(--container, 76rem)); margin-inline: auto; }
section { padding-block: var(--section, clamp(4rem, 3rem + 6vw, 9rem)); }

模式

無中斷點的卡片網格

.grid { display: grid; gap: clamp(1rem, 2vw, 2rem);
  grid-template-columns: repeat(auto-fit, minmax(min(100%, 18rem), 1fr)); }

min(100%, 18rem) 可在容器窄於最小值時阻止溢位。當最後一排較短且不應拉伸時,請改用 auto-fill。

分割區塊(文字與媒體)

.split { display: grid; gap: clamp(2rem, 5vw, 5rem); align-items: center; }
@media (min-width: 62em) { .split { grid-template-columns: 1.1fr 0.9fr; }
  .split--flip > :first-child { order: 2; } }

在手機版上,請刻意決定哪個元素先出現;通常文字優先,除非圖片本身就是傳達的訊息。

Bento 網格

.bento { display: grid; gap: 1rem; grid-template-columns: repeat(6, 1fr); grid-auto-rows: minmax(12rem, auto); }
.bento > .wide  { grid-column: span 4; }
.bento > .half  { grid-column: span 3; }
.bento > .third { grid-column: span 2; }
.bento > .tall  { grid-row: span 2; }
@media (max-width: 61.99em) { .bento { grid-template-columns: repeat(2, 1fr); }
  .bento > * { grid-column: span 1; grid-row: auto; } .bento > .wide { grid-column: span 2; } }
@media (max-width: 35.99em) { .bento { grid-template-columns: 1fr; } .bento > * { grid-column: auto; } }

每一列的跨欄總和必須等於欄數,否則會出現間隙。在撰寫 class 之前,先草稿出每一列的佈局。

長內容旁的固定側邊欄(常見問答、文章)

.with-aside { display: grid; gap: 3rem; }
@media (min-width: 62em) { .with-aside { grid-template-columns: 22rem 1fr; align-items: start; }
  .with-aside aside { position: sticky; top: 6rem; } }

position: sticky 如果任何祖先元素設有 overflow: hidden 或 auto,則會靜默失效。請在祖先元素上改用 overflow: clip。

頁首與行動選單

  • 桌面版:Logo、行內導覽列、行動呼籲按鈕。當導覽列無法容納時折疊為選單按鈕,而非在任意寬度下折疊。
  • 選單按鈕是一個真正的 <button aria-expanded aria-controls>。面板開啟時應鎖定焦點、按下 Escape 關閉,並將焦點還原給按鈕。
  • 開啟時鎖定背景捲動(在 html 上設 overflow: hidden),並考量捲軸寬度以防止佈局跳動。
  • 下拉選單在觸控裝置上點擊開啟;單純的 hover 是無法操作的。

元件層級的回應式設計

當一個卡片同時出現在寬網格與窄側邊欄時,使用容器查詢讓卡片根據自身寬度回應:

.card-wrap { container-type: inline-size; }
@container (min-width: 28rem) { .card { display: grid; grid-template-columns: 10rem 1fr; } }

中斷點

使用 em 以尊重使用者的縮放。建議的中斷點集合:36em(576px)、48em(768px)、62em(992px)、75em(1200px)。僅當你能明確說出缺少它時會破壞什麼,才新增一個中斷點。

溢位追蹤

手機版上的橫向捲動幾乎總是下列情況之一:

  1. 元素或圖片上固定的 px 單位 width。
  2. 帶有內距的父元素內使用了 100vw(應改用 100%)。
  3. 長的未斷行文字,如 URL 或 Email:使用 overflow-wrap: anywhere。
  4. 不願縮小的 flex 子項目:加上 min-width: 0。
  5. 在 320px 螢幕上使用 minmax(20rem, 1fr) 的網格軌道:將最小值包在 min(100%, …) 中。
  6. 位於視口外部的絕對定位裝飾:使用 overflow: clip 裁剪區塊。
  7. 全寬列上的負邊距。

若要找出問題來源,請在控制台中執行:

[...document.querySelectorAll("*")].filter(e => e.getBoundingClientRect().right > innerWidth + 1)

觸控與 Viewport 細節

  • 觸控目標至少 44×44px,彼此間距 8px。
  • 全高度區塊請使用 svh/dvh 而非 vh,以免手機瀏覽器工具列裁切內容。
  • 輸入框字型大小至少 16px;更小的字型會導致 iOS 在聚焦時自動縮放。
  • 尊重瀏海區域:padding-inline: max(var(--gutter), env(safe-area-inset-left))。

測試寬度

320、390、768、1024、1440 及 1920。同時測試 200% 瀏覽器縮放,以及在導覽列中放入長翻譯字串。

Individual skills in this repo

This repo contains 4 individual skills — each has its own dedicated page.

hero-section

Design and build the hero (first screen) of a website, including headline layout, media treatment, CTA, proof badge, marquee and entrance animation. Use this whenever someone asks for a hero section, a landing page header, "the top of my homepage", a cinematic or animated intro, a video background, a text marquee, or says their first screen looks plain or generic.

homepage-blueprint

Plan the section structure and content slots of a homepage for an agency, freelancer, studio or service business before any design or code. Use this whenever someone asks "what sections should my homepage have", "plan my website", "structure a landing page for my business", wants a wireframe or sitemap for a service site, or starts building a homepage without a clear outline.

scroll-motion

Add scroll-driven and entrance animation to a website with GSAP, ScrollTrigger and Lenis, including reveals, parallax, pinned sections, horizontal scroll, line drawing, counters and preloaders. Use this whenever someone wants a site to "feel premium", asks for smooth scrolling, scroll animations, parallax, text reveals, a pinned or sticky scroll section, an animated preloader, or names GSAP, ScrollTrigger, Lenis, Locomotive or AOS.

website-workflow

Plan and sequence a whole website build, from structure through design, code, WordPress and launch, by routing each stage to the right webcraft skill. Use this when someone asks to "build me a website", "design my site from scratch", "redo our homepage", wants an end-to-end plan for a marketing, agency, studio or service site, or the request spans more than one of structure, look, layout, motion, CMS and QA.

相關技能