How to Create Animation using styled-components

mohammadfaisal

Mohammad Faisal

Posted on June 7, 2023

How to Create Animation using styled-components

To read more articles like this, visit my blog

Styled Components is a popular CSS framework for single-page applications. Today we will see how we can create animations using styled-components.

To learn more about styled-components go here.

https://styled-components.com/

Now let’s see how we can animate our components using styled-components.

Get started

First, create a boilerplate application with ReactJS.

npx create-react-app styled-components-animation-demo
Enter fullscreen mode Exit fullscreen mode

Then install the dependency

npm i styled-components
Enter fullscreen mode Exit fullscreen mode

And we are ready to roll

Define the Animation

Let’s create a FadeInAnimation with styled-components.

const FadeInAnimation = keyframes`
  0% {opacity:0;}
  100% {opacity:1;}
`;
Enter fullscreen mode Exit fullscreen mode

This is a straightforward animation that brings a component into view smoothly.

Use the animation

Let’s say we have a header to animate to bring into view. We can easily do that by defining the animation property.

The final result will look something like this.

const StyledHeader = styled.div`
  animation: ${FadeInAnimation} 300ms linear;
`;
Enter fullscreen mode Exit fullscreen mode

Final Words

This was a simple demonstration of how to animate components using styled-components .

Teaching animation was not the goal of this article. Just to clarify, any CSS animation will work similarly using this approach.

Get in touch with me via LinkedIn or my Personal Website.

💖 💪 🙅 🚩
mohammadfaisal
Mohammad Faisal

Posted on June 7, 2023

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related