DEV Community

Designyff
Designyff

Posted on • Originally published at designyff.com

Loader - Text loading

Tutorial on how to create a loader inside of a text - CSS only.

Loader - Text loading

HTML

For HTML we need two "loading" texts inside of a container with class "loader".
One will represent the text and the other will animate.

<div class="loader">
    <span>LOADING</span>
    <span>LOADING</span>
</div>
Enter fullscreen mode Exit fullscreen mode

CSS

For CSS, first we'll style the container. We'll just set position to relative so that we can position properly the elements inside.

.loader {
    position: relative;
}
Enter fullscreen mode Exit fullscreen mode

Now we'll style "loading" texts inside of a container.
We'll set position to absolute and transform translate X and Y to -50%, to position elements in the center.
We'll increase the font and set it's size to 50 pixels and weight to 600.

.loader span {
    position: absolute;
    transform: translate(-50%, -50%);
    font-size: 50px;
    font-weight: 600;
}
Enter fullscreen mode Exit fullscreen mode

Now we'll style only the first text element (the one that represents the text).
We'll set it's color to transparent, and display only the borders of the text by setting text-stroke property.

.loader span:nth-child(1) {
    color: transparent;
    -webkit-text-stroke: 1px rgb(27, 27, 27);
}
Enter fullscreen mode Exit fullscreen mode

Now we'll style the second text element (the one that animates).
We'll only set it's color to yellow.

.loader span:nth-child(2) {
    color: rgb(181, 255, 22);
}
Enter fullscreen mode Exit fullscreen mode

Now we'll create the animation for the second text.
Using clip-path, we'll create two different shapes. The one we'll set at the beginning and at the end od the animation, and the other at 50%.

@keyframes waves {
    0%, 100% {
        clip-path: polygon(0% 45%, 15% 44%, 32% 50%, 54% 60%, 70% 61%, 84% 59%, 100% 52%, 100% 100%, 0% 100%);
    }
    50% {
        clip-path: polygon(0% 60%, 16% 65%, 34% 66%, 51% 62%, 67% 50%, 84% 45%, 100% 46%, 100% 100%, 0% 100%);
    }
}
Enter fullscreen mode Exit fullscreen mode

Now we'll add our animation to the second text element and set it's duration to 2 seconds with infinite iteration count.

.loader span:nth-child(2) {
    color: rgb(181, 255, 22);
    animation: waves 3s infinite;  /* Added animation */
}
Enter fullscreen mode Exit fullscreen mode

And that's it.

Video tutorial:
You can find the whole code with video tutorial here.

Thank you for reading ❤️

Top comments (0)