间距与对齐网格系统设计指南
导言
网格系统是现代 UI 设计的基础。合理的间距和对齐规范能够:
- 提高设计一致性
- 加快设计效率
- 改善用户体验
- 便于开发实现
本文深入讲解如何设计和实现一套完整的网格系统。
第一部分:网格系统基础
8pt 网格系统
8pt 网格是业界标准,所有间距都是 8 的倍数:
// 设计系统中的间距规范
const spacingScale = {
xs: 4, // 4px (8pt的1/2)
sm: 8, // 8px
md: 16, // 16px
lg: 24, // 24px
xl: 32, // 32px
xxl: 48, // 48px
xxxl: 64, // 64px
};
// 在 CSS 中应用
const spacing = {
margin: {
xs: '4px',
sm: '8px',
md: '16px',
lg: '24px',
},
padding: {
xs: '4px',
sm: '8px',
md: '16px',
lg: '24px',
},
};
为什么选择 8pt?
- 易于计算:所有尺寸都能整除 8
- 响应式友好:在不同屏幕上缩放流畅
- 行高一致:大多数字体在 8 的倍数下最清晰
- 团队协作:统一的间距单位便于沟通
第二部分:列网格布局
12 列网格系统
12 列网格提供了灵活性和一致性的最佳平衡:
/* 响应式 12 列网格 */
.grid-container {
display: grid;
grid-template-columns: repeat(12, 1fr);
gap: 16px;
max-width: 1200px;
margin: 0 auto;
}
/* 响应式布局示例 */
@media (max-width: 768px) {
.grid-container {
grid-template-columns: repeat(6, 1fr);
gap: 12px;
}
}
@media (max-width: 480px) {
.grid-container {
grid-template-columns: repeat(1, 1fr);
gap: 8px;
}
}
/* 占据不同列数的组件 */
.item-full { grid-column: 1 / -1; }
.item-half { grid-column: span 6; }
.item-third { grid-column: span 4; }
.item-quarter { grid-column: span 3; }
网格优势
- 灵活布局:12 能整除 1、2、3、4、6
- 对齐一致:所有元素都基于网格对齐
- 响应式:轻松调整列数适配屏幕
- 视觉层次:清晰的结构组织
第三部分:垂直节奏
建立垂直节奏
垂直节奏是指行高和间距的一致性:
/* 定义基础垂直节奏 */
:root {
--line-height: 1.5;
--base-unit: 8px;
--rhythm-unit: calc(24px * var(--line-height));
}
body {
font-size: 16px;
line-height: var(--line-height);
margin: 0;
}
/* 所有标题和段落都遵循垂直节奏 */
p {
margin-bottom: var(--rhythm-unit);
}
h1 {
font-size: 32px;
line-height: calc(32px * 1.2);
margin-top: var(--rhythm-unit);
margin-bottom: calc(var(--rhythm-unit) / 2);
}
h2 {
font-size: 24px;
line-height: calc(24px * 1.3);
margin-top: var(--rhythm-unit);
margin-bottom: calc(var(--rhythm-unit) / 2);
}
h3 {
font-size: 20px;
line-height: calc(20px * 1.4);
margin-top: calc(var(--rhythm-unit) / 2);
margin-bottom: calc(var(--rhythm-unit) / 4);
}
垂直节奏的好处
- 视觉和谐:页面看起来更平衡
- 可读性:段落间距一致,易于阅读
- 专业感:设计显得精心打磨
- 易于维护:所有间距都有规律可循
第四部分:对齐原则
左对齐 vs 居中对齐
/* 左对齐 - 适用于列表、表格 */
.text-left {
text-align: left;
}
/* 居中对齐 - 适用于标题、CTA 按钮 */
.text-center {
text-align: center;
}
/* 右对齐 - 适用于价格、数字 */
.text-right {
text-align: right;
}
/* 两端对齐 - 谨慎使用 */
.text-justify {
text-align: justify;
}
垂直对齐策略
/* Flexbox 垂直对齐 */
.flex-center {
display: flex;
align-items: center; /* 垂直居中 */
justify-content: center; /* 水平居中 */
height: 100%;
}
.flex-baseline {
display: flex;
align-items: baseline; /* 基线对齐 */
}
/* Grid 对齐 */
.grid-center {
display: grid;
place-items: center; /* 水平和垂直居中 */
}
/* 顶部对齐 */
.align-top {
display: flex;
align-items: flex-start;
}
/* 底部对齐 */
.align-bottom {
display: flex;
align-items: flex-end;
}
第五部分:设计系统中的应用
创建间距系统
// Design tokens - 间距规范
const tokens = {
spacing: {
0: '0',
1: '4px',
2: '8px',
3: '12px',
4: '16px',
5: '20px',
6: '24px',
7: '28px',
8: '32px',
9: '36px',
10: '40px',
},
// 预定义的间距组合
gutter: '16px', // 组件间距
section: '48px', // 章节间距
inset: '8px', // 内部间距
};
// 在组件中使用
const Button = styled.button`
padding: ${tokens.spacing[3]} ${tokens.spacing[4]};
margin: 0;
gap: ${tokens.spacing[2]};
`;
const Card = styled.div`
padding: ${tokens.spacing[4]};
margin-bottom: ${tokens.gutter};
border-radius: 8px;
`;
const Section = styled.section`
margin-bottom: ${tokens.section};
padding: 0 ${tokens.gutter};
`;
响应式网格组件
// React 中的响应式网格
import styled from 'styled-components';
const Grid = styled.div`
display: grid;
grid-template-columns: repeat(auto-fit, minmax(${props => props.minWidth || '300px'}, 1fr));
gap: ${props => props.gap || '16px'};
margin-bottom: ${props => props.mb || '0'};
`;
const GridItem = styled.div`
grid-column: span ${props => props.span || 1};
@media (max-width: 768px) {
grid-column: span ${props => props.spanMobile || props.span || 1};
}
`;
// 使用示例
<Grid minWidth="250px" gap="24px" mb="48px">
<GridItem span={4}>
<Card>Item 1</Card>
</GridItem>
<GridItem span={4}>
<Card>Item 2</Card>
</GridItem>
<GridItem span={4}>
<Card>Item 3</Card>
</GridItem>
</Grid>
第六部分:实际案例
完整的卡片布局
<!-- HTML 结构 -->
<div class="card-grid">
<article class="card">
<img src="image.jpg" alt="Card Image" class="card-image">
<div class="card-content">
<h3 class="card-title">标题</h3>
<p class="card-description">描述文本</p>
<footer class="card-footer">
<span class="card-date">2025-12-08</span>
<button class="card-action">了解更多</button>
</footer>
</div>
</article>
</div>
<!-- CSS 应用网格系统 -->
<style>
.card-grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 24px;
padding: 24px;
}
.card {
display: flex;
flex-direction: column;
border-radius: 8px;
overflow: hidden;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}
.card-image {
width: 100%;
height: 200px;
object-fit: cover;
}
.card-content {
padding: 16px;
flex-grow: 1;
display: flex;
flex-direction: column;
}
.card-title {
font-size: 18px;
margin: 0 0 8px 0;
}
.card-description {
font-size: 14px;
color: #666;
margin: 0 0 16px 0;
flex-grow: 1;
}
.card-footer {
display: flex;
justify-content: space-between;
align-items: center;
padding-top: 8px;
border-top: 1px solid #eee;
}
</style>
第七部分:最佳实践
✅ 应该做的事
- 保持一致性
- 所有间距都遵循网格系统
- 定义清晰的间距规范
- 文档化规范
- 记录所有间距和对齐规则
- 提供组件库示例
- 工具支持
- 使用设计工具的网格功能
- 在代码中应用 Design Tokens
- 测试响应式
- 在多个屏幕尺寸测试布局
- 确保网格在移动设备上有效
❌ 不应该做的事
- 随意间距
- 避免使用任意的 px 值
- 不要混合不同的间距系统
- 过度对齐
- 不要强制所有内容都完全对齐网格
- 某些情况下灵活性更重要
- 忽视可访问性
- 确保间距对视力障碍者友好
- 可点击元素要有足够的大小和间距
- 不适配响应式
- 网格必须在所有屏幕尺寸上有效
- 间距应该根据屏幕自动调整
第八部分:工具和资源
推荐工具
// Tailwind CSS - 内置间距系统
<div className="p-4 m-6 gap-8">
<!-- Tailwind 自动应用 8 的倍数间距 -->
</div>
// Material-UI Grid
<Grid container spacing={3}>
<Grid item xs={12} sm={6} md={4}>
Content
</Grid>
</Grid>
// Bootstrap Grid
<div class="row">
<div class="col-md-4">Column</div>
<div class="col-md-8">Column</div>
</div>
设计系统参考
- Material Design: 4dp 网格系统
- Apple Human Interface: 基于 8pt 网格
- IBM Carbon: 详细的间距规范
- Ant Design: 清晰的设计 Tokens
总结
有效的网格系统和间距规范是优秀设计的基础:
✅ 核心要点:
- 8pt 网格系统是行业标准
- 12 列网格提供最大灵活性
- 垂直节奏确保视觉和谐
- 对齐原则指导设计决策
- 设计 Tokens 便于实现
📊 应用层次:
- 定义间距规范
- 建立网格系统
- 创建对齐原则
- 文档化最佳实践
- 在项目中实施
🎯 下一步:
- 为你的项目定义间距系统
- 创建网格系统文档
- 在设计工具中配置网格
- 在代码中应用 Design Tokens


