198 字
1 分钟
CSS 文字打字机效果

效果说明#

使用纯 CSS 实现的打字机效果:

  • ⌨️ 模拟逐字打出的动画
  • ✏️ 闪烁的光标效果
  • 🎨 可自定义颜色和速度

完整代码#

<!DOCTYPE html>
<html>
<head>
<style>
body {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background: #1a1a2e;
font-family: 'Courier New', monospace;
}
.typewriter h1 {
color: #e2e8f0;
font-size: 2rem;
overflow: hidden;
border-right: 3px solid #63b3ed;
white-space: nowrap;
margin: 0 auto;
letter-spacing: 0.1em;
animation:
typing 3.5s steps(30, end),
blink-caret 0.75s step-end infinite;
}
@keyframes typing {
from { width: 0 }
to { width: 100% }
}
@keyframes blink-caret {
from, to { border-color: transparent }
50% { border-color: #63b3ed }
}
</style>
</head>
<body>
<div class="typewriter">
<h1>Hello, I'm a developer.</h1>
</div>
</body>
</html>

原理解析#

  1. overflow: hidden + white-space: nowrap 让文字不换行且超出隐藏
  2. typing 动画通过改变 width 从 0 到 100%
  3. steps(30, end) 让动画分步进行,模拟打字效果
  4. blink-caret 让右边框闪烁,模拟光标
CSS 文字打字机效果
https://blog.singlelyra.top/posts/demo-css-typewriter/
作者
ByteCraft
发布于
2026-03-08
许可协议
CC BY-NC-SA 4.0