{"id":25339,"date":"2022-11-30T10:21:26","date_gmt":"2022-11-30T18:21:26","guid":{"rendered":"https:\/\/coderpad.io\/?p=25339"},"modified":"2023-06-05T13:48:57","modified_gmt":"2023-06-05T20:48:57","slug":"how-to-create-a-typewriter-effect-in-vue","status":"publish","type":"post","link":"https:\/\/coderpad.io\/blog\/development\/how-to-create-a-typewriter-effect-in-vue\/","title":{"rendered":"How To Create A Typewriter Effect In Vue"},"content":{"rendered":"\n<p><em><strong>Typewriter effect<\/strong><\/em> is an animation that gives the impression that text is being typed on the screen one letter at a time, which can be a great way to grab a user&#8217;s attention. The typewriter effect can be created by using either CSS or JavaScript. Depending on the effect we want to create we will need to choose one over the other.<\/p>\n\n\n\n<p>When using only CSS, we don\u2019t have access to the text used for the effect but with JavaScript, we do, which means JavaScript has more potential to create a more feature-rich typewriter effect.&nbsp; For example, JavaScript is the best option if we want to create a deleting-and-retyping effect like the one on <a href=\"https:\/\/app.coderpad.io\/register\">CoderPad\u2019s registration page<\/a>:<\/p>\n\n\n\n<figure class=\"wp-block-video aligncenter\"><video height=\"130\" style=\"aspect-ratio: 306 \/ 130;\" width=\"306\" autoplay controls loop muted src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/tweffect.mp4\"><\/video><\/figure>\n\n\n\n<p>In this tutorial, we will learn how to create a coding typewriter effect that includes syntax highlighting just like the one used on Codepen\u2019s landing page.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Setting up<\/h2>\n\n\n\n<p>For this article, we&#8217;ll be making use of the Vue environment on the <a href=\"https:\/\/app.coderpad.io\/sandbox\">CoderPad sandbox<\/a>. To create a new Vue sandbox, click on the <strong>Language<\/strong> tab on the sidebar and select <strong>Vue<\/strong> from the <strong>Frontend<\/strong> section.<\/p>\n\n\n\n<p>For adding syntax highlighting to our typewriter effect we will be using <a href=\"https:\/\/www.npmjs.com\/package\/vue3-highlightjs\" target=\"_blank\" rel=\"noopener\">vue3-highlightjs<\/a>. In the sandbox shell, install the <code>vue3-highlightjs<\/code> library:<\/p>\n\n\n<pre class=\"wp-block-code\"><span><code class=\"hljs shcb-wrap-lines\">$ npm i vue3-highlightjs<\/code><\/span><\/pre>\n\n\n<blockquote class=\"wp-block-quote\">\n<p>\ud83d\udca1 The CoderPad sandbox automatically starts your application and applies changes made during development without needing a manual refresh. <\/p>\n<\/blockquote>\n\n\n\n<h2 class=\"wp-block-heading\">Creating a typewriter effect with CSS<\/h2>\n\n\n\n<p>The typewriter effect in CSS is achieved by gradually revealing the text by increasing the width of its element from 0 to 100% using the CSS animation <code>steps()<\/code> function. Then you\u2019ll add a blinking border that acts as a cursor that types out the text. Let\u2019s learn how it\u2019s done with a simple <code>console.log<\/code> example.<\/p>\n\n\n\n<p>First, we need to define the text to be used for the typewriter effect.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-1\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">setup<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">template<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">code<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"typingEffect\"<\/span>&gt;<\/span>\n        console.log('Hello world')\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">code<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">template<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span> <span class=\"hljs-attr\">scoped<\/span>&gt;<\/span><span class=\"css\">\n  <span class=\"hljs-selector-tag\">code<\/span> {\n    <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#202129<\/span>;\n    <span class=\"hljs-attribute\">border-radius<\/span>: <span class=\"hljs-number\">4px<\/span>;\n    <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">20px<\/span>;\n    <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">70px<\/span>;\n    <span class=\"hljs-attribute\">color<\/span>: white;\n    <span class=\"hljs-attribute\">display<\/span>: flex;\n    <span class=\"hljs-attribute\">margin<\/span>: <span class=\"hljs-number\">0<\/span> auto;\n    <span class=\"hljs-attribute\">align-items<\/span>: center;\n    <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">350px<\/span>;\n  }\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-1\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Next, let\u2019s add the typing animation.&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-2\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.typingEffect<\/span> {\n    <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">0<\/span>;\n    <span class=\"hljs-attribute\">overflow<\/span>: hidden; <span class=\"hljs-comment\">\/* Ensure the text is not visible until the typewriter effect*\/<\/span>\n    <span class=\"hljs-attribute\">border-right<\/span>: <span class=\"hljs-number\">2px<\/span> solid white; <span class=\"hljs-comment\">\/* The cursor*\/<\/span>\n    <span class=\"hljs-attribute\">font-size<\/span>: <span class=\"hljs-number\">16px<\/span>;\n    <span class=\"hljs-attribute\">white-space<\/span>: nowrap; <span class=\"hljs-comment\">\/* Keeps the text on a single line *\/<\/span>\n    <span class=\"hljs-attribute\">animation<\/span>: typing <span class=\"hljs-number\">2s<\/span> forwards;\n  }\n\n<span class=\"hljs-comment\">\/* The typing animation *\/<\/span>\n<span class=\"hljs-keyword\">@keyframes<\/span> typing {\n  <span class=\"hljs-selector-tag\">from<\/span> { <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">0<\/span> }\n  <span class=\"hljs-selector-tag\">to<\/span> { <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">100%<\/span> }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-2\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>After adding the above styles, here is what it will look like:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_63879a17cfb11.gif\" alt=\"\"\/><figcaption class=\"wp-element-caption\">The sentence <strong>console.log(&#8216;Hello world&#8217;)<\/strong> is printed word by word.<\/figcaption><\/figure>\n\n\n\n<p>We can see some typing animation going on but this obviously isn\u2019t what a typewriter effect looks like \u2013 we want to reveal the text letter by letter. This is where the CSS <code>steps()<\/code> function comes in. With the <code>steps()<\/code> function we can specify the number of steps the animation should take before completion.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-3\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.typingEffect<\/span> {\n  \/\/\u2026\n  <span class=\"hljs-attribute\">animation<\/span>: typing <span class=\"hljs-number\">2s<\/span> <span class=\"hljs-built_in\">steps<\/span>(<span class=\"hljs-number\">30<\/span>) forwards;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-3\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here is what we get:<\/p>\n\n\n\n<figure class=\"wp-block-image is-resized\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_63879a19c7584.gif\" alt=\"\" width=\"840\" height=\"102\"\/><figcaption class=\"wp-element-caption\">The sentence <strong>console.log(&#8216;Hello world&#8217;)<\/strong> is printed word by word with a brief pause after printing.<\/figcaption><\/figure>\n\n\n\n<p>That&#8217;s more like it! If we still want to adjust the speed to make it slower or faster we can just tweak the animation delay and <code>steps()<\/code> function.<\/p>\n\n\n\n<p>There is one more thing left to add to get a perfect typewriter effect, and that is the blinking cursor. This can easily be achieved by infinitely animating the background color of the border from transparent to its original color with the following animation.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-4\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-keyword\">@keyframes<\/span> blink {\n  0%, 45% {\n    <span class=\"hljs-attribute\">border-color<\/span>: transparent;\n  }\n  50%, 100% {\n    <span class=\"hljs-attribute\">border-color<\/span>: white;\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-4\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Next, modify the type <code>typingEffect<\/code> class to use the <code>blink<\/code> animation.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-5\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.typingEffect<\/span> {\n  \/\/\u2026\n  <span class=\"hljs-attribute\">animation<\/span>: typing <span class=\"hljs-number\">2s<\/span> <span class=\"hljs-built_in\">steps<\/span>(<span class=\"hljs-number\">30<\/span>) forwards, blink <span class=\"hljs-number\">1s<\/span> infinite;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-5\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, using what we have learned, let\u2019s create the \u201ccoding\u201d typewriter effect like you\u2019d see in an IDE. First, let\u2019s define the text for the typewriter effect:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-6\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">setup<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">template<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">code<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"typingEffect__line1\"<\/span>&gt;<\/span>\n        function helloWorld() {\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"typingEffect__line2\"<\/span>&gt;<\/span>\n        console.log('Hello world')\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">br<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"typingEffect__line3\"<\/span>&gt;<\/span>}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">code<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">template<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span> <span class=\"hljs-attr\">scoped<\/span>&gt;<\/span><span class=\"css\">\n  <span class=\"hljs-selector-tag\">code<\/span> {\n    <span class=\"hljs-attribute\">display<\/span>: block;\n    <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#202129<\/span>;\n    <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">20px<\/span>;\n    <span class=\"hljs-attribute\">color<\/span>: white;\n    <span class=\"hljs-attribute\">overflow<\/span>: hidden;\n    <span class=\"hljs-attribute\">margin<\/span>: <span class=\"hljs-number\">0<\/span> auto;\n    <span class=\"hljs-attribute\">align-items<\/span>: center;\n    <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">350px<\/span>;\n    <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">130px<\/span>;\n  }\n\n  <span class=\"hljs-selector-tag\">code<\/span> <span class=\"hljs-selector-tag\">div<\/span> {\n    <span class=\"hljs-attribute\">display<\/span>: inline-block;\n  }\n\n  <span class=\"hljs-selector-class\">.typingEffect__line2<\/span> {\n    <span class=\"hljs-attribute\">padding-left<\/span>: <span class=\"hljs-number\">24px<\/span>;\n    <span class=\"hljs-attribute\">box-sizing<\/span>: border-box;\n  }\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-6\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Above we are placing each line of code in a different paragraph element so that the typing animation can be done a line at a time and proper indentation can be added to the code. Here is the result:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_63879a1c0b154.png\" alt=\"\"\/><figcaption class=\"wp-element-caption\">A <code>helloWorld()<\/code> function that logs &#8220;Hello world&#8221; to when called.<\/figcaption><\/figure>\n\n\n\n<p>Next, to add the typewriter effect where the code will be revealed line by line, we will be adding an animation delay to the second and third lines of code to prevent their animation from starting until the previous one is complete.&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-7\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-tag\">code<\/span> <span class=\"hljs-selector-tag\">p<\/span> {\n  <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">0<\/span>;\n  <span class=\"hljs-attribute\">margin-right<\/span>: <span class=\"hljs-number\">5px<\/span>;\n  <span class=\"hljs-attribute\">overflow<\/span>: hidden;\n  <span class=\"hljs-attribute\">border-right<\/span>: <span class=\"hljs-number\">2px<\/span> solid transparent;\n  <span class=\"hljs-attribute\">white-space<\/span>: nowrap;\n}\n\n<span class=\"hljs-selector-class\">.typingEffect__line1<\/span> {\n  <span class=\"hljs-attribute\">animation<\/span>: typing <span class=\"hljs-number\">2s<\/span> <span class=\"hljs-built_in\">steps<\/span>(<span class=\"hljs-number\">30<\/span>) forwards, blink <span class=\"hljs-number\">1s<\/span> <span class=\"hljs-number\">3<\/span>;\n}\n\n<span class=\"hljs-selector-class\">.typingEffect__line2<\/span> {\n  <span class=\"hljs-attribute\">animation<\/span>: typing <span class=\"hljs-number\">2.5s<\/span> <span class=\"hljs-built_in\">steps<\/span>(<span class=\"hljs-number\">35<\/span>) <span class=\"hljs-number\">3s<\/span> forwards, blink <span class=\"hljs-number\">1s<\/span> <span class=\"hljs-number\">3s<\/span> <span class=\"hljs-number\">3<\/span>;\n}\n\n<span class=\"hljs-selector-class\">.typingEffect__line3<\/span> {\n  <span class=\"hljs-attribute\">animation<\/span>: typing <span class=\"hljs-number\">0s<\/span> <span class=\"hljs-number\">6s<\/span> forwards, blink <span class=\"hljs-number\">1s<\/span> <span class=\"hljs-number\">6s<\/span> infinite;\n}\n\n<span class=\"hljs-keyword\">@keyframes<\/span> typing {\n  <span class=\"hljs-selector-tag\">from<\/span> {\n    <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">0<\/span>;\n  }\n  <span class=\"hljs-selector-tag\">to<\/span> {\n    <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">100%<\/span>;\n  }\n}\n\n<span class=\"hljs-keyword\">@keyframes<\/span> blink {\n  0%, 45% {\n    <span class=\"hljs-attribute\">border-color<\/span>: transparent;\n  }\n  50%, 100% {\n    <span class=\"hljs-attribute\">border-color<\/span>: white;\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-7\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Here is the result:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_63879a1daca37.gif\" alt=\"\"\/><figcaption class=\"wp-element-caption\">The function <code>helloWorld()<\/code> function printed in an animated style.<\/figcaption><\/figure>\n\n\n\n<p>Finally, to add syntax highlighting, we will first add <code>vue3-highlightjs<\/code> to our app by modifying the <code>main.ts<\/code> file to the following:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-8\" data-shcb-language-name=\"TypeScript\" data-shcb-language-slug=\"typescript\"><span><code class=\"hljs language-typescript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> { createApp } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'vue'<\/span>\n<span class=\"hljs-keyword\">import<\/span> App <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/App.vue'<\/span>\n<span class=\"hljs-keyword\">import<\/span> vue3Highlightjs <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'vue3-highlightjs'<\/span>\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">'highlight.js\/styles\/dracula.css'<\/span>\n\ncreateApp(App).use(vue3Highlightjs).mount(<span class=\"hljs-string\">'#app'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-8\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">TypeScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">typescript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Then, we specify where we want to add highlighting using the <code>v-highlightjs<\/code> directive and the language to be highlighted. To do this, modify the template tag in <code>App.vue<\/code> to the following:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-9\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">template<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">v-highlightjs<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">code<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"javascript\"<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"typingEffect__line1\"<\/span>&gt;<\/span>\n          function helloWorld() {\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"typingEffect__line2\"<\/span>&gt;<\/span>\n          console.log('Hello world')\n        <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">br<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n        <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"typingEffect__line3\"<\/span>&gt;<\/span>}<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">code<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">template<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-9\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>With this, we are done building our coding typewriter effect using only CSS.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Creating the typewriter effect in Vue<\/h2>\n\n\n\n<p>The typewriter effect with Vue is achieved by appending one character at a time to a string which is then displayed in the interface. Then we simply add a blinking pipe character ( | ) or border that acts as a cursor that types out the text. Let\u2019s learn how it\u2019s done with a simple console.log example.<\/p>\n\n\n\n<p>First, let&#8217;s create the typing area and define the text to be used for the typewriter effect:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-10\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">setup<\/span>&gt;<\/span><span class=\"javascript\">\n<span class=\"hljs-keyword\">import<\/span> { ref } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'vue'<\/span>;\n<span class=\"hljs-keyword\">const<\/span> code = <span class=\"hljs-string\">\"console.log('Hello world')\"<\/span>\n<span class=\"hljs-keyword\">const<\/span> typeValue = ref(<span class=\"hljs-string\">''<\/span>)\n\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">template<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">code<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"typingEffect\"<\/span>&gt;<\/span>\n        {{ typeValue }}\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">code<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">template<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span> <span class=\"hljs-attr\">scoped<\/span>&gt;<\/span><span class=\"css\">\n<span class=\"hljs-selector-tag\">code<\/span> {\n  <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#202129<\/span>;\n  <span class=\"hljs-attribute\">border-radius<\/span>: <span class=\"hljs-number\">4px<\/span>;\n  <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">20px<\/span>;\n  <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">70px<\/span>;\n  <span class=\"hljs-attribute\">color<\/span>: white;\n  <span class=\"hljs-attribute\">display<\/span>: flex;\n  <span class=\"hljs-attribute\">margin<\/span>: <span class=\"hljs-number\">0<\/span> auto;\n  <span class=\"hljs-attribute\">align-items<\/span>: center;\n  <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">350px<\/span>;\n}\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-10\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Next, let&#8217;s create a function which will be called when our app loads that will append each character of the <code>code<\/code> variable to the <code>typeValue<\/code> state which has been bound to the UI.&nbsp;&nbsp;<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-11\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">setup<\/span>&gt;<\/span><span class=\"javascript\">\n<span class=\"hljs-comment\">\/\/ ...<\/span>\n\n<span class=\"hljs-keyword\">const<\/span> typeEffect = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n  <span class=\"hljs-keyword\">if<\/span> (typeValue.value.length &lt; code.length) {\n    typeValue.value += code.charAt(typeValue.value.length);\n    setTimeout(typeEffect, <span class=\"hljs-number\">50<\/span>);\n  } \n}\ntypeEffect()\n\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-11\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Above we have created a recursion function which will run until all characters of the <code>code<\/code> variable have been appended to the <code>typeValue<\/code> state. We use<code>setTimeout<\/code> to recursively call the <code>typeEffect<\/code> function after 50 milliseconds<strong> <\/strong>so that characters won\u2019t be appended instantly in order to create the full feel of a typewriter effect.&nbsp;<\/p>\n\n\n\n<p>Here is the result:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/d2h1bfu6zrdxog.cloudfront.net\/wp-content\/uploads\/2022\/11\/img_63879a21ae66b.gif\" alt=\"\"\/><figcaption class=\"wp-element-caption\"><strong>console.log(<\/strong>&#8216;Hello world&#8217;) printed with a typewriter style.<\/figcaption><\/figure>\n\n\n\n<p>Looking good so far. Finally, let\u2019s add the blinking cursor using the following styles:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-12\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.typingEffect<\/span> {\n  <span class=\"hljs-attribute\">padding-right<\/span>: <span class=\"hljs-number\">5px<\/span>;\n  <span class=\"hljs-attribute\">border-right<\/span>: <span class=\"hljs-number\">2px<\/span> solid white;\n  <span class=\"hljs-attribute\">white-space<\/span>: nowrap;\n  <span class=\"hljs-attribute\">animation<\/span>: blink <span class=\"hljs-number\">1s<\/span> linear infinite;\n}\n\n<span class=\"hljs-keyword\">@keyframes<\/span> blink {\n  0%, 45% {\n    <span class=\"hljs-attribute\">border-color<\/span>: transparent;\n  }\n  50%, 100% {\n    <span class=\"hljs-attribute\">border-color<\/span>: white;\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-12\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Now, using what we have learned let\u2019s create the \u201ccoding\u201d typewriter effect. First, let&#8217;s create the typing area and define the text to be used for the typewriter effect:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-13\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">script<\/span> <span class=\"hljs-attr\">setup<\/span>&gt;<\/span><span class=\"javascript\">\n<span class=\"hljs-keyword\">import<\/span> { ref } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'vue'<\/span>;\n<span class=\"hljs-keyword\">const<\/span> codeLine1 = <span class=\"hljs-string\">\"function helloWorld() =&gt; {\"<\/span>\n<span class=\"hljs-keyword\">const<\/span> codeLine2 = <span class=\"hljs-string\">\"console.log('Hello world')\"<\/span>\n<span class=\"hljs-keyword\">const<\/span> codeLine3 = <span class=\"hljs-string\">\"}\"<\/span>\n\n<span class=\"hljs-keyword\">const<\/span> typeLine1 = ref(<span class=\"hljs-string\">''<\/span>)\n<span class=\"hljs-keyword\">const<\/span> typeLine2 = ref({<span class=\"hljs-attr\">text<\/span>: <span class=\"hljs-string\">''<\/span>, <span class=\"hljs-attr\">state<\/span>: <span class=\"hljs-literal\">false<\/span>})\n<span class=\"hljs-keyword\">const<\/span> typeLine3 = ref({<span class=\"hljs-attr\">text<\/span>: <span class=\"hljs-string\">''<\/span>, <span class=\"hljs-attr\">state<\/span>: <span class=\"hljs-literal\">false<\/span>})\n\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">script<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">template<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">code<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"typingEffect\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"typingEffect__line1\"<\/span>&gt;<\/span>\n        {{typeLine1}}\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> &gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"typingEffect__line2\"<\/span>&gt;<\/span>\n        {{typeLine2.text}}\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> &gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">p<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"typingEffect__line3\"<\/span>&gt;<\/span>\n        {{typeLine3.text}}\n      <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">p<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">code<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">template<\/span>&gt;<\/span>\n\n<span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">style<\/span> <span class=\"hljs-attr\">scoped<\/span>&gt;<\/span><span class=\"css\">\n<span class=\"hljs-selector-class\">.typingEffect<\/span> {\n  <span class=\"hljs-attribute\">color<\/span>: white;\n  <span class=\"hljs-attribute\">display<\/span>: flex;\n  <span class=\"hljs-attribute\">flex-direction<\/span>: column;\n  <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">20px<\/span>;\n  <span class=\"hljs-attribute\">background-color<\/span>: <span class=\"hljs-number\">#202129<\/span>;\n  <span class=\"hljs-attribute\">margin<\/span>: <span class=\"hljs-number\">0<\/span> auto;\n  <span class=\"hljs-attribute\">width<\/span>: <span class=\"hljs-number\">350px<\/span>;\n  <span class=\"hljs-attribute\">height<\/span>: <span class=\"hljs-number\">120px<\/span>;\n}\n\n<span class=\"hljs-selector-class\">.typingEffect__line2<\/span> {\n  <span class=\"hljs-attribute\">margin-left<\/span>: <span class=\"hljs-number\">24px<\/span>;\n}\n<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">style<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-13\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Above we have declared <code>typeLine2<\/code> and <code>typeLine3<\/code> as objects in order to add a <code>state<\/code> property which we will use later on to control the typing animation.<\/p>\n\n\n\n<p>Next, let\u2019s create the functions that create the typing animation by appending each character of <code>codeLine1<\/code>, <code>codeLine2<\/code>, and<code> codeLine3<\/code> variables to their respective state.<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-14\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">const<\/span> typeEffect3 = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n  typeLine3.value.text += codeLine3.charAt(typeLine3.value.text.length);\n}\n\n<span class=\"hljs-keyword\">const<\/span> typeEffect2 = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n  <span class=\"hljs-keyword\">if<\/span> (typeLine2.value.text.length &lt; codeLine2.length) {\n    typeLine2.value.text += codeLine2.charAt(typeLine2.value.text.length);\n    setTimeout(typeEffect2, <span class=\"hljs-number\">60<\/span>);\n  }\n  <span class=\"hljs-keyword\">if<\/span>(typeLine2.value.text.length === codeLine2.length &amp;&amp; !typeLine3.value.state) {\n    setTimeout(typeEffect3, <span class=\"hljs-number\">500<\/span>)\n    typeLine3.value.state = <span class=\"hljs-literal\">true<\/span>\n  }\n}\n\n<span class=\"hljs-keyword\">const<\/span> typeEffect1 = <span class=\"hljs-function\"><span class=\"hljs-params\">()<\/span> =&gt;<\/span> {\n  <span class=\"hljs-keyword\">if<\/span> (typeLine1.value.length &lt; codeLine1.length) {\n    typeLine1.value += codeLine1.charAt(typeLine1.value.length);\n    setTimeout(typeEffect1, <span class=\"hljs-number\">60<\/span>)\n  }\n  <span class=\"hljs-keyword\">if<\/span>(typeLine1.value.length === codeLine1.length &amp;&amp; !typeLine2.value.state) {\n    setTimeout(typeEffect2, <span class=\"hljs-number\">800<\/span>)\n    typeLine2.value.state = <span class=\"hljs-literal\">true<\/span>\n  }\n}\ntypeEffect1()<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-14\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Above we have created three functions <code>typeEffect1<\/code>, <code>typeEffect2<\/code>, and <code>typeEffect3<\/code>, each to handle a line of code for the typewriter effect. When the app loads, <code>typeEffect1<\/code> \u2013 which is responsible for creating the typing animation for the first line of code \u2013 will run. After all characters of the first line of code are displayed, the next function (<code>typeEffect2<\/code>) is called which then calls the next.<\/p>\n\n\n\n<p>Notice we are using <code>setTimeout<\/code> to call the next functions after a few milliseconds of delays. These delays are set to make the typewriter effect look more like how code is actually written.&nbsp;<\/p>\n\n\n\n<p>Next, let\u2019s add the blinking cursor using the following styles:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-15\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.typingEffect<\/span> <span class=\"hljs-selector-tag\">p<\/span> {\n  <span class=\"hljs-attribute\">width<\/span>: fit-content;\n  <span class=\"hljs-attribute\">height<\/span>: fit-content;\n  <span class=\"hljs-attribute\">border-right<\/span>: <span class=\"hljs-number\">2px<\/span> solid transparent;\n}\n\n<span class=\"hljs-selector-class\">.typingEffect__line1<\/span> {\n  <span class=\"hljs-attribute\">animation<\/span>: blink <span class=\"hljs-number\">1s<\/span> <span class=\"hljs-number\">2<\/span>;\n}\n\n<span class=\"hljs-selector-class\">.typingEffect__line2<\/span> {\n  <span class=\"hljs-attribute\">animation<\/span>: blink <span class=\"hljs-number\">1s<\/span> <span class=\"hljs-number\">2s<\/span> <span class=\"hljs-number\">2<\/span>;\n}\n\n<span class=\"hljs-selector-class\">.typingEffect__line3<\/span> {\n  <span class=\"hljs-attribute\">animation<\/span>: blink <span class=\"hljs-number\">1s<\/span> <span class=\"hljs-number\">4s<\/span> infinite;\n}\n\n<span class=\"hljs-keyword\">@keyframes<\/span> blink {\n  0%, 45% {\n    <span class=\"hljs-attribute\">border-color<\/span>: transparent;\n  }\n  50%, 100% {\n    <span class=\"hljs-attribute\">border-color<\/span>: white;\n  }\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-15\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Finally, to add syntax highlighting. First, we will add <code>vue3-highlightjs<\/code> to our app by modifying the <code>main.ts<\/code> file to the following:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-16\" data-shcb-language-name=\"JavaScript\" data-shcb-language-slug=\"javascript\"><span><code class=\"hljs language-javascript shcb-wrap-lines\"><span class=\"hljs-keyword\">import<\/span> { createApp } <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'vue'<\/span>\n<span class=\"hljs-keyword\">import<\/span> App <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'.\/App.vue'<\/span>\n<span class=\"hljs-keyword\">import<\/span> vue3Highlightjs <span class=\"hljs-keyword\">from<\/span> <span class=\"hljs-string\">'vue3-highlightjs'<\/span>\n<span class=\"hljs-keyword\">import<\/span> <span class=\"hljs-string\">'highlight.js\/styles\/dracula.css'<\/span>\n\n\ncreateApp(App).use(vue3Highlightjs).mount(<span class=\"hljs-string\">'#app'<\/span>)<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-16\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">JavaScript<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">javascript<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>Then, we specify where we want to add highlighting using the <code>v-highlightjs<\/code> directive and the language to be highlighted. To do this, modify the template tag in <code>App.vue<\/code> to the following:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-17\" data-shcb-language-name=\"HTML, XML\" data-shcb-language-slug=\"xml\"><span><code class=\"hljs language-xml shcb-wrap-lines\"><span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">template<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">code<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"typingEffect\"<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">v-highlightjs<\/span>=<span class=\"hljs-string\">\"typeLine1\"<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">code<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"typingEffect__line1 javascript\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">code<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">v-highlightjs<\/span>=<span class=\"hljs-string\">\"typeLine2.text\"<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">code<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"typingEffect__line2 javascript\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">code<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">div<\/span> <span class=\"hljs-attr\">v-highlightjs<\/span>=<span class=\"hljs-string\">\"typeLine3.text\"<\/span>&gt;<\/span>\n      <span class=\"hljs-tag\">&lt;<span class=\"hljs-name\">code<\/span> <span class=\"hljs-attr\">class<\/span>=<span class=\"hljs-string\">\"typingEffect__line3 javascript\"<\/span>&gt;<\/span><span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">code<\/span>&gt;<\/span>\n    <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">div<\/span>&gt;<\/span>\n  <span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">code<\/span>&gt;<\/span>\n<span class=\"hljs-tag\">&lt;\/<span class=\"hljs-name\">template<\/span>&gt;<\/span><\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-17\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">HTML, XML<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">xml<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>The syntax highlighting has been added but we will notice that the UI is a bit messy and the blinking cursor is gone. To fix that, add the following styles:<\/p>\n\n\n<pre class=\"wp-block-code\" aria-describedby=\"shcb-language-18\" data-shcb-language-name=\"CSS\" data-shcb-language-slug=\"css\"><span><code class=\"hljs language-css shcb-wrap-lines\"><span class=\"hljs-selector-class\">.typingEffect<\/span> <span class=\"hljs-selector-tag\">code<\/span> {\n  <span class=\"hljs-attribute\">padding<\/span>: <span class=\"hljs-number\">0<\/span>;\n  <span class=\"hljs-attribute\">background-color<\/span>: inherit;\n}\n\n<span class=\"hljs-selector-class\">.typingEffect<\/span> <span class=\"hljs-selector-tag\">code<\/span> {\n  <span class=\"hljs-attribute\">width<\/span>: fit-content;\n  <span class=\"hljs-attribute\">height<\/span>: fit-content;\n  <span class=\"hljs-attribute\">border-right<\/span>: <span class=\"hljs-number\">2px<\/span> solid transparent;\n}<\/code><\/span><small class=\"shcb-language\" id=\"shcb-language-18\"><span class=\"shcb-language__label\">Code language:<\/span> <span class=\"shcb-language__name\">CSS<\/span> <span class=\"shcb-language__paren\">(<\/span><span class=\"shcb-language__slug\">css<\/span><span class=\"shcb-language__paren\">)<\/span><\/small><\/pre>\n\n\n<p>With this, we are done creating the typewriter effect. Try it out in the sandbox:<\/p>\n\n\n<div\n\tclass=\"sandbox-embed responsive-embed  sandbox-embed--full-width\"\n\tstyle=\"padding-top: 125%\"\ndata-block-name=\"coderpad-sandbox-embed\">\n\t<iframe src=\"https:\/\/embed.coderpad.io\/sandbox?question_id=236236&#038;use_question_button\" width=\"640\" height=\"800\" loading=\"lazy\" aria-label=\"Try out the CoderPad sandbox\"><\/iframe>\n<\/div>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>In this tutorial, we have learned how to create a typewriter effect using only CSS, and JavaScript. If you\u2019re interested in learning more about creating a great UI with CSS and JavaScript frameworks, check out these blog posts:<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li><a href=\"https:\/\/coderpad.io\/blog\/development\/interactive-animated-backgrounds-react-tsparticles\/\">How to Create Interactive Animated Backgrounds in React with tsParticles<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/coderpad.io\/blog\/development\/how-to-animate-a-react-application-with-framer-motion\/\">How to Animate A React Application With Framer Motion<\/a><\/li>\n\n\n\n<li><a href=\"https:\/\/coderpad.io\/blog\/development\/how-to-build-a-draggable-slider-with-javascript\/\">How To Build A Draggable Slider With JavaScript<\/a><\/li>\n<\/ul>\n\n\n\n<p><em>Taminoturoko Briggs is an enthusiastic software developer and technical writer. Core languages include JavaScript and Python.<\/em><\/p>\n","protected":false},"excerpt":{"rendered":"<p>The typewriter effect is an animation that gives the impression that text is being typed on the screen one letter at a time, which can be a great way to grab a user&#8217;s attention. Learn how you can grab your user&#8217;s attention by implementing the typewriter effect in your Vue applications in this article.<\/p>\n","protected":false},"author":1,"featured_media":25670,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[9],"tags":[],"persona":[29],"blog-programming-language":[64],"keyword-cluster":[],"class_list":["post-25339","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-development"],"acf":[],"_links":{"self":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/25339","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/comments?post=25339"}],"version-history":[{"count":126,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/25339\/revisions"}],"predecessor-version":[{"id":32609,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/posts\/25339\/revisions\/32609"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media\/25670"}],"wp:attachment":[{"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/media?parent=25339"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/categories?post=25339"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/tags?post=25339"},{"taxonomy":"persona","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/persona?post=25339"},{"taxonomy":"blog-programming-language","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/blog-programming-language?post=25339"},{"taxonomy":"keyword-cluster","embeddable":true,"href":"https:\/\/coderpad.io\/wp-json\/wp\/v2\/keyword-cluster?post=25339"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}