w3resource

HTML-CSS: Typewriter effect

HTML-CSS : Exercise-11 with Solution

Using HTML, CSS, JavaScript create a typewriter effect animation.

  • Define two animations, typing to animate the characters and blink to animate the caret.
  • Use the :after pseudo-element to add the caret to the container element.
  • Use JavaScript to set the text for the inner element and set the --characters variable containing the character count. This variable is used to animate the text.
  • Use white-space: nowrap and overflow: hidden to make content invisible as necessary.

HTML Code:

<!--License: https://bit.ly/3GjrtVF--><!-- License information -->
<!DOCTYPE html><!-- Document type declaration -->
<html><!-- Opening html tag -->
<head><!-- Opening head tag -->
<meta charset="utf-8"><!-- Character encoding -->
<meta name="viewport" content="width=device-width"><!-- Viewport meta tag -->
<title>Using HTML, CSS, JavaScript create a typewriter effect animation</title><!-- Title of the webpage -->
</head><!-- Closing head tag -->
<body><!-- Opening body tag -->
<strong>Preview:</strong><br><!-- Preview heading -->
<div class="typewriter-effect"><!-- Container for typewriter effect -->
<div class="text" id="typewriter-text"></div><!-- Div element for displaying text with typewriter effect -->
</body><!-- Closing body tag -->
</html><!-- Closing html tag -->

Explanation:

  • <!--License: https://bit.ly/3GjrtVF-->: Provides information about the license for the code.
  • <!DOCTYPE html>: Declares the document type as HTML5.
  • <html>: Opening tag for the HTML document.
  • <head>: Opening tag for the head section containing metadata.
  • <meta charset="utf-8">: Specifies the character encoding of the document as UTF-8.
  • <meta name="viewport" content="width=device-width">: Configures the viewport to the device's width.
  • <title>Using HTML, CSS, JavaScript create a typewriter effect animation</title>: Sets the title of the webpage.
  • <body>: Opening tag for the body section of the HTML document.
  • <strong>Preview:</strong><br>: Displays a heading for the preview section.
  • <div class="typewriter-effect">: Defines a container for the typewriter effect.
  • <div class="text" id="typewriter-text"></div>: Creates a div element with the class "text" and the id "typewriter-text" for displaying text with the typewriter effect.

CSS Code:

<style>
/* Opening style tag for CSS */

.typewriter-effect {
  display: flex; /* Display as a flex container */
  justify-content: center; /* Center align the content horizontally */
  font-family: monospace; /* Use monospace font */
}

.typewriter-effect > .text {
  max-width: 0; /* Set initial width to 0 */
  animation: typing 3s steps(var(--characters)) infinite; /* Apply typing animation */
  white-space: nowrap; /* Prevent text from wrapping */
  overflow: hidden; /* Hide overflowing text */
}

.typewriter-effect:after {
  content: " |"; /* Display a blinking cursor */
  animation: blink 1s infinite; /* Apply blinking animation */
  animation-timing-function: step-end; /* Set animation timing function */
}

@keyframes typing {
  75%, /* At 75% of the animation duration */
  100% { /* Until the end of the animation */
    max-width: calc(var(--characters) * 1ch); /* Gradually increase width to show text */
  }
}

@keyframes blink {
  0%, /* At the beginning of the animation */
  75%, /* At 75% of the animation duration */
  100% { /* Until the end of the animation */
    opacity: 1; /* Fully visible */
  }
  25% { /* At 25% of the animation duration */
    opacity: 0; /* Invisible */
  }
}
</style>/* Closing style tag */

Explanation:

  • .typewriter-effect: Styles for the container of the typewriter effect.
  • .typewriter-effect > .text: Styles for the text element within the typewriter effect container.
  • .typewriter-effect:after: Styles for the cursor element after the text.
  • @keyframes typing: Keyframes for the typing animation.
  • @keyframes blink: Keyframes for the blinking cursor animation.

JavaScript Code:


<script>
const typeWriter = document.getElementById('typewriter-text');
const text = 'w3resource HTML-CSS Practical Exercises.';

typeWriter.innerHTML = text;
typeWriter.style.setProperty('--characters', text.length);
</script>

HTML-CSS Editor:

See the Pen html-css-practical-exercises by w3resource (@w3resource) on CodePen.


What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/html-css-exercise/html-css-practical-exercises/html-css-practical-exercise-11.php