text animation css and js

Text Animation in CSS and JS

If you want to make your website more engaging and interactive, adding text animation can be a great way to attract users. In this post, we will go over some of the ways you can animate text using CSS and JS.

CSS Animations

CSS animations are a great way to add simple, yet effective animations to your website. Here is an example of how you can animate text in CSS:


  .animate {
    animation-name: fade-in;
    animation-duration: 2s;
    animation-fill-mode: forwards;
  }
  
  @keyframes fade-in {
    from { opacity: 0; }
    to { opacity: 1; }
  }

In the above code, we have created a class called "animate" and applied it to our text element. We have also defined an animation called "fade-in" that will gradually increase the opacity of our text from 0 to 1 over a duration of 2 seconds. Finally, we have set the fill mode to "forwards" so that the text stays visible after the animation completes.

JS Animations

If you want more complex animations or want to control the animation with user interactions, you can use JS. Here is an example of how you can animate text using JS:


  const text = document.querySelector('.text');
  
  text.addEventListener('mouseover', () => {
    text.classList.add('animate');
  });
  
  text.addEventListener('mouseleave', () => {
    text.classList.remove('animate');
  });

In the above code, we have selected our text element using the querySelector method and added event listeners for mouseover and mouseleave. When the mouse is over the text, we add the "animate" class which will trigger our CSS animation. When the mouse leaves the text, we remove the "animate" class to stop the animation.

Using Libraries

If you don't want to create your own animations from scratch, there are plenty of libraries available that provide pre-made animations. Some popular libraries include:

These libraries provide a wide range of animations that you can use on your website with minimal effort. Simply include the library in your HTML file and follow their documentation to apply the animations to your text elements.

In conclusion, there are many ways to animate text using CSS and JS. Whether you want a simple fade-in effect or more complex user-controlled animations, you can achieve it with these techniques. Additionally, using libraries can save you time and effort while still providing a great user experience.

Subscribe to The Poor Coder | Algorithm Solutions

Don’t miss out on the latest issues. Sign up now to get access to the library of members-only issues.
[email protected]
Subscribe