How to create typing effects using CSS
You don't need JavaScript to create the classic typewriter effect. CSS's step() function will help you create typography effects easily.
What is the typewriter effect?
The typewriter effect is an animation technique for text that simulates the process of content being displayed gradually, mimicking the act of typing as it unfolds before the viewer's eyes. The effect is reminiscent of old-fashioned typewriters, early computer terminals, or Command Line Interface (CLI).
The gradual emergence of text adds an element of suspense and intrigue, encouraging the audience to pay attention to the message being conveyed.
How the steps() function works in CSS
The step() function is a common function used in CSS animations. It makes the animation look like it's progressing in separate steps instead of a smooth transition.
step() is a function that determines the animation time with two parameters. The first parameter represents the number of steps you want your animation to take. The second parameter determines the behavior of each step. The syntax of the step() function is as follows:
animation-timing-function: steps(n, direction)
In the code block above, the step() function has two parameters: n and direction . The direction parameter can be start or end .
Setting direction to set ensures that the first step is completed as soon as the animation begins. Meanwhile, setting direction to end will run the last step when the animation ends. To illustrate the importance of the step() function , consider the following HTML code:
The code block above defines a container div with child divs. If you want the child div to glide across the screen, you use a CSS animation like this:
.container { background-color: blue; } div:not(.container) { background-color: red; width: 88px; height: 88px; animation: movebox 4s infinite; } @keyframes movebox { 100% { transform: translateX(100vw); } }
The code block above uses the @keyframes rule to determine the movebox named animation. This animation is then applied to the child div, causing it to move smoothly across the screen in an endless loop.
If you don't like smooth animations and want to achieve a "jumpy" effect, you can use the step() function like this:
div:not(.container){ background-color: red; width: 88px; height: 88px; animation: movebox 4s infinite; animation-timing-function: steps(10, end); }
As you can see in the GIF below, combining the step() function with the parameter value 10 will animate the child div in steps instead of a smooth animation. The higher the step count, the less choppy your animation will be.
In the above example, direction parameter is provided. However, if you omit direction, the browser will use end as the default value for direction .
Create a typewriter effect
Now that you understand how the step() function works , it's time to put everything you've learned into practice. Create a new folder and give it an appropriate name. In that folder, add the index.htm file for markup and the style.css file for styling.
In the index.htm file , add the following boilerplate code:
Document Lorem ipsum dolor sit amet consectetur adipisicing elit. Reiciendis, tempore!
The code block above defines the markup for a simple HTML web page. There is a container div containing another div with some dummy text.
Create effects for text
Open the style.css file and set the width of the container div to the width of the content inside it.
.container{ width: fit-content; }
Next, using the @keyframes rule , define the animation that controls how it progresses over time. Set width to "0%" at 0% . At 100% , set the width to "100%" like this:
@keyframes type-text { 0% { width: 0%; } 100% { width: 100%; } }
Next, select the element named class text and set its overflow property to hidden . Doing so will ensure that the text remains hidden as long as the typing effect has not started. Having done that, make sure the text stays on one line by setting the whitespace property to nowrap . Give the text class a monospace font and add a green vertical border to the right of the text.
This border will create the appearance of a cursor. Set the appropriate font size and animation properties to the text type. Finally, add the step() function to the animation-timing-function .
.text { overflow: hidden; white-space: nowrap; font-family: "Courier New", Courier, monospace; border-right: solid 10px green; font-size: 23px; animation: type-text forwards 4s; animation-timing-function: steps(40); }
When you run the code in the browser, you will see:
If you want a longer typing effect, you can adjust the animation duration and number of steps specified in the step() function.
Make the cursor animate
The typewriter effect is almost complete, but one feature is still missing: the flashing cursor. Recall that in the last block of code, the right border was placed on the text to be used as a cursor. You can also add animation to this cursor using the @keyframes rule .
@keyframes cursor-blink { 0% { border-color: transparent; } 100% { border-color: green; } }
After defining the custom animation, add the animation name to the animation properties on the text layer and set the duration to 0.6 seconds.
.text{ /* Other style rules*/ animation: type-text forwards 4s, cursor-blink .6s infinite; }
Now when you run the code, you will see:
It's done! Wishing you success!
You should read it
- Set of multiple choice questions for programming with P15 prize
- Set of multiple choice questions about programming with P10 prize
- Set of multiple choice questions about programming with P7 prize
- Set of multiple-choice questions on award-winning programming P5
- Set of multiple choice questions about programming with P6
- P13 programming set of multiple choice questions
- Set of multiple choice questions on programming with P3 prize
- Set of multiple choice questions on programming with P16 prize
May be interested
- Practice typing 10 fingers, typing fast with Mario Teaches Typing gamepractice typing 10 fingers or practicing typing fast with long text pages, meaningless words are boring. with mario teaches typing your quick typing practice will be much more interesting. let's find out how this game will help improve typing speed.
- How to create interesting Cyberpunk photo effects in Photoshopthe overall background of cyberpunk photos is often quite dark. striking on the background are street corners lit by bright neon lights, busy crowds of people crowded. all combine harmoniously to create a scene full of magic and fanciful.
- To improve typing speed, visit these 5 websitesimproving typing skills is one of the skills that any user wants, especially office workers. if you want to know what your typing speed is currently and how to improve your typing speed, please refer to the following article of network administrator.
- Instructions for creating Gradient effects in Wordgradient effects are used a lot in design, such as using the gradient effect in photoshop to adjust the color of an image, or using text effects in word to create beautiful effects for documents.
- Practice typing 10 fingers to speed up typingsee the following 10-finger typing exercise, trying to grasp this 10-finger typing technique to ensure you will significantly improve your typing speed.
- Do you know how to create the Like button effect on Facebook?new facebook launches like button effect to hover over the interface, when you can write keywords in english and vietnamese to appear effects.
- Tutorial for creating 3D photo effects in Photoshop (Part 1)did you know that in photoshop there are 3d photo editing effects that make your photos more vivid. the following article the network administrator will show you how to create 3d effects in photoshop.
- Create light effects and shadows in Photoshopin addition to the text effects available in photoshop software, you can also create a different style for yourself. here tipsmake.com will guide and create a picture with text applied light effects and shadows.
- RapidTyping - Download RapidTyping hererapidtyping is a convenient and easy-to-use typing practice tool that will help you improve your typing speed and reduce typos. with lessons organized for different levels, rapidtyping will teach you how to type or enhance existing skills.
- How to check typing speed, typing 10 fingersdo you wonder if your typing speed, 10-finger typing is up to the standard? or you are confident your ability to hit your computer at a professional level. the typing speed test websites will tell you the answer.