关于 layout-composition
面向前端设计与开发人员的 CSS 布局技能,提供 Grid、Flexbox、Bento 等组合模式及响应式建议。还包括按内容类型选择布局和通过多视口截图检查页面的方法。
主要功能
- 01提供 Grid 与 Flexbox 布局模式
- 02构建 Bento 和非对称布局
- 03应用留白与视觉层级建议
- 04采用响应式和容器查询布局
- 05通过多视口截图检查页面
适用场景
- 01搭建仪表盘或营销页面结构
- 02为卡片列表和功能展示选择布局
- 03检查页面在移动端与桌面端的排版
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ arnavpuri/ designskills --skill layout-composition安装后在 Claude Code 中输入/layout-composition确认已加载。
详细介绍
布局与构图
使用现代 CSS 构建结构清晰、视觉效果出色的布局。
前置条件
- 阅读
.agents/design-context.md(参见design-context),了解间距系统(以 4px/8px 为基准)、圆角、风格类型和平台优先级。下方的间距令牌遵循设计系统的比例(--space-N= N × 4px);颜色使用 color-palette 中共享的--brand-*/--gray-*基础变量。 - 确定内容类型(文本密集型、媒体密集型、仪表板、营销),以选择合适的模式。
步骤 1:CSS Grid 模式
12 列网格
.grid-12 {
display: grid; grid-template-columns: repeat(12, 1fr);
gap: 1.5rem; max-width: 1280px; margin-inline: auto; padding-inline: 1.5rem;
}
.col-full { grid-column: 1 / -1; }
.col-half { grid-column: span 6; }
.col-third { grid-column: span 4; }
.col-quarter { grid-column: span 3; }
@media (max-width: 768px) { .grid-12 > * { grid-column: 1 / -1; } }
自适应网格(无需媒体查询)
.grid-auto { display: grid; grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); gap: 1.5rem; }
命名网格区域
.page-layout {
display: grid; grid-template-columns: 250px 1fr 300px;
grid-template-rows: auto 1fr auto; min-height: 100dvh;
grid-template-areas: "header header header" "sidebar main aside" "footer footer footer";
}
.header { grid-area: header; } .sidebar { grid-area: sidebar; }
.main { grid-area: main; } .aside { grid-area: aside; } .footer { grid-area: footer; }
@media (max-width: 1024px) {
.page-layout { grid-template-columns: 1fr; grid-template-areas: "header" "main" "sidebar" "aside" "footer"; }
}
步骤 2:Flexbox 布局模式
导航栏
.navbar { display: flex; align-items: center; justify-content: space-between; padding: 1rem 2rem; gap: 2rem; }
居中内容页
.centered-page { display: flex; flex-direction: column; align-items: center; min-height: 100dvh; }
.centered-page main { flex: 1; width: 100%; max-width: 65ch; padding: 2rem; }
等高卡片行
.card-row { display: flex; flex-wrap: wrap; gap: 1.5rem; }
.card-row > * { flex: 1 1 260px; display: flex; flex-direction: column; } /* wraps instead of squashing on mobile */
.card-row .card-footer { margin-top: auto; }
步骤 3:Bento 网格布局
基础 Bento
.bento { display: grid; grid-template-columns: repeat(4, 1fr); grid-auto-rows: 200px; gap: 1rem; }
.bento-large { grid-column: span 2; grid-row: span 2; }
.bento-wide { grid-column: span 2; }
.bento-tall { grid-row: span 2; }
功能展示 Bento
.bento-showcase {
display: grid; grid-template-columns: repeat(6, 1fr);
grid-template-rows: repeat(4, 180px); gap: 1rem; padding: 1rem;
}
.bento-showcase > :nth-child(1) { grid-column: 1 / 4; grid-row: 1 / 3; }
.bento-showcase > :nth-child(2) { grid-column: 4 / 7; grid-row: 1 / 2; }
.bento-showcase > :nth-child(5) { grid-column: 1 / 3; grid-row: 3 / 5; }
.bento-showcase > * { border-radius: 1rem; padding: 1.5rem; overflow: hidden; background: var(--gray-100); }
@media (max-width: 768px) {
.bento-showcase { grid-template-columns: 1fr 1fr; grid-template-rows: auto; }
.bento-showcase > * { grid-column: auto !important; grid-row: auto !important; }
}
Bento 卡片样式
.bento-card { border-radius: 1.25rem; padding: 2rem; overflow: hidden; position: relative; display: flex; flex-direction: column; justify-content: space-between; }
.bento-card--gradient { background: linear-gradient(135deg, var(--brand-100), var(--brand-50)); }
.bento-card--dark { background: var(--gray-900); color: var(--gray-50); }
步骤 4:将留白作为设计元素
间距比例
与 design-system 技能采用相同的比例:--space-N = N × 4px。如果项目中已有间距比例,请勿重新定义。
:root {
--space-1: 0.25rem; --space-2: 0.5rem; --space-3: 0.75rem; --space-4: 1rem;
--space-5: 1.25rem; --space-6: 1.5rem; --space-8: 2rem; --space-10: 2.5rem;
--space-12: 3rem; --space-16: 4rem; --space-20: 5rem; --space-24: 6rem;
}
section { padding-block: clamp(4rem, 8vw, 8rem); }
.hero { padding-block: clamp(6rem, 12vw, 12rem); }
密度指南
- 密集型 UI(仪表板):间距 4-8px,内边距 12-16px
- 标准 UI(应用):间距 8-16px,内边距 16-24px
- 营销 / 编辑内容:间距 16-32px,区块内边距 32-64px
- 奢华 / 高端:所有数值加倍
步骤 5:视觉层次技巧
按影响力从强到弱排序:
- 尺寸 —— 将最重要的元素设为最大
- 颜色与对比度 —— 高对比度能吸引视线;CTA 使用品牌色,次要内容使用柔和的灰色
- 字重与字体 —— 粗体表示重要性
- 位置 —— 左上方最先被阅读(LTR);将关键信息置于首屏
- 邻近与分组 —— 相关项目彼此靠近,不相关项目之间明确分隔
- 负空间 —— 留白能增强视觉分量
区块标题模式
.section-header { max-width: 600px; margin-inline: auto; text-align: center; margin-bottom: var(--space-16); }
.section-label { font-size: var(--text-sm); font-weight: 600; text-transform: uppercase; letter-spacing: 0.08em; color: var(--brand-600); margin-bottom: var(--space-3); } /* 600: small text needs 4.5:1 */
.section-title { font-size: var(--text-4xl); font-weight: 800; line-height: 1.1; color: var(--gray-900); margin-bottom: var(--space-4); }
.section-description { font-size: var(--text-lg); color: var(--gray-500); line-height: 1.6; }
步骤 6:阅读模式
F 型模式(文本密集型页面)
用户会先横向扫读顶部,再扫读中部的一部分,最后沿左侧向下阅读。将关键信息放在前两个段落中,以粗体关键词开启区块,并使用左对齐标题。
Z 型模式(落地页)
用户的视线顺序为:左上角 Logo > 右上角导航 > 斜向扫至中央 CTA > 底部。沿这条对角线构建落地页。
步骤 7:三分法与非对称布局
将焦点放在三分线交点上,而不是正中央。略微偏离中心的布局更具动感。
.hero-content { display: grid; grid-template-columns: 2fr 1fr; align-items: center; min-height: 80vh; }
非对称分栏(60/40)
.asymmetric { display: grid; grid-template-columns: 1.5fr 1fr; gap: 4rem; align-items: center; }
.asymmetric--reversed { grid-template-columns: 1fr 1.5fr; }
错位网格
.offset-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 2rem; }
.offset-grid > :nth-child(3n+2) { transform: translateY(3rem); }
.offset-grid { padding-bottom: 3rem; } /* transform doesn't reserve space */
@media (max-width: 768px) { .offset-grid { grid-template-columns: 1fr; } .offset-grid > * { transform: none; } }
步骤 8:响应式策略
堆叠模式(自动响应)
.layout { display: grid; grid-template-columns: repeat(auto-fit, minmax(min(100%, 350px), 1fr)); gap: 1.5rem; }
重排模式
.reflow { display: grid; grid-template-columns: repeat(3, 1fr); gap: 1.5rem; }
@media (max-width: 768px) { .reflow { grid-template-columns: repeat(2, 1fr); } }
@media (max-width: 480px) { .reflow { grid-template-columns: 1fr; } }
步骤 9:容器查询布局
.card-container { container-type: inline-size; container-name: card; }
.card { display: flex; flex-direction: column; }
@container card (min-width: 500px) {
.card { flex-direction: row; align-items: center; gap: 2rem; }
.card-image { flex: 0 0 200px; }
}
步骤 10:使用宽高比确保媒体尺寸一致
.thumbnail { aspect-ratio: 16 / 9; object-fit: cover; border-radius: 0.75rem; width: 100%; }
.avatar { aspect-ratio: 1; object-fit: cover; border-radius: 50%; }
/* Common ratios: 1:1 avatars, 4:3 cards, 16:9 video, 3:2 photos, 21:9 cinematic */
快速参考:布局选择指南
| 内容类型 | 推荐布局 |
|---|---|
| 落地页 | 单列 + 全宽区块 |
| 博客 / 文章 | 居中单列,最大宽度 65ch |
| 仪表板 | 12 列网格或侧边栏 + 主内容区 |
| 功能展示 | Bento 网格 |
| 卡片列表 | 自适应网格 |
| 作品集 | 瀑布流或错位网格 |
| 文档 | 侧边栏导航 + 居中内容 |
| 电商产品 | 非对称双列(图片 + 详情) |
验证布局
绝不要发布未经检查的布局。渲染页面,并在三种宽度下截图,然后使用 Read 查看每张图片:
for w in 375 768 1440; do
npx playwright screenshot --full-page --viewport-size=$w,900 "file://$PWD/index.html" layout-$w.png
done
检查每张截图:每个区块都有一个明确的视觉焦点;左边缘和间距一致;没有水平溢出(在浏览器中:document.documentElement.scrollWidth > innerWidth 必须为 false);移动设备上的堆叠顺序合理(DOM 顺序 = 阅读顺序;不要通过 order/网格布局重新排列视觉顺序,以免破坏键盘 Tab 顺序);网格最后一行没有孤立的单张卡片。修复问题并重新截图,直到布局整洁为止。
评论
支持 Markdown