responsive-design
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseResponsive Design Guidelines
响应式设计指南
Core Principles
核心原则
- Write semantic HTML to improve accessibility and SEO
- Use CSS for styling, avoiding inline styles
- Ensure responsive design using media queries and flexible layouts
- Prioritize accessibility by using ARIA roles and attributes
- Design mobile-first, then enhance for larger screens
- 编写语义化HTML以提升可访问性与SEO
- 使用CSS进行样式设置,避免内联样式
- 利用媒体查询和弹性布局实现响应式设计
- 通过ARIA角色与属性优先保障可访问性
- 采用移动优先设计,再针对更大屏幕进行增强
Mobile-First Approach
移动优先方法
Strategy
策略
- Start with styles for the smallest viewport
- Add complexity through progressive enhancement
- Base styles work without media queries
- Media queries add features for larger screens
- 从最小视口的样式开始设计
- 通过渐进增强的方式增加复杂度
- 基础样式无需媒体查询即可生效
- 媒体查询为更大屏幕添加功能
Benefits
优势
- Forces prioritization of essential content
- Improves performance on mobile devices
- Ensures core functionality works everywhere
- Reduces CSS complexity and specificity issues
css
/* Base styles for mobile */
.container {
padding: 1rem;
}
/* Tablet and up */
@media (min-width: 768px) {
.container {
padding: 2rem;
}
}
/* Desktop and up */
@media (min-width: 1024px) {
.container {
max-width: 1200px;
margin: 0 auto;
}
}- 迫使开发者优先考虑核心内容
- 提升移动设备上的性能
- 确保核心功能在所有设备上可用
- 降低CSS复杂度和特异性问题
css
/* Base styles for mobile */
.container {
padding: 1rem;
}
/* Tablet and up */
@media (min-width: 768px) {
.container {
padding: 2rem;
}
}
/* Desktop and up */
@media (min-width: 1024px) {
.container {
max-width: 1200px;
margin: 0 auto;
}
}Flexible Layouts
弹性布局
Flexbox
Flexbox
- Use for one-dimensional layouts (row or column)
- Leverage for responsive wrapping
flex-wrap - Use for consistent spacing
gap - Combine with media queries for layout changes
- 用于一维布局(行或列)
- 利用实现响应式换行
flex-wrap - 使用保证间距一致
gap - 结合媒体查询进行布局调整
CSS Grid
CSS Grid
- Use for two-dimensional layouts
- Leverage and
auto-fitfor responsive gridsauto-fill - Use for flexible track sizing
minmax() - Combine units with fixed minimums
fr
css
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 1rem;
}- 用于二维布局
- 利用和
auto-fit实现响应式网格auto-fill - 使用设置弹性轨道尺寸
minmax() - 将单位与固定最小值结合使用
fr
css
.grid {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
gap: 1rem;
}Container Queries
容器查询
- Style components based on container size, not viewport
- Use for truly reusable components
- Define containers with
container-type
- 根据容器尺寸而非视口尺寸为组件设置样式
- 用于打造真正可复用的组件
- 通过定义容器
container-type
Responsive Typography
响应式排版
Fluid Typography
流体排版
- Use for responsive font sizes
clamp() - Set minimum, preferred, and maximum values
- Avoid text that's too small on mobile or too large on desktop
css
h1 {
font-size: clamp(1.5rem, 4vw + 1rem, 3rem);
}- 使用实现响应式字体大小
clamp() - 设置最小值、首选值和最大值
- 避免移动端文本过小或桌面端文本过大的问题
css
h1 {
font-size: clamp(1.5rem, 4vw + 1rem, 3rem);
}Units
单位
- Use for scalable typography
rem - Base on user's browser settings
rem - Use for component-relative sizing
em - Avoid fixed values for font sizes
px
- 使用实现可缩放排版
rem - 基于用户浏览器设置定义基准
rem - 使用实现组件相对尺寸
em - 避免为字体大小使用固定值
px
Responsive Images
响应式图片
Srcset and Sizes
Srcset与Sizes
- Use for multiple image resolutions
srcset - Use to indicate display size at breakpoints
sizes - Let browser choose optimal image
html
<img
src="image-800.jpg"
srcset="image-400.jpg 400w, image-800.jpg 800w, image-1200.jpg 1200w"
sizes="(max-width: 600px) 100vw, (max-width: 1000px) 50vw, 800px"
alt="Description"
>- 使用提供多分辨率图片
srcset - 使用指定断点处的显示尺寸
sizes - 由浏览器选择最优图片
html
<img
src="image-800.jpg"
srcset="image-400.jpg 400w, image-800.jpg 800w, image-1200.jpg 1200w"
sizes="(max-width: 600px) 100vw, (max-width: 1000px) 50vw, 800px"
alt="Description"
>Picture Element
Picture元素
- Use for art direction (different crops at different sizes)
- Provide fallback with element
<img> - Use media attributes for breakpoint-specific sources
- 用于艺术指导(不同尺寸下使用不同裁剪的图片)
- 通过元素提供降级方案
<img> - 为特定断点的资源使用media属性
Performance
性能优化
- Include and
widthattributes to prevent layout shiftheight - Use for below-fold images
loading="lazy" - Consider CSS property
aspect-ratio
- 包含和
width属性以避免布局偏移height - 对折叠区域以下的图片使用
loading="lazy" - 考虑使用CSS属性
aspect-ratio
Media Queries
媒体查询
Common Breakpoints
常见断点
css
/* Small phones */
@media (min-width: 320px) { }
/* Large phones */
@media (min-width: 480px) { }
/* Tablets */
@media (min-width: 768px) { }
/* Laptops/Desktops */
@media (min-width: 1024px) { }
/* Large screens */
@media (min-width: 1280px) { }
/* Extra large screens */
@media (min-width: 1536px) { }css
/* Small phones */
@media (min-width: 320px) { }
/* Large phones */
@media (min-width: 480px) { }
/* Tablets */
@media (min-width: 768px) { }
/* Laptops/Desktops */
@media (min-width: 1024px) { }
/* Large screens */
@media (min-width: 1280px) { }
/* Extra large screens */
@media (min-width: 1536px) { }Feature Queries
特性查询
- Use for feature detection
@supports - Provide fallbacks for unsupported features
- Test for specific CSS properties
- 使用进行特性检测
@supports - 为不支持的特性提供降级方案
- 检测特定CSS属性
Preference Queries
偏好查询
- for dark/light mode
prefers-color-scheme - for animation preferences
prefers-reduced-motion - for contrast preferences
prefers-contrast
- 用于适配深色/浅色模式
prefers-color-scheme - 用于适配动画偏好
prefers-reduced-motion - 用于适配对比度偏好
prefers-contrast
Touch Targets
触摸目标
Sizing
尺寸要求
- Minimum 44x44 pixels for touch targets
- Provide adequate spacing between targets
- Consider thumb reach zones on mobile
- 触摸目标最小尺寸为44x44像素
- 目标之间保留足够间距
- 考虑移动端拇指可触及区域
Interaction
交互设计
- Don't rely solely on hover states
- Provide touch alternatives for hover interactions
- Use for hover-capable devices
@media (hover: hover)
- 不要仅依赖悬停状态
- 为悬停交互提供触摸替代方案
- 对支持悬停的设备使用
@media (hover: hover)
Viewport Configuration
视口配置
Meta Tag
Meta标签
html
<meta name="viewport" content="width=device-width, initial-scale=1">html
<meta name="viewport" content="width=device-width, initial-scale=1">Best Practices
最佳实践
- Always include viewport meta tag
- Don't disable user scaling (avoid )
user-scalable=no - Test with pinch-to-zoom enabled
- Ensure content is usable at 200% zoom
- 始终包含视口meta标签
- 不要禁用用户缩放(避免使用)
user-scalable=no - 测试捏合缩放功能
- 确保内容在200%缩放时仍可正常使用
Testing
测试
Tools
工具
- Browser DevTools device emulation
- Real device testing when possible
- Lighthouse for performance audits
- W3C validators for code quality
- 浏览器DevTools设备模拟
- 尽可能进行真实设备测试
- 使用Lighthouse进行性能审计
- 使用W3C验证工具检查代码质量
Checklist
检查清单
- Test at multiple viewport sizes
- Test landscape and portrait orientations
- Test with touch and mouse input
- Test with keyboard navigation
- Verify images load appropriate sizes
- Check typography readability
- Verify touch targets are adequate
- Test with browser zoom
- 在多种视口尺寸下进行测试
- 测试横屏与竖屏方向
- 测试触摸与鼠标输入
- 测试键盘导航
- 验证图片加载了合适的尺寸
- 检查排版可读性
- 确认触摸目标尺寸达标
- 测试浏览器缩放效果
Performance Considerations
性能考量
Critical CSS
关键CSS
- Inline critical above-fold CSS
- Defer non-critical stylesheets
- Minimize render-blocking resources
- 内联首屏关键CSS
- 延迟加载非关键样式表
- 减少渲染阻塞资源
Asset Optimization
资源优化
- Compress images appropriately
- Use modern formats (WebP, AVIF)
- Lazy load below-fold content
- Consider responsive loading strategies
- 合理压缩图片
- 使用现代格式(WebP、AVIF)
- 懒加载折叠区域以下的内容
- 考虑响应式加载策略