DEEP SPACE纯 CSS 星点飘动
星空粒子飘动
Starfield particles
多颗细小星点错开节奏缓慢飘动并微微明暗呼吸,只用 CSS 不耗主线程
常见大白话搜法:星空背景粒子漂浮、深色背景星点闪烁、轻量背景颗粒飘动、只用CSS的星空装饰
- 适用位置
- 背景
- 气质感觉
- 柔和、科技感
- 触发方式
- 一直循环
- 适合网站
- 品牌官网、个人网站、工具和应用
实现原理与无障碍底线
在隔离层叠上下文的背景容器里放置多颗带 aria-hidden="true" 和 pointer-events: none 的小圆点,通过 CSS 变量设置飘动时长和幅度,用 @keyframes 仅驱动 translate 与 opacity 两个合成层属性,各颗星按比例错开 animation-delay,避免重排且不引入 JS/canvas 开销;手机端自动减少星点数量,减少动态时保持静止。
适用场景:深色首屏背景、AI 产品宣传区、极客个人主页,在文字下方营造静谧的太空星点氛围
减少动态(prefers-reduced-motion):关闭飘动和明暗呼吸动画,星点静止在原处作为静态底纹
移动端与触屏:窄屏下自动隐藏部分星点(减少颗粒密度),仅动合成层属性保持流畅
关键可调参数
| 参数 | 键名 | 默认值 | 建议范围 | 说明 |
|---|---|---|---|---|
| 一圈时长 | duration | 5000ms | — | — |
| 飘动幅度 | distance | 10px | — | — |
可直接复制给 AI(Cursor / Claude / v0)的提示词
请在我的项目里给背景加上「星空粒子飘动」(Starfield particles)效果:在隔离层叠上下文的背景容器里放置多颗带 aria-hidden="true" 和 pointer-events: none 的小圆点,通过 CSS 变量设置飘动时长和幅度,用 @keyframes 仅驱动 translate 与 opacity 两个合成层属性,各颗星按比例错开 animation-delay,避免重排且不引入 JS/canvas 开销;手机端自动减少星点数量,减少动态时保持静止。
参数:一圈时长 5000ms,飘动幅度 10px。
要求:保留原有的内容和布局;系统开启"减少动态"时直接显示最终状态;脚本出错或没有执行时内容仍然可见。
参考:https://donglang.linter.top/effects/starfield.md(原理、参数和参考代码)原生无依赖参考代码
CSS
/* 星空粒子飘动(Starfield particles)· 只用 CSS,不需要脚本。 */
:where(.stf) {
--stf-duration: 5000ms;
--stf-distance: 10px;
--stf-color: currentColor;
position: relative;
}
.stf {
isolation: isolate;
}
.stf-dots {
position: absolute;
inset: 0;
z-index: -1;
overflow: hidden;
pointer-events: none;
}
.stf-dots > i {
position: absolute;
border-radius: 50%;
background: var(--stf-color);
animation: stf-drift var(--stf-duration) ease-in-out infinite;
}
.stf-dots > i:nth-child(2) { animation-delay: calc(var(--stf-duration) * 0.16); }
.stf-dots > i:nth-child(3) { animation-delay: calc(var(--stf-duration) * 0.32); }
.stf-dots > i:nth-child(4) { animation-delay: calc(var(--stf-duration) * 0.48); }
.stf-dots > i:nth-child(5) { animation-delay: calc(var(--stf-duration) * 0.64); }
.stf-dots > i:nth-child(6) { animation-delay: calc(var(--stf-duration) * 0.80); }
:where(.stf-dots > i:nth-child(1)) { left: 12%; top: 24%; width: 4px; height: 4px; }
:where(.stf-dots > i:nth-child(2)) { left: 28%; top: 72%; width: 5px; height: 5px; }
:where(.stf-dots > i:nth-child(3)) { left: 48%; top: 18%; width: 3px; height: 3px; }
:where(.stf-dots > i:nth-child(4)) { left: 68%; top: 68%; width: 5px; height: 5px; }
:where(.stf-dots > i:nth-child(5)) { left: 82%; top: 28%; width: 4px; height: 4px; }
:where(.stf-dots > i:nth-child(6)) { left: 90%; top: 78%; width: 3px; height: 3px; }
@keyframes stf-drift {
0%, 100% {
translate: 0 0;
opacity: 0.35;
}
50% {
translate: 0 calc(var(--stf-distance) * -1);
opacity: 0.95;
}
}
@media (max-width: 720px) {
.stf-dots > i:nth-child(n + 5) {
display: none;
}
}
@media (prefers-reduced-motion: reduce) {
.stf-dots > i {
animation: none;
opacity: 0.5;
}
}HTML
<section class="stf" style="padding:48px;background:#141519;color:#ffffff;border-radius:14px;">
<span class="stf-dots" aria-hidden="true">
<i></i><i></i><i></i><i></i><i></i><i></i>
</span>
<h2>星空背景区</h2>
<p>细小星点在文字下方缓缓飘动。</p>
</section>