How to Animate a “Twisting Text Effect” With CSS and JavaScript

In this tutorial, we’ll learn how to split an element’s text into separate characters, which we’ll then animate to give us a twisting effect.

What We’ll be Building

Without further intro, let’s check out what we’ll be building:

In our case, there will be two types of animations:

  • The first animation will happen each time a heading comes into view.
  • The second animation will happen each time the user hovers over a link.

1. Begin With the HTML Markup

We’ll define three sections. Each section will have a heading, a paragraph, and a link.

We’ll add the data-split attribute to the elements that will be animated. An additional data-split-type attribute will determine the animation type. Possible values are hover and scroll.

Here’s the required markup:

2. Split Text

The splitCharacters() function will be responsible for splitting the text of all elements with the data-split attribute and wrapping each of their characters into a span element.

So, assuming that initially, we have this element:

After running the function, the aforementioned element’s markup will turn into this:

The end result will appear the same as before running the function.

Here are a few things to note:

  • Inside the target element we’ll define the .inner element which will contain the .front and .back elements. We add this wrapper span to isolate the styles of the target element (e.g. link) and avoid any inconsistencies (e.g. if we add paddings to the link).
  • Both the .front and .back elements will contain the element’s initial text wrapped into span elements. 
  • By default, the contents of the .front element will be visible. Depending on the animation type, the contents of the .back one will appear when either we hover over the target element or scroll till it comes into view. 
  • As we’ll see in a bit, we’ll use the transition-delay property to sequentially animate the characters. To create different delays between them, we’ll use the index CSS variable. Each character, apart from the empty one (whitespace), will receive as a value of this variable an incremented number which will denote the character’s position/index inside their parent.

With all these in mind, here’s the complete declaration of the splitCharacters() function:

3. Add Styles

For simplicity, we’ll only focus our attention on the main styles. Besides, you can check them all by clicking on the CSS tab of the demo.

Here are the noteworthy things:

  • The .back element will be an absolute element. 
  • By default, there will be a 0.015s difference between the animation of each character. That said, the first character of the .front and .back elements will have a transition delay of 0.015s, the second one 0.03s, the third one 0.045s, and so on. The whitespace won’t have any delay. 
  • The characters inside the .back element will be hidden by default and sit underneath the text like this: 
The .back element as it appears initiallyThe .back element as it appears initiallyThe .back element as it appears initially

The associated styles:

Hover Animation

As we hover over an element with the [data-split-type="hover"] attribute, the characters of the .back element will appear while the ones of the .front element will become hidden by moving upwards like this:

The .front element when the animation happensThe .front element when the animation happensThe .front element when the animation happens

Here are the corresponding styles:

As we scroll the page, all elements with the [data-split-type="scroll"] attribute will be animated as soon as they become visible in the viewport. In our example, only the headings will have this behavior.

To accomplish this task, we’ll borrow some code from this tutorial that uses the Intersection Observer API.

So, when at least 50% of each heading comes into view, it’ll receive the is-animated class. Otherwise, it’ll lose this class, and the animation will return to its initial state.

Below we declare the function responsible for this stuff:

And here are the styles that kick in during this condition:

Conclusion

That’s all for today, folks! During this exercise, we covered a way to split an element’s text into individual characters and animate them on scroll and hover. I hope you gained some new knowledge that you’ll use to enhance this demo or create similar text effects in your projects. If so, don’t forget to give our demo some love :)

Let’s look at our creation once again:

If you need a more complete and robust solution to animate words, characters, lines, etc. you can try a JavaScript library like Splitting.js or GSAP’s SplitText.js (although it isn’t free).

As always, thanks a lot for reading!