modern-css
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseModern CSS
现代CSS
Pure native CSS for building interfaces — no preprocessors, no frameworks.
使用纯原生CSS构建界面——无需预处理器,无需框架。
When to Use (and When NOT to)
适用场景(与不适用场景)
| Use Freely (Baseline) | Feature-Detect First |
|---|---|
| CSS Grid, Subgrid, Flexbox | |
| Container Queries (size + style) | Customizable |
| Scroll-state queries (Chrome-only) |
CSS Nesting, | |
| |
| Typed |
| Relative color syntax | |
| |
| Scroll-driven animations | Grid Lanes / masonry (experimental) |
| Anchor positioning, Popover API | |
| |
| View Transitions, logical properties |
| 可自由使用(基线支持) | 需先检测特性支持 |
|---|---|
| CSS Grid、Subgrid、Flexbox | |
| Container Queries(尺寸+样式) | 可自定义 |
| 滚动状态查询(仅Chrome支持) |
CSS嵌套、 | |
| |
| 超出 |
| 相对颜色语法 | |
| |
| 滚动驱动动画 | Grid Lanes / 瀑布流(实验性特性) |
| 锚点定位、Popover API | |
| |
| 视图过渡、逻辑属性 |
CRITICAL: The Modern Cascade
核心要点:现代层叠规则
Understanding how styles resolve is the single most important concept in CSS. The additions of and fundamentally changed the cascade algorithm.
@layer@scopeStyle Resolution Order (highest priority wins):
┌─────────────────────────────────────────────────┐
│ 1. Transitions (active transition wins) │
│ 2. !important (user-agent > user > author) │
│ 3. @layer order (later layer > earlier layer) │
│ 4. Unlayered styles (beat ALL layers) │
│ 5. Specificity (ID > class > element) │
│ 6. @scope proximity (closer root wins) NEW │
│ 7. Source order (later > earlier) │
└─────────────────────────────────────────────────┘
Unlayered > Last layer > ... > First layer
(utilities) (reset)Cascade layers () and scope proximity () are now more powerful than selector specificity. Define your layer order once () and specificity wars disappear. Unlayered styles always beat layered styles — use this for overrides.
@layer@scope@layer reset, base, components, utilities;理解样式的解析逻辑是CSS中最重要的概念。和的加入从根本上改变了层叠算法。
@layer@scopeStyle Resolution Order (highest priority wins):
┌─────────────────────────────────────────────────┐
│ 1. Transitions (active transition wins) │
│ 2. !important (user-agent > user > author) │
│ 3. @layer order (later layer > earlier layer) │
│ 4. Unlayered styles (beat ALL layers) │
│ 5. Specificity (ID > class > element) │
│ 6. @scope proximity (closer root wins) NEW │
│ 7. Source order (later > earlier) │
└─────────────────────────────────────────────────┘
Unlayered > Last layer > ... > First layer
(utilities) (reset)层叠层级()和作用域优先级()现在比选择器优先级更强大。只需定义一次层级顺序(),就能避免优先级冲突。未分层的样式始终优先于分层样式——可用于覆盖样式。
@layer@scope@layer reset, base, components, utilities;Quick Decision Trees
快速决策树
"How do I lay this out?"
"我该如何布局?"
Layout approach?
├─ 2D grid (rows + columns) → CSS Grid
│ ├─ Children must align across → Grid + Subgrid
│ └─ Waterfall / masonry → grid-lanes (experimental)
├─ 1D row OR column → Flexbox
├─ Component adapts to container → Container Query + Grid/Flex
├─ Viewport-based responsiveness → @media range syntax
└─ Element sized to content → fit-content / min-content / stretchLayout approach?
├─ 2D grid (rows + columns) → CSS Grid
│ ├─ Children must align across → Grid + Subgrid
│ └─ Waterfall / masonry → grid-lanes (experimental)
├─ 1D row OR column → Flexbox
├─ Component adapts to container → Container Query + Grid/Flex
├─ Viewport-based responsiveness → @media range syntax
└─ Element sized to content → fit-content / min-content / stretch"How do I style this state?"
"我该如何为状态设置样式?"
Style based on what?
├─ Child/descendant presence → :has()
├─ Container size → @container (inline-size)
├─ Container custom property → @container style()
├─ Scroll position (stuck/snapped) → scroll-state() query
├─ Element's own custom property → if(style(...))
├─ Browser feature support → @supports
├─ User preference (motion/color) → @media (prefers-*)
└─ Multiple selectors efficiently → :is() / :where()Style based on what?
├─ Child/descendant presence → :has()
├─ Container size → @container (inline-size)
├─ Container custom property → @container style()
├─ Scroll position (stuck/snapped) → scroll-state() query
├─ Element's own custom property → if(style(...))
├─ Browser feature support → @supports
├─ User preference (motion/color) → @media (prefers-*)
└─ Multiple selectors efficiently → :is() / :where()"How do I animate this?"
"我该如何实现动画?"
Animation type?
├─ Enter/appear on DOM → @starting-style + transition
├─ Exit/disappear (display:none) → transition-behavior: allow-discrete
├─ Animate to/from auto height → interpolate-size: allow-keywords
├─ Scroll-linked (parallax/reveal) → animation-timeline: scroll()/view()
├─ Page/view navigation → View Transitions API
├─ Custom easing (bounce/spring) → linear() function
└─ Always: respect user preference → @media (prefers-reduced-motion)Animation type?
├─ Enter/appear on DOM → @starting-style + transition
├─ Exit/disappear (display:none) → transition-behavior: allow-discrete
├─ Animate to/from auto height → interpolate-size: allow-keywords
├─ Scroll-linked (parallax/reveal) → animation-timeline: scroll()/view()
├─ Page/view navigation → View Transitions API
├─ Custom easing (bounce/spring) → linear() function
└─ Always: respect user preference → @media (prefers-reduced-motion)What CSS Replaced JavaScript For
CSS替代JavaScript的场景
| JavaScript Pattern | CSS Replacement |
|---|---|
| Scroll position listeners | Scroll-driven animations |
| IntersectionObserver for reveal | |
| Sticky header shadow toggle | |
| Floating UI / Popper.js | Anchor positioning |
| Carousel prev/next/dots | |
| Auto-expanding textarea | |
| Staggered animation delays | |
| |
| Parent element selection | |
| Theme toggle logic | |
| Tooltip/popover show/hide | Popover API + invoker commands |
| Color manipulation functions | |
| JavaScript模式 | CSS替代方案 |
|---|---|
| 滚动位置监听器 | 滚动驱动动画 |
| 用于元素显示的IntersectionObserver | |
| 粘性头部阴影切换 | |
| Floating UI / Popper.js | 锚点定位 |
| 轮播图上一页/下一页/指示器 | |
| 自动扩展的文本域 | |
| 交错动画延迟 | |
| |
| 父元素选择 | |
| 主题切换逻辑 | |
| 工具提示/弹出框的显示/隐藏 | Popover API + 调用命令 |
| 颜色处理函数 | |
Anti-Patterns (CRITICAL)
反模式(核心要点)
| Anti-Pattern | Problem | Fix |
|---|---|---|
Overusing | Specificity arms race | Use |
Deep nesting ( | Fragile, DOM-coupled | Flat selectors, |
IDs for styling ( | Too specific to override | Classes ( |
| Viewport-coupled, not reusable | Container queries |
| JS scroll listeners for effects | Janky, expensive | Scroll-driven animations |
| JS for tooltip positioning | Floating UI dependency | Anchor positioning |
| JS for carousel controls | Fragile, a11y issues | |
| JS for auto-expanding textarea | Unnecessary complexity | |
| Wrong duration, janky | |
| Breaks in RTL/vertical | Logical properties ( |
| Legacy syntax | |
| Removes ALL functionality | |
| Preprocessor-only variables | Can't change at runtime | CSS custom properties |
| Preprocessor-only nesting | Extra build step dependency | Native CSS nesting |
| Preprocessor color functions | Can't respond to context | |
| Performance-heavy | Only headings/short text |
| Delays LCP rendering | Only off-screen sections |
Overusing | Wastes GPU memory | Apply only to animating elements |
| 反模式 | 问题 | 解决方案 |
|---|---|---|
过度使用 | 引发优先级军备竞赛 | 使用 |
深层嵌套( | 脆弱、与DOM结构耦合 | 扁平选择器、 |
使用ID进行样式设置( | 优先级过高难以覆盖 | 使用类选择器( |
使用 | 与视口耦合,无法复用 | 容器查询 |
| 使用JS滚动监听器实现效果 | 卡顿、性能开销大 | 滚动驱动动画 |
| 使用JS处理工具提示定位 | 依赖Floating UI库 | 锚点定位 |
| 使用JS实现轮播图控制 | 脆弱、可访问性问题 | |
| 使用JS实现自动扩展文本域 | 不必要的复杂度 | |
使用 | 动画时长错误、卡顿 | |
使用 | 在RTL/垂直布局中失效 | 逻辑属性( |
使用带逗号的 | 旧版语法 | 使用空格分隔的 |
对select使用 | 移除所有原生功能 | 使用 |
| 仅使用预处理器变量 | 无法在运行时修改 | CSS自定义属性 |
| 仅使用预处理器嵌套 | 依赖额外构建步骤 | 原生CSS嵌套 |
| 仅使用预处理器颜色函数 | 无法响应上下文变化 | |
对段落使用 | 性能开销大 | 仅对标题/短文本使用 |
对首屏内容使用 | 延迟LCP渲染 | 仅对屏幕外区域使用 |
过度使用 | 浪费GPU内存 | 仅对正在动画的元素使用 |
Reference Documentation
参考文档
| File | Purpose |
|---|---|
| references/CASCADE.md | Nesting, |
| references/LAYOUT.md | Grid, Subgrid, Flexbox, Container Queries, and intrinsic sizing |
| references/SELECTORS.md | |
| references/COLOR.md | OKLCH, |
| references/TOKENS.md | |
| references/ANIMATION.md | |
| references/SCROLL.md | Scroll-driven animations, scroll-state queries, native carousels |
| references/COMPONENTS.md | Customizable |
| references/PERFORMANCE.md | |
| references/CHEATSHEET.md | Quick reference: browser support, legacy→modern patterns, units |
| 文件 | 用途 |
|---|---|
| references/CASCADE.md | 嵌套、 |
| references/LAYOUT.md | Grid、Subgrid、Flexbox、容器查询与内在尺寸设置 |
| references/SELECTORS.md | |
| references/COLOR.md | OKLCH、 |
| references/TOKENS.md | |
| references/ANIMATION.md | |
| references/SCROLL.md | 滚动驱动动画、滚动状态查询、原生轮播图 |
| references/COMPONENTS.md | 可自定义 |
| references/PERFORMANCE.md | |
| references/CHEATSHEET.md | 快速参考:浏览器支持、旧版→现代模式、单位 |
Sources
资料来源
Official Specifications
官方规范
- CSS Snapshot 2025 — W3C
- CSS Values and Units Level 5 — ,
if(),random()sibling-index/count() - CSS Functions and Mixins Level 1 — ,
@function@mixin - CSS Conditional Rules Level 5 — Scroll-state queries
- CSS Anchor Positioning
- CSS Overflow Level 5 — Scroll markers/buttons
- CSS Snapshot 2025 — W3C
- CSS Values and Units Level 5 — 、
if()、random()sibling-index/count() - CSS Functions and Mixins Level 1 — 、
@function@mixin - CSS Conditional Rules Level 5 — 滚动状态查询
- CSS Anchor Positioning
- CSS Overflow Level 5 — 滚动标记/按钮
Browser Vendor Blogs
浏览器厂商博客
- CSS Wrapped 2025 — Chrome DevRel
- Interop 2025 — WebKit
- What's New in Web UI (I/O 2025)
- CSS Wrapped 2025 — Chrome开发者关系团队
- Interop 2025 — WebKit
- What's New in Web UI (I/O 2025)