Skip to content

打字机效果

打字机效果的CSS动画效果,纯CSS实现,无需JavaScript。

实时预览

Hello, CSS World!

代码

css
.typewriter {
  font-family: 'JetBrains Mono', 'Fira Code', monospace;
  font-size: 28px;
  font-weight: 700;
  color: #e2e8f0;
  overflow: hidden;
  white-space: nowrap;
  border-right: 3px solid #a78bfa;
  width: 0;
  animation:
    typing 2.5s steps(20, end) forwards,
    blink-caret 0.75s step-end infinite;
}

@keyframes typing {
  from { width: 0; }
  to { width: 20ch; }
}

@keyframes blink-caret {
  from, to { border-color: transparent; }
  50% { border-color: #a78bfa; }
}
html
<span class="typewriter">Hello, CSS World!</span>

纯 CSS 实现的打字机效果文字动画效果——通过渐变、阴影、滤镜和变换等 CSS 属性让文字产生视觉动效。所有文字内容保持可选中、可复制、对搜索引擎完全可见。

快速使用

  1. 复制 CSS 代码到你的样式文件中
  2. 在 HTML 中放入对应的文字结构(注意逐字效果需要每个字符包裹 <span>
  3. 根据你的文字内容调整 animation-delay 和文字数量
  4. 修改颜色和字号以匹配你的品牌风格

使用场景

  • 页面主标题:用渐变文字、霓虹发光或逐字弹跳效果吸引用户注意力
  • 品牌名称/Logo:3D 文字或故障效果传递技术与设计感
  • 营销活动页:彩虹文字、闪烁效果营造促销紧迫感
  • 技术博客/文档:打字机效果展示代码片段或技术标语

常见问题

Q: 文字动画在哪些浏览器下兼容? A: 大部分效果所有现代浏览器都支持。需注意:background-clip: text-webkit- 前缀;@property 仅 Chrome/Edge 支持。渐变文字和霓虹发光效果建议加降级方案。

Q: 动画文字会影响 SEO 吗? A: 不会。文字内容仍然在 HTML 中正常渲染,搜索引擎能完整抓取。CSS 动画只影响视觉呈现,不改变 DOM 结构和文本可访问性。

Q: 如何让逐字动画适配任意长度的文字? A: 用 JavaScript 动态给每个字符包裹 <span> 并设置 animation-delay: calc(var(--i) * 0.05s),或使用 CSS animation-delay 配合 nth-child 错位。

浏览器兼容性

浏览器兼容性
Chrome / Edge✅ 完全支持(含 @property
Firefox✅ 基本支持(不含 @property
Safari✅ 基本支持(需 -webkit-background-clip
移动端浏览器✅ 完全支持
IE 11❌ 渐变文字等高级效果不支持

使用说明

属性说明
steps(20, end)20 steps = 20 characters; adjust to match your text length
width: 20chch unit = width of "0"; set to your character count
border-rightSimulates the blinking cursor
white-space: nowrapPrevents text wrapping during reveal
animation-fill-mode: forwardsKeeps the final state after animation completes

Tip: Change the steps() count and width value to match the exact number of characters in your text.