ūü•≥
WindFrame is here: Design, prototype and code
sale icon End of Summer Sale ūüéĀ Get Contrast PRO UI library + admin template with 5 admin dahsboards, 23 pages and 10000+ components at 60% off ūüéČ
postImage

How to create Tailwind CSS Animation

blogImage

By Emmanuel Chinonso

Technical Writer

Web Animations make your websites come alive, when done properly they capture users attention and make for an immaculate user experience.

Web Animations have become the norm recently as developers are constantly making use of animations on their projects to make their applications interactive.

This has made many companies and websites adopt this technology on their website and applications.

When you click a button or open a website, and there are movements on the site, you look at an amination.

In the present day, anything from a cartoon to Japanese anime is considered animation. With simple CSS and JavaScript, you can create amazing animations.

As evident as the pros to incorporating animations in your projects are, animations can prove to be really difficult to build out from scratch.

However, with Tailwind CSS we do not need to build from scratch as we get access to the styling we want out of the box by using the Tailwind CSS classes.

In this tailwind CSS tutorial, we are going to look at

  • What is Animation
  • How to create Tailwind CSS Animation.
  • How to use customized animation in Tailwind CSS.

Table of Content

  • What is Animation
  • Prerequisite
  • How to create Tailwind CSS Animation.
  • How to use customized animation in Tailwind CSS.
  • Conclusion

What is Animation

Animations are used to make elements move from one position to another over a period of time on your website. Aminations are often, at times, triggered by events such as scrolling, loading of webpage or applications. In this Tailwind CSS tutorial, we will look at how to use the tailwind CSS animation.

Tailwind CSS offers us some default animations such as

  • Animate-spin
  • Animate-bounce
  • Animate-ping
  • Animate-pulse

As their name implies, we will use all of this default utility class animation to build a website loader. At the end of the tailwind CSS tutorial, our project will look like the image below.

Tailwind CSS Animations

Prerequisite

In order to make the most of this Tailwind CSS tutorial, you need the following

  • Basic Knowledge in HTML and CSS animations.
  • Tailwind CSS installed in your project. To figure out how to this, you can check out our article on installing TailwindCSS here.

Building our Loader

Before we jump right in, it is imperative that you link your Tailwind CSS stylesheet to your HTML. Just like the code below

1<link rel="stylesheet" href="styles.css">

Your projects's HTML head should like this

1<!DOCTYPE html>
2<html lang="en">
3
4<head>
5 <meta charset="UTF-8">
6 <meta http-equiv="X-UA-Compatible" content="IE=edge">
7 <meta name="viewport" content="width=device-width, initial-scale=1.0">
8 <link rel="stylesheet" href="styles.css">
9 <title>Website loader</title>
10</head>

We can go ahead to build our loader. To make our loader as rich as possible, we will create four different kind of animations with four different TailwindCSS animation classes.

1<body class="flex justify-center items-center bg-white-900 h-screen">
2    <div class="bg-white flex space-x-2 p-5 justify-center items-center">
3        <div class="m-auto ml-10 h-20 w-20 bg-blue-900 p-2 animate-spin"></div>
4        <div class="m-auto ml-10 h-20 w-20 bg-purple-500 p-2 animate-ping"></div>
5        <div class="m-auto ml-10 h-20 w-20 bg-green-600 p-2  animate-bounce"></div>
6        <div class="m-auto ml-10 h-20 w-20 bg-blue-500 p-2 animate-pulse"></div>
7    </div>
8</body>

We use class names for our divs to affect different properties of our div element to . These class names are:

  • m-auto, centers your div horizontally. Offers the same functionaliity as the native CSS margin: auto.
  • ml-10, gives your div a margin-left of 10px. Offers the same functionaliity as the native CSS margin-left: 10px .
  • h-20, defines the height of your div. Offers the same functionality as the native CSS height: 20px .
  • w-20, defines the width of your div. Offers the same functionaliity as the native CSS width: 20px .

The animation classes we use to define the animation we want in our project are :

  • animate-spin, this gives your element a linear spin animation.
  • animate-ping, this makes the element scale and fade out.
  • animate-bounce, this gives your element the illusion of bouncing.
  • animate-pulse, this makes your element gently fade in and out.

Our animated loader should look like the image below.

Tailwind CSS Animations

Custom Animations

Tailwind CSS animations are not limited to only the animations mentioned above, you have the freedom to create more complex, and more interesting animations. Asides from the div element we could animate other HTML elements, an example would be to animate the button element to make them more responsive.

With the Tailwind CSS animation, you can customize your animation to better suit your needs. You can add, remove, change animations in the tailwind.config.js file. We define the animation behaviour in the animation section of the module.exports object.

1module.exports = {
2 theme: {
3 extend: {
4 animation: {
5 'spin-slow': 'spin 3s linear infinite',
6 }
7 }
8 }
9}

You can also use @keyframes to define animations not already created in native CSS.

1// tailwind.config.js
2
3module.exports = {
4 theme: {
5 extend: {
6 keyframes: {
7 wiggle: {
8 '0%, 100%': { transform: 'rotate(-3deg)' },
9 '50%': { transform: 'rotate(3deg)' },
10 }
11 }
12 }
13 }
14}

With the @keyframes we define the animation for when it is 0% and 100% complete, we want to rotate the element 3 degrees left, and when it is 50% complete we want to rotate the element 3 degrees right.

After creating the animation we can reference the wiggle animation in the animation section of our module.exports object.

1module.exports = {
2 theme: {
3 extend: {
4 animation: {
5 wiggle: 'wiggle 1s ease-in-out infinite',
6 }
7 }
8 }
9}

Once you have added your custom animation, remember to run npm run build to build it to your CSS.

Conclusion

In this article we learned what a web animation is, how to animate elements and create custom animations. We used this knowledge to create loaders in our applications.

Create Stunning websites and web apps

Trying to build out all user interfaces and components for your website or web app from scratch can become a very tedious task. A huge reason why we created Contrast Bootstrap to help reduce the amount of time we spend doing that, so we can focus on building some other aspects of the project. Contrast Bootstrap PRO consists of a UI Kit featuring over 10000+ component variants. Together with a template of 5 admin dashboards and 23+ additional multipurpose pages template for building almost any type of website or web app. You can view a demo and learn more about Contrast by clicking here.Try contrast pro

ad-banner

Contrast Bootstrap PRO was built using the most popular CSS framework Bootstrap to help build your next landing, admin SAAS, prelaunch etc project with a clean, prebuilt and well documented template and UI components.Learn more about Contrast

Related Posts

Comments

...