Communitygithub.com

responsive-layout

使用 CSS Grid、Flexbox、容器查询和流式尺寸构建适配从手机到宽屏桌面端的页面布局,包括卡片网格、Bento 网格、分栏区块、粘性侧边栏和可折叠导航。当有人说网站“在手机上坏了”、要求响应式网格、Bento 布局、移动菜单、修复水平滚动或溢出,或者正在布局任何多栏区块时,请使用此技能。

responsive-layout 是什么?

这是一个专注于 CSS 响应式布局的专业 AI 技能,适用于 Claude Code、Cursor 等 AI 代码助手。它提供了一系列经过实战验证的布局模式,包括无需断点的自适应卡片网格、文本与媒体并排的分栏区块、可堆叠的 Bento 网格、粘性侧边栏以及带无障碍支持的移动菜单。该技能的核心哲学是让布局自主适应,仅在内容真正断裂时才添加断点。它支持容器查询以实现组件级响应,并提供了详尽的溢出调试指南,包括使用 JavaScript 控制台定位溢出元素的技巧。此外,还涵盖了触控目标尺寸、视口单位、安全区域和字体缩放等移动端细节。开发人员可以直接在其项目中复用这些 CSS 模式,也可将之作为快速原型和构建健壮响应式布局的参考。技能覆盖从 320px 手机到 1920px 宽屏的测试宽度,并强调使用 em 单位定义断点以尊重用户缩放。

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

在你喜欢的 AI 中提问

打开一个已预加载此 Agent Skill 的新对话。

文档

响应式布局

编写布局使其自动适应,仅在内容真正断裂的位置添加断点。先构建桌面端再向下修补的布局是大多数移动端 bug 的根源。

基础

*, *::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; } }

每行的跨列数必须加起来等于列数,否则会出现间隙。在编写类之前先画出行的草图。

长内容旁的粘性侧边栏(常见于 FAQ、文章)

.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; } }

如果任何祖先元素设置了 overflow: hidden 或 auto,position: sticky 会静默失效。请在祖先元素上使用 overflow: clip。

头部和移动菜单

  • 桌面端:logo、内联导航、行动号召按钮。当导航不再适合时(而不是在某个任意宽度)折叠为菜单按钮。
  • 菜单按钮是真正的 <button aria-expanded aria-controls>。面板打开时捕获焦点,按下 Escape 键关闭,并恢复焦点到按钮。
  • 面板打开时锁定背景滚动(在 html 上设置 overflow: hidden),并考虑滚动条宽度以防止布局跳跃。
  • 下拉菜单在触控设备上通过点击打开;仅悬停是无法触及的。

组件级响应能力

当卡片同时出现在宽网格和窄侧边栏中时,使用容器查询使卡片响应自身的宽度:

.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 或电子邮件:使用 overflow-wrap: anywhere。
  4. 某个 flex 子项拒绝收缩:添加 min-width: 0。
  5. 在 320px 屏幕上的网格轨道使用了 minmax(20rem, 1fr):将最小值包裹在 min(100%, …) 中。
  6. 定位在视口之外的 absolute 定位装饰:使用 overflow: clip 裁剪区块。
  7. 全宽行上的负外边距。

要找到罪魁祸首,请在控制台中运行:

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

触控和视口细节

  • 触控目标至少 44x44px,彼此间距 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.

相关技能