YL

TEXT EFFECT

故障文字Glitch Text

文字带红青双色错位的抖动,像信号故障。ReactBits 的 GlitchText、Magic UI 的 Hyper Text 都在这个家族。

中英对照

中文
故障文字
英文
Glitch Text
别名
Glitch Effect、RGB Split、Cyberpunk Text
一句话
文字带红青双色错位的抖动,像信号故障。ReactBits 的 GlitchText、Magic UI 的 Hyper Text 都在这个家族。

效果预览

GLITCH

适合用在哪

赛博/科幻主题、游戏与音乐产品、报错页的趣味化表达。字幕式标题慎用,可读性会下降。

不适合:医疗、金融、政务这类需要传递可靠感的页面:故障效果本身就是「出错」的视觉隐喻;多行标题、正文和 16px 以下的小字也不适合,红青错位会让笔画边缘糊在一起,几乎无法阅读。

和相近模式的区别

文字乱码解密Text Scramble

乱码解密改的是字符本身:先显示随机字符,再逐位落定成真实文字,结束后就是干净的标题,适合品牌名揭示;故障文字从不改变字符,只是在主文字后面叠两层红青副本错位抖动,主文字始终可读,适合营造赛博、信号干扰的氛围,同样要控制在 5 秒内停下。

容易踩的坑

  • 抖动频率过快或整屏闪烁,可能触发光敏性癫痫风险。
  • 伪元素复制的 data-t 文案未同步更新,红青层显示的是旧文字。
  • 用于超过一行的正文或小于 16px 的字,错位后几乎无法阅读。

设计提示词 · Prompt

故障文字(Glitch Text):主文本正常显示,另外用 ::before 与 ::after 复制同一段文字(content:attr(data-t)),分别染成红(#ef4444)与青(#06b6d4)。::before 用 clip-path:inset(0 0 58% 0) 只露上半,::after 用 inset(56% 0 0 0) 只露下半。两个伪元素放在主文字后面(父元素 isolation:isolate,伪元素 z-index:-1),在 2.4s steps(1, end) 里播放 2 遍(共 4.8 秒)后停下,各自做 2-3px 的反向位移抖动(translate(-2.5px,-1.5px) 等关键帧,任意 1 秒内位置变化不超过 3 次)。伪元素的 content 写成 attr(data-t) / "",避免读屏把复制的文字再读一遍;prefers-reduced-motion 与 forced-colors 时隐藏两个伪元素。主文字保持不动,保证还能读。
Glitch Text: the main text displays normally, while ::before and ::after duplicate the same text (content:attr(data-t)) and are tinted red (#ef4444) and cyan (#06b6d4) respectively. ::before uses clip-path:inset(0 0 58% 0) to reveal only the top half, and ::after uses inset(56% 0 0 0) to reveal only the bottom half. Place both pseudo-elements behind the main text (isolation:isolate on the parent, z-index:-1 on the pseudo-elements) and let them jitter 2-3px in opposite directions with 2.4s steps(1, end), played twice (4.8s in total) and then stopping (keyframes like translate(-2.5px,-1.5px), no more than 3 position changes in any one second). Write the pseudo-element content as attr(data-t) / "" so screen readers do not read the copied text again; hide both pseudo-elements under prefers-reduced-motion and forced-colors. The main text stays still so it remains readable.

可复制 CSS

自包含的最小实现,粘贴到空白页面即可生效;类名可以按项目规范改名。

/* <h1 class="glitch" data-t="GLITCH">GLITCH</h1>;data-t 必须与文字完全一致 */
.glitch {
  position: relative;
  isolation: isolate;
  display: inline-block;
  margin: 0;
  color: #111827;
  font: 800 clamp(40px, 7vw, 80px)/1 system-ui, sans-serif;
  letter-spacing: .02em;
}
.glitch::before,
.glitch::after {
  content: attr(data-t);
  /* 替代文本为空,读屏不会把复制的文字再读一遍;不支持此写法的浏览器会忽略这一行 */
  content: attr(data-t) / "";
  position: absolute;
  inset: 0;
  z-index: -1;
  pointer-events: none;
}
/* 每 0.6 秒换一次位置,每秒约 1.7 次变化;播 2 遍共 4.8 秒后停在原位,藏回主文字后面 */
.glitch::before {
  color: #ef4444;
  clip-path: inset(0 0 58% 0);
  animation: glitch-a 2.4s steps(1, end) 2;
}
.glitch::after {
  color: #06b6d4;
  clip-path: inset(56% 0 0 0);
  animation: glitch-b 2.4s steps(1, end) 2;
}
@keyframes glitch-a {
  25% { transform: translate(-2.5px, -1.5px); }
  50% { transform: translate(2px, 1px); }
  75% { transform: translate(-1.5px, .5px); }
}
@keyframes glitch-b {
  25% { transform: translate(2.5px, 1.5px); }
  50% { transform: translate(-2px, -1px); }
  75% { transform: translate(1.5px, -.5px); }
}
@media (prefers-reduced-motion: reduce) {
  .glitch::before,
  .glitch::after { display: none; }
}
@media (forced-colors: active) {
  .glitch::before,
  .glitch::after { display: none; }
}

演示 HTML 结构

<div class="s d-center">
  <div class="tx-glitch" data-t="GLITCH">GLITCH</div>
</div>

无障碍与性能

无障碍

  • 伪元素的 content 写成 attr(data-t) / "",替代文本为空:在 Chromium 中,不加这一行时标题的可访问名称是「GLITCH GLITCH GLITCH」(::before、正文、::after 各一遍),加上后恢复为「GLITCH」。不支持这种写法的浏览器会忽略它,退回第一行,这时可以改用两个 aria-hidden 的 span 代替伪元素。
  • 闪动频率:位置每 0.6 秒跳一次,每秒约 1.7 次变化,一来一回约 0.8 次,低于 SC 2.3.1 每秒 3 次的上限;而且变化的只是 2-3px 宽的彩色边缘,面积很小。
  • 播 2 遍共 4.8 秒后停下,低于 SC 2.2.2 的 5 秒;prefers-reduced-motion: reduce 和强制颜色模式(forced-colors: active)下直接隐藏红青副本,只剩清晰的主文字。

性能

  • 只动画两个伪元素的 transform;clip-path 是静态的,不参与动画。
  • 副本放在主文字后面(isolation: isolate 加 z-index: -1),静止时完全被主文字盖住,不需要额外隐藏。

常见问题

故障文字的英文是什么?

故障文字的英文是 Glitch Text。常见英文别名还包括 Glitch Effect、RGB Split、Cyberpunk Text。

Glitch Text 的中文是什么?

Glitch Text 的中文是「故障文字」。文字带红青双色错位的抖动,像信号故障。ReactBits 的 GlitchText、Magic UI 的 Hyper Text 都在这个家族。

故障文字的闪动频率怎么算,才不会触发光敏风险?

WCAG SC 2.3.1 要求任意 1 秒内闪烁不超过 3 次,或者闪烁面积和亮度变化都低于阈值;一次闪烁指亮度一升一降的一对相反变化,饱和红色的闪烁另有更严格的「红闪」阈值。按关键帧推算:本条 2.4 秒里有 4 个位置,steps(1, end) 让每段直接跳变,每秒约 1.7 次变化,折合不到 1 次闪烁。如果改成 steps(2),每段关键帧会再多跳一次,变化次数翻倍;关键帧越多、时长越短,频率越高,改参数后要重新算一遍;也不要让整块背景跟着闪。

用伪元素复制的文字会被读屏读两遍吗?

会。CSS 生成的 content 文字会进入无障碍树,content: attr(data-t) 会让标题的可访问名称变成两遍或三遍原文。解决办法是 CSS 生成内容的替代文本语法:content: attr(data-t) / "",斜杠后的空字符串表示这段内容对读屏不可见。较新的主流浏览器已支持这种写法;为了兼容,先写一行不带替代文本的 content 再写这一行,不支持的浏览器会把第二行当作无效声明忽略。

常见搭配

同一页面里经常和「故障文字」一起出现的模式,可以收藏后用「组合收藏为整页提示词」一起交给 AI。

所属设计风格

「故障文字」是这些整体设计风格的常见组成部分,在设计风格词典里可以看到整页效果。

标签