css
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseCSS Development Guidelines
CSS开发指南
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 external stylesheets over inline or embedded styles
- 编写语义化HTML以提升可访问性和SEO
- 使用CSS进行样式设计,避免内联样式
- 利用媒体查询和弹性布局实现响应式设计
- 优先使用外部样式表,而非内联或嵌入式样式
Layout Techniques
布局技术
Flexbox
Flexbox
- Use Flexbox for one-dimensional layouts (rows or columns)
- Leverage and
justify-contentfor alignmentalign-items - Use for responsive wrapping behavior
flex-wrap - Prefer property over margins for consistent spacing
gap
- 使用Flexbox实现一维布局(行或列)
- 借助和
justify-content进行对齐align-items - 使用实现响应式换行
flex-wrap - 优先使用属性而非外边距来保证间距一致性
gap
Grid
Grid
- Use CSS Grid for two-dimensional layouts
- Define grid templates with and
grid-template-columnsgrid-template-rows - Use for named template areas
grid-area - Leverage and
auto-fitfor responsive gridsauto-fill
- 使用CSS Grid实现二维布局
- 通过和
grid-template-columns定义网格模板grid-template-rows - 使用设置命名模板区域
grid-area - 利用和
auto-fit实现响应式网格auto-fill
Units and Typography
单位与排版
- Use units for typography to respect user preferences
rem - Use units for component-relative sizing
em - Avoid for font sizes; reserve for borders and fixed elements
px - Implement fluid typography with when appropriate
clamp() - Use viewport units (,
vw) thoughtfully for full-screen layoutsvh
- 使用单位设置字体,以尊重用户偏好
rem - 使用单位进行组件相对尺寸设置
em - 避免使用设置字体大小;仅用于边框和固定元素
px - 适当情况下使用实现流体排版
clamp() - 谨慎使用视窗单位(,
vw)实现全屏布局vh
CSS Variables (Custom Properties)
CSS Variables(自定义属性)
- Define design tokens as CSS variables on
:root - Use variables for colors, spacing, typography, and shadows
- Implement theming by overriding variables in different contexts
- Name variables semantically (e.g., ,
--color-primary)--spacing-md
- 在上定义设计令牌作为CSS变量
:root - 将变量用于颜色、间距、排版和阴影设置
- 通过在不同上下文中重写变量实现主题切换
- 语义化命名变量(例如:,
--color-primary)--spacing-md
Naming Conventions
命名规范
BEM Methodology
BEM方法论
- Block: Standalone component (e.g., )
.card - Element: Part of a block (e.g., )
.card__title - Modifier: Variation of block or element (e.g., )
.card--featured
- Block(块):独立组件(例如:)
.card - Element(元素):块的组成部分(例如:)
.card__title - Modifier(修饰符):块或元素的变体(例如:)
.card--featured
Best Practices
最佳实践
- Use lowercase with hyphens for class names
- Avoid IDs for styling; reserve for JavaScript hooks
- Keep specificity low by using single class selectors
- Scope styles to components to prevent conflicts
- 类名使用小写加连字符的格式
- 避免使用ID进行样式设置;预留作为JavaScript钩子
- 通过使用单类选择器保持低优先级
- 为样式设置组件作用域以避免冲突
Specificity Management
优先级管理
- Maintain specificity at 0-1-0 (single class) when possible
- Avoid declarations
!important - Use cascading intentionally, not accidentally
- Consider CSS Layers () for specificity control
@layer
- 尽可能将优先级保持在0-1-0(单类选择器)
- 避免使用声明
!important - 有意而非无意地利用层叠特性
- 考虑使用CSS Layers()控制优先级
@layer
Responsive Design
响应式设计
- Implement mobile-first media queries
- Use relative units for flexible layouts
- Test across multiple viewport sizes
- Consider container queries for component-level responsiveness
- 采用移动端优先的媒体查询
- 使用相对单位实现弹性布局
- 在多个视窗尺寸下进行测试
- 考虑使用容器查询实现组件级响应式设计
Performance
性能优化
- Minimize selector complexity
- Avoid deeply nested selectors
- Use efficient selectors (class over element)
- Leverage CSS containment for isolated components
- Consider critical CSS for above-the-fold content
- 最小化选择器复杂度
- 避免深度嵌套的选择器
- 使用高效的选择器(类选择器优于元素选择器)
- 利用CSS containment隔离组件
- 考虑为首屏内容使用关键CSS
Modern CSS Features
现代CSS特性
- Use for maintaining proportions
aspect-ratio - Leverage in Flexbox and Grid
gap - Use logical properties (,
margin-inline)padding-block - Implement smooth transitions with property
transition - Use for feature detection
@supports
- 使用保持比例
aspect-ratio - 在Flexbox和Grid中使用
gap - 使用逻辑属性(,
margin-inline)padding-block - 利用属性实现平滑过渡
transition - 使用进行特性检测
@supports
Organization
样式表组织
- Structure stylesheets logically (base, layout, components, utilities)
- Keep files modular and focused
- Document complex styles with comments
- Use a consistent property order within declarations
- 按逻辑结构组织样式表(基础样式、布局、组件、工具类)
- 保持文件模块化且聚焦单一功能
- 为复杂样式添加注释说明
- 在声明中保持一致的属性顺序
Browser Compatibility
浏览器兼容性
- Use vendor prefixes when necessary (consider Autoprefixer)
- Test across target browsers
- Provide fallbacks for newer features
- Use progressive enhancement approach
- 必要时使用厂商前缀(可考虑使用Autoprefixer)
- 在目标浏览器中进行测试
- 为较新特性提供降级方案
- 采用渐进增强的开发方式
Accessibility
可访问性
- Ensure sufficient color contrast (WCAG AA minimum)
- Provide visible focus styles for keyboard navigation
- Avoid hiding content in ways that affect screen readers
- Use media query for animations
prefers-reduced-motion
- 确保足够的颜色对比度(至少符合WCAG AA标准)
- 为键盘导航提供可见的焦点样式
- 避免以影响屏幕阅读器的方式隐藏内容
- 使用媒体查询控制动画
prefers-reduced-motion