Rotating texts
Written byPhuoc Nguyen
Created
26 Aug, 2023
A rotating text effect is a cool animated effect that cycles through a series of different text messages. It's a fantastic way to add some visual interest to your website and draw attention to important content.
In this post, I'll show you how to create a rotating text animation using CSS. It's easier than you might think, so let's get started!
#Markup
First things first, let's set up the basic markup. The layout consists of three layers:
- The outermost element, which wraps all the other elements. It has only one child node, which has the CSS class
`container__inner`
. - The middle layer, which contains the
`container__inner`
element. - The innermost layer, which contains individual elements for each text you want to rotate through.
<div class="container">
<div class="container__inner">
<div class="container__text">iPhone</div>
<div class="container__text">iPad</div>
<div class="container__text">Chrome</div>
<div class="container__text">Firefox</div>
<div class="container__text">Safari</div>
</div>
</div>
The container and text elements should be the same height. Plus, we need to add the
`overflow: hidden`
style to keep our text from overflowing outside the container..container {
overflow: hidden;
height: 4rem;
}
.container__text {
height: 4rem;
}
#Rotating effect
To animate text elements one by one, we can create keyframes called
`slide`
. In each phase, the element will move up to the same height as the current text. Since each text has a height of `4rem`
and there are 5 text elements in total, the animation could move the target element up by a multiple of `4rem`
which is `4rem`
, `8rem`
, `12rem`
, and `16rem`
, respectively.At the end of the animation, the target will slide back down to its original position, revealing the first text element as the beginning phrase.
@keyframes slide {
0%, 10% {
transform: translateY(0);
}
20%, 30% {
transform: translateY(-4rem);
}
40%, 50% {
transform: translateY(-8rem);
}
60%, 70% {
transform: translateY(-12rem);
}
80%, 90% {
transform: translateY(-16rem);
}
100% {
transform: translateY(0);
}
}
Finally, the inner element will run the animation for 10 seconds using the linear timing function, and with the
`infinite`
value set, the animation will loop indefinitely..container__inner {
animation: slide 10s linear infinite;
}
Check out the rotating text animation in action!
To add more text, just include additional
`div`
elements with the class `container__text`
and adjust the position in the `slide`
keyframes accordingly. It's that simple!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 ⚡
Copy the content of an element to your clipboard
01 Oct, 2023
Make a text area fit its content automatically
30 Sep, 2023
Quickly insert alternate characters while typing
30 Sep, 2023
Zebra-like background
30 Sep, 2023
Add autocomplete to your text area
28 Sep, 2023
Linear scale of a number between two ranges
28 Sep, 2023
Highlight the current line in a text area
27 Sep, 2023
Create your own custom cursor in a text area
27 Sep, 2023
Mirror a text area for improving user experience
26 Sep, 2023
Display the line numbers in a text area
24 Sep, 2023
Select a given line in a text area
24 Sep, 2023
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