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>原理解析
overflow: hidden+white-space: nowrap让文字不换行且超出隐藏typing动画通过改变width从 0 到 100%steps(30, end)让动画分步进行,模拟打字效果blink-caret让右边框闪烁,模拟光标