关于 modern-css
modern-css 是面向编写现代、稳健 CSS 的参考技能,适合构建响应式界面和交互组件的开发者。内容涵盖布局、色彩、排版、动画及浏览器支持,并强调渐进增强和尊重用户偏好。
主要功能
- 01容器查询与响应式布局
- 02现代色彩空间和主题方案
- 03流式排版与动态视口单位
- 04滚动动画和视图过渡
- 05弹出层、锚点定位与表单增强
- 06渐进增强及浏览器支持检查
适用场景
- 01为组件编写适应容器尺寸的样式
- 02设计明暗主题和现代调色方案
- 03构建动画、弹出层和表单交互
- 04评估新 CSS 特性的浏览器兼容性
安装
复制下面的命令,在终端中运行。
$
npx skills add https:/ / github.com/ paulirish/ dotfiles --skill modern-css安装后在 Claude Code 中输入/modern-css确认已加载。
附带文件references/argyle-cacadia-2025-deck.md
详细介绍
现代 CSS
本技能提供编写现代、健壮且高效的 CSS 的参考。
布局与响应式设计
容器查询
.card {
container: --my-card / inline-size;
}
@container --my-card (width < 40ch) {
/* Component-based responsive design */
}
@container (20ch < width < 50ch) {
/* Range syntax */
}
容器单位: cqi、cqb、cqw、cqh——相对于容器尺寸
锚定容器查询: 根据锚点回退状态为定位元素设置样式
.tooltip {
container-type: anchored;
}
@container anchored(top) {
/* Styles when positioned at top */
}
媒体查询范围语法
@media (width <= 1024px) { }
@media (360px < width < 1024px) { }
Grid 增强
- Subgrid: 嵌套布局继承父级网格线
- Masonry: 使用
display: grid-lanes创建 Pinterest 风格的布局,并保持逻辑的标签页顺序。(此前提议使用grid-template-rows: masonry。)
颜色与主题
配色方案与 Light-Dark 函数
:root {
color-scheme: light dark;
--surface-1: light-dark(white, #222);
--text-1: light-dark(#222, #fff);
}
现代颜色空间
/* OKLCH: uniform brightness, P3+ colors */
.vibrant {
background: oklch(72% 75% 330);
}
/* Display-P3 for HDR displays */
@media (dynamic-range: high) {
.neon {
--neon-red: color(display-p3 1 0 0);
}
}
/* Better gradients with in oklch */
.gradient {
background: linear-gradient(
to right in oklch,
color(display-p3 1 0 .5),
color(display-p3 0 1 1)
);
}
颜色调整
/* color-mix() */
.lighten {
background: color-mix(in oklab, var(--brand), white);
}
/* Relative color syntax */
.lighter {
background: oklch(from blue calc(l + .25) c h);
background: oklch(from blue 75% c h); /* Set to specific lightness */
}
.semi-transparent {
background: oklch(from var(--color) l c h / 50%);
}
.complementary {
background: hsl(from blue calc(h + 180) s l);
}
强调色
:root {
accent-color: hotpink; /* Tints checkboxes, radios, range inputs */
}
排版
文本换行
h1 {
text-wrap: balance; /* Balanced multi-line headings */
max-inline-size: 25ch;
}
p {
text-wrap: pretty; /* No orphans */
max-inline-size: 50ch;
}
文本框裁剪
h1, p, button {
text-box: trim-both cap alphabetic; /* Optical vertical centering */
}
流式排版
.heading {
font-size: clamp(1rem, 1rem + 0.5vw, 2rem); /* Respects user preferences */
}
动态视口单位
dvh/dvw——动态单位(会考虑移动浏览器界面)svh/svw——小视口单位(可能的最小视口)lvh/lvw——大视口单位(可能的最大视口)
动画与动效
滚动驱动动画
/* Animate on scroll position */
.parallax {
animation: slide-up linear both;
animation-timeline: scroll();
}
/* Animate on viewport intersection */
.fade-in {
animation: fade linear both;
animation-timeline: view();
animation-range: cover -75cqi contain 20cqi;
}
视图过渡
状态: 已纳入基线,现已推出(同文档)。 跨文档过渡目前为有限可用(Chrome/Safari 18.2+)。
@view-transition {
navigation: auto; /* Automatically animate page transitions (MPAs) */
}
nav {
view-transition-name: --persist-nav; /* Persist specific elements */
view-transition-class: --site-header; /* Group transitions with classes */
}
/* Style the active transition */
html:active-view-transition {
overflow: hidden;
}
嵌套视图过渡组: 在过渡期间保留 3D 变换和裁剪效果。
使用 linear() 实现高级缓动
.springy {
--spring: linear(
0, 0.14 4%, 0.94 17%, 1.15 24% 30%, 1.02 43%, 0.98 51%, 1 77%, 1
);
transition: transform 1s var(--spring);
}
@starting-style
.dialog {
transition: opacity .5s, scale .5s;
@starting-style {
opacity: 0;
scale: 1.1;
}
}
自定义属性与高级功能
@property
类型安全、可动画的自定义属性:
@property --gradient-angle {
syntax: "<angle>";
inherits: false;
initial-value: 0deg;
}
.animate {
transition: --gradient-angle 1s ease;
&:hover {
--gradient-angle: 360deg;
}
}
数学函数与 calc-size()
现已推出: calc-size() 支持对固有尺寸(auto、min-content)进行计算和过渡。
/* Finally: Animate to auto height! */
.accordion-content {
height: 0;
overflow: hidden;
transition: height 0.3s ease;
}
.accordion-item.open .accordion-content {
height: calc-size(auto);
}
.radial-layout {
--_angle: calc(var(--sibling-index) * var(--_offset));
translate:
calc(cos(var(--_angle)) * var(--_circle-size))
calc(sin(var(--_angle)) * var(--_circle-size));
}
树计数函数(即将推出)
.staggered {
animation-delay: calc(sibling-index() * .1s);
background-color: hsl(sibling-count() 50% 50%);
}
使用 if() 实现条件 CSS(即将推出)
.dynamic {
color: if(
style(--theme: dark),
white,
black
);
}
架构与组织
层叠层
@layer reset, design-system, components, utilities;
@import "open-props/colors" layer(design-system);
@import "components/nav/base.css" layer(components.nav);
@layer components.nav.primary {
nav {
position: sticky;
inset-block-start: 0;
}
}
优点:
- 以较低的优先级导入第三方 CSS
- 按关注点而非选择器权重组织样式
- 嵌套层可创建清晰的层级结构
交互组件
对话框
<dialog id="modal">
<form method="dialog">
<button value="cancel">Cancel</button>
<button value="confirm">Confirm</button>
</form>
</dialog>
<button commandfor="modal" command="showModal">Open</button>
<button commandfor="modal" command="close">Close</button>
新增: closedby 属性支持轻触外部关闭行为
弹出层
<button popovertarget="menu">Show Menu</button>
<div popover id="menu">...</div>
popover=hint: 不会关闭其他弹出层的临时工具提示
[popover] {
transition:
display .5s allow-discrete,
overlay .5s allow-discrete,
opacity .5s;
@starting-style {
&:popover-open {
opacity: 0;
}
}
}
锚点定位
.tooltip-anchor {
anchor-name: --tooltip;
}
.tooltip[popover] {
position-anchor: --tooltip;
position-area: block-start;
position-try-fallbacks: flip-block;
position-try-order: most-height;
}
伪元素: anchor()、::scroll-button()、::scroll-marker()
独占式手风琴
<details name="accordion">...</details>
<details name="accordion">...</details>
可自定义的 Select
select {
appearance: base-select; /* Full CSS control */
}
/* Style options with rich HTML */
select option::before {
content: ""; /* Can include images, icons */
}
Search 元素
<search>
<form>
<input type="search" name="q">
<button type="submit">Search</button>
</form>
</search>
表单增强
字段尺寸
textarea, select, input {
field-sizing: content; /* Auto-grow to content */
}
textarea {
min-block-size: 3lh; /* Line-height units */
max-block-size: 80dvh;
}
更完善的验证伪类
/* Wait for user interaction before showing errors */
:user-invalid {
outline-color: red;
}
:user-valid {
outline-color: green;
}
label:has(+ input:user-invalid) {
text-decoration: underline wavy red;
}
Select 中的 HR
<select>
<option>Option 1</option>
<hr>
<option>Option 2</option>
</select>
视觉效果
滚动条样式
.custom-scrollbar {
scrollbar-color: hotpink transparent;
scrollbar-width: thin;
}
形状函数
.complex-clip {
clip-path: shape(
from 0% 0%,
curve by 50% 25% via 25% 50%,
line to 100% 100%
);
}
圆角形状
.fancy-corners {
corner-shape: squircle;
corner-shape: notch;
corner-shape: scoop;
corner-shape: superellipse(0.7);
}
渐进增强模式
功能检测
@supports (animation-timeline: view()) {
.fade-in {
animation: fade linear both;
animation-timeline: view();
}
}
@supports (container-type: inline-size) {
.responsive-card {
container-type: inline-size;
}
}
尊重用户偏好
@media (prefers-reduced-motion: no-preference) {
.animated {
animation: slide 1s ease;
}
}
@media (prefers-color-scheme: dark) {
:root {
--surface: #222;
}
}
@media (prefers-contrast: more) {
.text {
font-weight: 600;
}
}
检查浏览器支持情况:Baseline
什么是 Baseline? 一种统一了解跨浏览器功能可用性的方法。功能分为以下类别:
- 广泛可用: 所有主流浏览器都在过去 2.5 年内支持
- 现已推出: 所有主流浏览器均已支持
- 有限可用: 尚未在所有浏览器中推出
如何检查 Baseline 状态
- 最佳方式:访问 https://web-platform-dx.github.io/web-features-explorer/groups/ 并查找相应功能,然后访问其详情页。
- Can I Use: caniuse.com 会在每项功能的顶部显示 Baseline 标记
- MDN: 在浏览器兼容性表中查找 Baseline 标记
- web.dev: 功能介绍文章中包含 Baseline 状态
请记住: 始终检查 Baseline 状态,对前沿功能使用 @supports,并通过媒体查询尊重用户偏好。现代 CSS 注重渐进增强,并构建适用于所有人的健壮界面。
实际示例:现代组件
下面是一个使用了许多现代 CSS 功能的卡片组件:
/* Cascade layer for organization */
@layer components.card {
/* Custom properties with @property */
@property --card-hue {
syntax: "<number>";
inherits: false;
initial-value: 200;
}
.card {
/* Container for responsive design */
container: card / inline-size;
/* Logical properties */
inline-size: 100%;
padding-inline: var(--space-md);
padding-block: var(--space-lg);
/* Modern color system */
background: light-dark(
oklch(98% 0.02 var(--card-hue)),
oklch(20% 0.02 var(--card-hue))
);
/* Border with relative color */
border: 1px solid oklch(from var(--surface) calc(l * 0.9) c h);
/* Smooth corners */
border-radius: var(--radius-md);
/* View transition */
view-transition-name: --card;
/* Scroll-driven animation */
animation: fade-in linear both;
animation-timeline: view();
animation-range: entry 0% cover 30%;
/* Anchor for tooltips */
anchor-name: --card-anchor;
/* Transition custom property */
transition: --card-hue 0.5s var(--ease-spring-3);
&:hover {
--card-hue: 280;
}
/* Responsive typography in container */
@container card (width > 30ch) {
.card__title {
font-size: clamp(1.5rem, 3cqi, 2.5rem);
text-wrap: balance;
}
}
@container card (width < 30ch) {
.card__image {
aspect-ratio: 16 / 9;
object-fit: cover;
}
}
}
.card__title {
/* Text box trim for optical alignment */
text-box: trim-both cap alphabetic;
text-wrap: balance;
/* Logical margin */
margin-block-end: var(--space-sm);
}
.card__body {
text-wrap: pretty;
max-inline-size: 65ch;
}
.card__cta {
/* Inherit font */
font: inherit;
/* Accent color */
accent-color: var(--brand);
/* Field sizing */
field-sizing: content;
/* Logical properties */
padding-inline: var(--space-md);
padding-block: var(--space-sm);
/* Modern color with relative syntax */
background: oklch(from var(--brand) l c h);
color: oklch(from var(--brand) 95% 0.05 h);
&:hover {
background: oklch(from var(--brand) calc(l * 1.1) c h);
}
&:user-invalid {
outline: 2px solid light-dark(red, #ff6b6b);
}
}
/* Popover tooltip anchored to card */
.card__tooltip[popover] {
position-anchor: --card-anchor;
position-area: block-start;
position-try-fallbacks: flip-block;
/* Entry animation */
@starting-style {
opacity: 0;
scale: 0.9;
}
transition:
opacity 0.2s,
scale 0.2s,
display 0.2s allow-discrete,
overlay 0.2s allow-discrete;
}
/* Scroll state container queries */
@supports (container-type: scroll-state) {
.card__sticky-header {
container-type: scroll-state;
position: sticky;
inset-block-start: 0;
@container scroll-state(stuck: top) {
box-shadow: 0 2px 8px oklch(0% 0 0 / 0.1);
}
}
}
/* Respect user preferences */
@media (prefers-reduced-motion: reduce) {
.card {
animation: none;
transition: none;
}
}
@media (prefers-contrast: more) {
.card {
border-width: 2px;
}
}
}
/* Keyframes for scroll animation */
@keyframes fade-in {
from {
opacity: 0;
scale: 0.95;
}
to {
opacity: 1;
scale: 1;
}
}
示例的 HTML
<article class="card">
<img
class="card__image"
src="image.jpg"
alt="Description"
loading="lazy"
>
<h2 class="card__title">Card Title</h2>
<p class="card__body">
Card description with pretty text wrapping that avoids orphans.
</p>
<button
class="card__cta"
popovertarget="card-tooltip"
>
Learn More
</button>
<div
class="card__tooltip"
popover="hint"
id="card-tooltip"
>
Additional information appears here
</div>
</article>
权威资源
- CSS Wrapped 2025 - 年度 CSS 新特性
- The Coyier CSS Starter - 观点鲜明的现代基线
- Adam Argyle's CascadiaJS 2025 Deck -(已在本地转换为 Markdown,位于 ./argyle-cacadia-2025-deck.md)
- Modern CSS in Real Life - 实际应用
使用指南
-
优先考虑稳定性:
- 在生产代码中,推荐使用新近推出或广泛可用的特性。
- 使用有限可用的特性时,应采用渐进增强、优雅降级或
@supports。也可以询问用户希望如何处理。
-
使用 Web 平台:
- 对于布局、动画和交互,始终优先采用标准 CSS 方案,而非 JavaScript 库(例如,使用 CSS Masonry,而非 Masonry.js;使用 Popover API,而非自定义工具提示脚本)。
-
代码风格:
- 新建调色板时,使用现代色彩空间(
oklch)。
- 新建调色板时,使用现代色彩空间(
评论
支持 Markdown