← Back toCSS Animation

Blinking cursor

Written byPhuoc Nguyen
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.


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`.
<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.
.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.
@keyframes blink {
0%, 100% {
background-color: transparent;
50% {
background-color: rgb(15 23 42);
The animation lasts one second and repeats infinitely.
.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

Questions? 🙋

Do you have any questions? Not just about this specific post, but about any topic in front-end development that you'd like to learn more about? If so, feel free to send me a message on Twitter or send me an email. You can find them at the bottom of this page.
I have a long list of upcoming posts, but your questions or ideas for the next one will be my top priority. Let's learn together! Sharing knowledge is the best way to grow 🥷.

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