Creating a Typing Animation Effect using HTML, CSS, and JS Tutorial

In this tutorial, you can learn to create a simple Typing Animation Effect Tutorial using HTML, CSS, and JS. The tutorial aims to provide students and beginners with a reference for learning how to create an interactive component animation for websites or web applications. Here, I will be providing a simple web page script that demonstrates the creation of a simple typing animation.

What is Typing Animation?

Basically, Typing Animation Effect is an element or component text animation of a website or web application which looks like live typing. Some developers used this type of animation to create interactive websites to provide their visitors with a better experience when they are using their sites or project.

How to create a Typing Animation Effect?

In websites or web applications, we can achieve Typing Animation using HTML elements, CSS properties/keyframes, and JavaScript built-in event listeners, methods, and APIs. We can simply add a class name or identification name to the HTML element that we want to put in a typing animation. Next, using the CSS scripts, we can simply design the text content and the typing cursor of the said animation. The JavaScript will make the animation functional and splits the characters of the element's text content to easily identify each to show it one by one which results in a typing animation. To understand it more and have an idea to achieve it in an actual script, check out the web page scripts that I created and provided below.

Sample Web Page

The scripts below result in a simple web page that display simple static content with Typing Animation Effect.

Page Interface

Here's the HTML file script known as index.html. It contains the page layout and animated text content element scripts.

  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <meta charset="UTF-8">
  4. <meta http-equiv="X-UA-Compatible" content="IE=edge">
  5. <meta name="viewport" content="width=device-width, initial-scale=1.0">
  6. <title>Typing Animation - HTML and CSS</title>
  7. <link rel="preconnect" href="https://fonts.googleapis.com">
  8. <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
  9. <link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@48,400,0,0" />
  10. <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
  11. <link rel="stylesheet" href="style.css">
  12. <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
  13. </head>
  14. <div class="content-md-lg py-3 h-100 d-flex align-items-center">
  15. <div class="col-lg-8 col-md-10 col-sm-12 col-12 mx-auto">
  16. <div class="page-title animate-typing">Typing Animation Effect using HTML and CSS</div>
  17. </div>
  18. </div>
  19. <script src="script.js"></script>
  20. </body>
  21. </html>

CSS

Next, here's the CSS file script named style.css. The file contains the stylesheet codes for the design of some elements and the page layout of the web page.

  1. @import url('https://fonts.googleapis.com/css2?family=Exo+2:ital,wght@0,100;0,200;0,300;0,400;0,500;0,600;0,700;0,800;0,900;1,100;1,200;1,300;1,400;1,500;1,600;1,700;1,800;1,900&display=swap');
  2.  
  3. *{
  4. box-sizing: border-box;
  5. font-family: 'Exo 2', sans-serif;
  6. }
  7. /**
  8. Page Design
  9. */
  10. body,
  11. html{
  12. height: 100%;
  13. width: 100%;
  14. margin: 0;
  15. padding: 0;
  16. overflow-x:hidden;
  17. }
  18. body{
  19. background-color: #282A3A;
  20. }
  21. .page-title{
  22. font-size: 2.5rem;
  23. font-weight: 500;
  24. color: #fff;
  25. letter-spacing: 3px;
  26. font-family: var(--secular-font);
  27. text-align: center;
  28. text-shadow: 0px 0px 3px #2020208c;
  29. }
  30. .border-dark-subtle{
  31. border-color: var(--border-dark-subtle) !important;
  32. }
  33.  
  34. /* Typing Animation Character Design*/
  35. .animate-typing>span:not(:nth-last-child(1)){
  36. color: #ff3e61;
  37. font-weight: 400;
  38. text-shadow: 1px 1px 3px #ffffff6e;
  39. }
  40. /* Typing Cursor Design */
  41. .animate-typing>span:nth-last-child(1){
  42. border-left:2px solid #fff;
  43. animation: blink 1s step-end infinite;
  44. }
  45. /* Animate Typing Cursor to blinking */
  46. @keyframes blink {
  47. 50%{
  48. visibility: hidden;
  49. }
  50. 100%{
  51. visibility: visible;
  52. }
  53. }
  54. /* Hide Element */
  55. .animate-typing>span.char-hide{
  56. display: none;
  57. }
  58.  

JavaScript

Lastly, here is the JavaScript file known as script.js. The file contains the code that makes the Typing Animation possible.

  1. // Select All Typing Element
  2. const typingEl = document.querySelectorAll('.animate-typing')
  3.  
  4. // Start Typing Animation
  5. const startTyping = (el) => {
  6. // Loop All Characters in Element
  7. [...el.querySelectorAll('span')].every(spanEl => {
  8. // Unhiding the first hidden span element
  9. if(spanEl.classList.contains('char-hide')){
  10. // Unhide Element
  11. spanEl.classList.remove('char-hide')
  12.  
  13. if(el.querySelectorAll('span.char-hide').length == 0){
  14. /**
  15. * Check if all characters are rendered/shown
  16. * - If Yes unhide it again after 3s and rerun animation
  17. */
  18. setTimeout(()=>{
  19. el.querySelectorAll('span').forEach((spanEL2, i)=>{
  20. // Hide only the Charactes and not the cursor element
  21. if((i+1) < el.querySelectorAll('span').length)
  22. spanEL2.classList.add('char-hide')
  23. })
  24. // Rerun Animation
  25. startTyping(el)
  26. },3000)
  27. }else{
  28. setTimeout(()=>{
  29. // Rerun Animation
  30. startTyping(el)
  31. },100)
  32. }
  33. return false
  34. }
  35. return true
  36. })
  37.  
  38. }
  39. // Initialize Typing Animation to All Elements that has a Typing Animation Class
  40. typingEl.forEach(el => {
  41. // Split All Characters
  42. var elTxtSplit = el.innerText.split("")
  43. // New Element HTML Content
  44. var newContent = ``;
  45. elTxtSplit.forEach(char =>{
  46. // Update Character enclosed in span element
  47. newContent += `<span class="char-hide">${char}</span>`;
  48. })
  49. // add typing cursor element
  50. newContent += `<span></span>`;
  51. // update the element content with the new content
  52. el.innerHTML = newContent
  53.  
  54. // Start Animation
  55. startTyping(el)
  56. })

Snapshots

Here are the snapshots of the overall result of the web page scripts I have provided above.

Typing Animation using HTML, CSS, and JS

Typing Animation using HTML, CSS, and JS

Typing Animation using HTML, CSS, and JS

DEMO

Typing Animation using HTML, CSS, and JS

There you go! I have provide also the complete source code zip of the web page scripts that I gave above on this site and it is free to download. The download button can be found after this tutorial's content. Feel Free to Download and modify it to do some experiments.

That's it! I hope this Creating a Typing Animation Effect using HTML, CSS, and JS Tutorial will help you with what you are looking for and will be useful for your current and future web application projects.

Explore more on this website for more Tutorials and Free Source Codes.

Happy Coding =)

Add new comment