← Back toCSS animation

Blinking cursor

Written byPhuoc Nguyen
Created
03 Sep, 2023
The blinking cursor animation is a neat trick that makes the text cursor appear and disappear at regular intervals.
You've probably seen this effect in text editors, where it shows where new text will appear when you start typing. But that's not the only place it's used! You'll also find it in search bars, where it lets you know you can start typing your question. And in login forms, it tells you which field is ready for your input.
In this post, we'll explore some simple and common ways to create a blinking cursor animation using CSS.

Markup

To add a caret to your webpage, start by creating an HTML element where you want the caret to appear. For instance, you could make a div element with a class of `caret`.
html
<div class="caret"></div>
The caret should have some basic styles that determine its dimensions, like height and width. Additionally, it should have a dark background color to resemble a real caret.
css
.cursor {
background-color: rgb(15 23 42);
width: 0.25rem;
height: 2rem;
}

Blinking animation

There are two common ways to make the caret blink. The first approach is to change the background color repeatedly, back and forth between the original and transparent colors.
It involves creating a blink keyframe to define the animation. This approach keeps the original color in the middle of the animation, but uses a transparent background color at the start and end, making the caret appear invisible.
css
@keyframes blink {
0%, 100% {
background-color: transparent;
}
50% {
background-color: rgb(15 23 42);
}
}
The animation lasts one second and repeats infinitely.
css
.cursor {
animation: blink 1s infinite;
}
Now, let's see an example of how this approach works in action.
Another approach to create a blinking effect is to change the `opacity` property instead of the background color. This animation starts at 100% opacity (fully visible) and ends at 0% opacity (fully invisible), then repeats.
By repeatedly changing the opacity, you can achieve the blinking effect you're looking for.
Let's take a look at the example below to see how this approach works.

See also

If you found this post helpful, please consider giving the repository a star on GitHub or sharing the post on your favorite social networks 😍. Your support would mean a lot to me!

Questions? 🙋

Do you have any questions about front-end development? If so, feel free to create a new issue on GitHub using the button below. I'm happy to help with any topic you'd like to learn more about, even beyond what's covered in this post.
While I have a long list of upcoming topics, I'm always eager to prioritize your questions and ideas for future content. Let's learn and grow together! Sharing knowledge is the best way to elevate ourselves 🥷.
Ask me questions

Recent posts ⚡

Newsletter 🔔

If you're into front-end technologies and you want to see more of the content I'm creating, then you might want to consider subscribing to my newsletter.
By subscribing, you'll be the first to know about new articles, products, and exclusive promotions.
Don't worry, I won't spam you. And if you ever change your mind, you can unsubscribe at any time.
Phước Nguyễn