Typewriter Effect in CSS

Create engaging character-by-character text animations using pure CSS with the steps() timing function

What Is the Typewriter Effect?

The typewriter effect has captivated web designers and developers for years, bringing a nostalgic yet modern touch to websites. This animation mimics the appearance of text being typed out character by character, creating an engaging visual experience that draws visitors' attention to key messages.

Modern web development with Next.js and CSS3 provides multiple approaches to implementing this effect, from pure CSS solutions to JavaScript-enhanced implementations, each with distinct advantages for different use cases. Our web development services team specializes in creating engaging animations that enhance user experience while maintaining optimal performance.

How the Typewriter Effect Works

At its core, the typewriter effect consists of two main components: the typing animation that reveals text progressively, and the cursor animation that creates the blinking cursor that accompanies the typing action.

The CSS typewriter effect works by animating the width of a text container from zero to its full width, combined with a blinking cursor animation. The key to achieving the classic typewriter look lies in the CSS steps() timing function, which creates the illusion of discrete character-by-character typing rather than smooth motion. This technique leverages CSS animations and keyframes to deliver a performant solution that works across modern browsers without requiring JavaScript.

The typing animation uses overflow: hidden to mask the unrevealed text, while the cursor creates the authentic typewriter appearance with a vertical bar that blinks on and off using a separate keyframe animation. The steps() function with parameters like steps(40, end) creates forty discrete steps from the starting width to the ending width, with each step appearing at the end of the interval. This combination creates a convincing simulation of text being typed in real-time, making it ideal for hero sections, introductions, and anywhere you want to make a strong visual statement.

Pure CSS Typewriter Implementation

The pure CSS approach to typewriter effects offers significant performance advantages and requires no JavaScript dependencies. By utilizing CSS keyframes with the steps() timing function, developers can create smooth character-by-character text reveal animations that execute efficiently on the GPU. This method is particularly well-suited for static content where the typing animation should play once on page load, such as hero headlines, welcome messages, and feature introductions.

Core CSS Properties

Creating a pure CSS typewriter effect requires combining several CSS properties to achieve the desired result. The foundation begins with setting overflow: hidden on the container to prevent text from wrapping during the animation, followed by white-space: nowrap to ensure the text remains on a single line. The border-right property creates the blinking cursor effect, typically set to a specific width and color with a solid style.

The typing animation itself uses @keyframes to transition the width from zero to one hundred percent, with the critical addition of animation-timing-function: steps(40, end) to create the stepped animation effect that mimics character-by-character typing. The blinking cursor animation alternates between visible and transparent states using a separate keyframe animation, typically with a half-second or three-quarter-second interval. Combining these elements with appropriate timing creates a convincing typewriter effect that plays automatically when the page loads, requiring no user interaction or JavaScript initialization.

Basic CSS Typewriter Effect
1.typewriter h1 {2 overflow: hidden;3 border-right: .15em solid orange;4 white-space: nowrap;5 margin: 0 auto;6 letter-spacing: .15em;7 animation:8 typing 3.5s steps(40, end),9 blink-caret .75s step-end infinite;10}11 12@keyframes typing {13 from { width: 0 }14 to { width: 100% }15}16 17@keyframes blink-caret {18 from, to { border-color: transparent }19 50% { border-color: orange; }20}

Understanding the steps() Timing Function

The steps() timing function is the secret ingredient that transforms a smooth width transition into a character-by-character typing animation. This function takes two parameters: the number of steps and the direction (either start or end). For typewriter effects, steps(40, end) creates forty discrete steps from the starting width to the ending width, with each step appearing at the end of the interval.

The steps() function works by defining key positions along the animation, with the browser calculating the intermediate states. For typewriter animations, each step reveals one or more characters, creating the appearance of discrete character reveals rather than smooth motion. The end direction means the animation holds at each step until transitioning to the next, which produces the classic typing effect where text appears suddenly rather than fading between characters.

Calculating Optimal Step Count

Choosing the correct number of steps directly impacts the quality of the typewriter effect. Too few steps result in multiple characters appearing simultaneously, breaking the illusion, while too many steps may not noticeably improve the animation but increase processing overhead. A good starting point is to use approximately one step per character in your text, adjusted based on the desired typing speed.

For a 40-character headline, steps(40, end) creates the smoothest typing effect with one character appearing per step. If your text is shorter, you can reduce the step count proportionally. The animation duration also affects the perceived typing speed--shorter durations with fewer steps create faster typing, while longer durations with more steps create slower, more deliberate typing. As noted in CSS-Tricks' comprehensive guide to typewriter effects, matching the step count to your character count ensures the most natural-looking animation.

steps() Function Options

Understanding the parameters for precise animation control

Number of Steps

The first parameter specifies how many discrete steps the animation should contain. Higher values create smoother typing effects with more character reveals.

Direction (start/end)

start shows the first frame immediately; end waits until the first interval completes. Most typewriter effects use 'end' for authentic typing appearance.

Duration Impact

Shorter durations create faster typing. Adjust steps proportionally to maintain smooth character-by-character animation across different speeds.

Performance Note

steps() is GPU-accelerated in modern browsers, making it more performant than JavaScript-based typing implementations that block the main thread.

Customizing Your Typewriter Effect

Modern CSS provides extensive customization options for typewriter effects beyond the basic implementation. These options allow you to match the animation to your brand identity, adjust typing speed for different contexts, and create unique cursor styles that enhance your design.

Typing Speed

The typing speed is controlled through the animation duration, with shorter durations creating faster typing and longer durations producing slower, more deliberate typing. For example, reducing the duration from 3.5s to 2s creates noticeably faster typing. When adjusting speed, remember to proportionally adjust the step count to maintain smooth character-by-character animation.

Letter Spacing

Adjusting the letter-spacing property affects both the visual appearance and the timing calculation. Wider spacing means fewer characters fit within the same width, potentially requiring fewer steps. The letter spacing also contributes to the overall aesthetic of the typewriter effect, with larger spacing creating a more dramatic, emphatic typing appearance.

Cursor Styling

The cursor can be customized through its border color, width, and animation timing. Using currentColor for the border color allows the cursor to automatically match the text color, while specific colors like orange or blue create a distinctive look. The cursor width affects how prominently it appears, with thinner cursors (0.1em) being more subtle and thicker cursors (0.3em) creating a stronger visual anchor.

Animation Iteration

By default, typewriter animations play once and stop with animation-fill-mode: forwards keeping the final text displayed. To create looping animations that continuously type and restart, add animation-iteration-count: infinite to the typing animation. You can also add delays before the animation starts using animation-delay to coordinate typewriter effects with other page animations.

Customized Typewriter Effect
1.typewriter-custom {2 /* Container styling */3 overflow: hidden;4 white-space: nowrap;5 border-right: 3px solid #3b82f6;6 7 /* Typing animation - faster speed */8 animation:9 typing 2s steps(30, end) forwards,10 cursor-blink 0.8s step-end infinite;11 12 /* Custom cursor styling */13 cursor: text;14 letter-spacing: 0.08em;15 16 /* Delay before starting */17 animation-delay: 0.5s;18}19 20/* Variant without cursor */21.typewriter-no-cursor {22 border-right: none;23 animation-name: typing;24 animation-iteration-count: 1;25 animation-fill-mode: forwards;26}

Advanced Typewriter Techniques

Beyond the basic typewriter effect, modern CSS enables more sophisticated implementations that handle multiple text strings, delete-and-retype animations, and multi-line content. These advanced techniques expand the creative possibilities of typewriter effects, allowing developers to create dynamic content displays, rotating messages, and more complex storytelling animations.

Multi-String Typewriter Effects

Creating a typewriter effect that cycles through multiple strings requires JavaScript to manage the sequence, though the core animation still relies on CSS. The typical approach involves hiding the current text, resetting the typing animation, and displaying the next string, creating a continuous cycle of typing and deleting. This technique is popular for hero sections that want to showcase multiple value propositions or features through animated text.

Implementing multi-string typewriter effects with JavaScript provides fine-grained control over timing, transitions, and user interaction. Libraries like TypeIt.js and Typed.js offer comprehensive solutions with extensive configuration options, including typing speed, cursor styles, loop behavior, and callback functions for triggering events between typing sequences. These libraries handle the complex logic of managing multiple strings while providing APIs that integrate smoothly with modern frameworks like React and Vue. For teams building interactive web applications, combining CSS animations with JavaScript libraries can create compelling user experiences that stand out.

Multi-Line Typewriter Effects

Typewriter effects on multi-line content present unique challenges since the basic single-line approach relies on width transitions. The primary solution involves wrapping each line in its own container and applying typewriter animations with appropriate delays, creating a staggered typing effect where lines appear sequentially. This technique works well for poems, verses, or content where lines naturally flow from one to the next.

An alternative approach to multi-line typewriter effects uses the CSS content property with pseudo-elements, though this method has limitations with dynamic content. The technique involves creating keyframes that specify content for each step of the animation, revealing text incrementally through CSS-generated content rather than width transitions. For dynamic or user-generated content, JavaScript-based solutions remain the most practical approach to multi-line typewriter effects.

Advanced Implementation FAQ

Performance and Best Practices

Implementing typewriter effects with performance in mind ensures smooth animations that enhance rather than detract from the user experience. CSS-based animations generally outperform JavaScript alternatives because they can be optimized by the browser's rendering engine and often run on a separate thread from the main JavaScript execution context.

GPU Acceleration Benefits

CSS animations benefit from browser optimizations including hardware acceleration, frame skipping prevention, and compositing on separate threads. The steps() timing function in particular creates predictable animation behavior that browsers can optimize efficiently. This means typewriter effects created with pure CSS are less likely to cause jank or frame drops, even on lower-powered devices. Our web development team prioritizes GPU-accelerated animations for optimal performance across all devices.

JavaScript-based typewriter effects, while more flexible, must calculate and apply each character manually, potentially blocking the main thread during long typing sequences or causing inconsistent frame rates on resource-constrained devices. When performance is critical--such as on landing pages with heavy visual content or mobile-first designs--pure CSS typewriter effects provide the best balance of visual appeal and technical efficiency.

Main Thread Considerations

The performance difference between CSS and JavaScript typewriter implementations stems from how each approach handles animation frames. JavaScript-based approaches can block the main thread during execution, potentially affecting other page interactions. CSS animations, by contrast, are handled by the browser's compositor thread, allowing smooth animation even while JavaScript continues executing. For complex typewriter behaviors that require JavaScript, developers should consider using requestAnimationFrame for smooth updates and implementing content pre-calculation to minimize runtime processing.

Performance Comparison

60fps

Target frame rate for smooth CSS animations

1

Thread for CSS animation rendering (separate from main)

0

JavaScript required for pure CSS typewriter effects

30-40

Recommended step count for most text lengths

Accessibility Considerations

Accessibility should be a primary concern when implementing typewriter effects, as animated content can create barriers for users with vestibular disorders, motion sensitivity, or cognitive differences. Implementing proper accessibility features ensures your animations enhance the experience for all users rather than creating obstacles.

Reduced Motion Fallback

The CSS prefers-reduced-motion media query allows developers to detect when users have requested reduced motion through their operating system preferences, providing an opportunity to disable or simplify the typewriter animation. A respectful implementation checks for this preference and provides a static fallback, ensuring the content remains accessible while respecting user settings.

Screen readers and assistive technologies may have difficulty with typewriter effects that rely on CSS content changes or width transitions. Ensuring that the final typed content is immediately available in the DOM--even if visually hidden until the animation completes--helps maintain accessibility. Additionally, avoiding display: none or visibility: hidden on typewriter content during the animation prevents screen readers from skipping the content entirely. For content that types repeatedly or loops indefinitely, providing a clear indication that the animation is repeating helps users understand what they are viewing.

Reduced Motion Fallback
1@media (prefers-reduced-motion: reduce) {2 .typewriter {3 animation: none;4 border-right: none;5 width: 100% !important;6 }7 8 .typewriter::after {9 display: none;10 }11}

Complete Code Examples

Practical implementation examples for various typewriter effect scenarios, from hero section implementations to responsive designs that adapt to different screen sizes.

Hero Section Typewriter

Hero section typewriter effects require careful attention to responsiveness and timing. The animation should be prominent enough to draw attention without delaying content visibility. Using animation-fill-mode: forwards ensures the text remains visible after typing completes, and animation-delay provides a brief pause before the animation begins.

The implementation includes responsive adjustments that slow the typing on mobile devices where users may have less patience for animations. The cursor styling uses currentColor to automatically match the text color, creating a cohesive appearance. Adding display: inline-block ensures the container only spans the text width rather than the full parent element, creating a more polished result. For complex animations that integrate with interactive elements, our AI automation services can help create smart, responsive animations that adapt to user behavior.

Hero Typewriter Component
1.hero-typewriter {2 position: relative;3 display: inline-block;4 overflow: hidden;5 white-space: nowrap;6 border-right: 2px solid currentColor;7 8 /* Adjust typing speed here */9 animation:10 typing 2.5s steps(35, end) forwards,11 blink-caret 0.75s step-end infinite;12 13 animation-delay: 0.3s;14 animation-fill-mode: both;15}16 17/* Responsive adjustments */18@media (max-width: 768px) {19 .hero-typewriter {20 animation-duration: 3s;21 letter-spacing: 0.05em;22 }23}

Conclusion

The typewriter effect remains a powerful tool for creating engaging, memorable web experiences that draw attention to key content. Modern CSS provides efficient, performant implementations through the steps() timing function and CSS keyframes, enabling developers to create character-by-character text reveal animations without JavaScript dependencies. For more complex requirements, JavaScript libraries extend these capabilities to handle multiple strings, delete-and-retype sequences, and dynamic content while maintaining smooth performance through optimized animation techniques.

When implementing typewriter effects, prioritizing performance and accessibility ensures the animation enhances rather than hinders the user experience. Using CSS animations where possible, providing reduced-motion fallbacks, and ensuring content remains accessible to assistive technologies demonstrates thoughtful implementation that serves all users. The key is balancing visual impact with technical excellence, creating typewriter effects that captivate visitors while respecting their devices and preferences.

Key Takeaways

  • Pure CSS typewriter effects use steps() for character-by-character animation
  • The cursor is created with border-right and a blinking keyframe animation
  • Match step count to character count for optimal typing effect
  • Always implement reduced-motion fallbacks for accessibility
  • Test performance on mobile devices to ensure smooth animation
  • Keep typewriter content concise for maximum impact
  • Use GPU-accelerated CSS animations for best performance

For professional web development services that incorporate engaging animations and modern CSS techniques, our team can help bring your vision to life with performant, accessible implementations. Contact our web development specialists to discuss how we can enhance your website with thoughtful animations.

Ready to Enhance Your Website?

Our web development team creates engaging, performant animations that elevate user experience while maintaining accessibility standards.

Sources

  1. CSS-Tricks: Typewriter Effect - Comprehensive CSS-only typewriter effect using steps() animation with blinking cursor
  2. Prismic: CSS Text Animations - Modern CSS animation techniques including typewriter effects and best practices
  3. Handoff.design: Creating a Typing Effect with CSS Steps - Detailed explanation of CSS steps() function for typewriter animations