📖 Scenario: You want to create a simple web page that shows a box with a smooth color change animation. This animation will make the box change its background color from blue to green and back continuously.
🎯 Goal: Build a web page using Tailwind CSS that includes a custom keyframe animation named colorchange. The animation should smoothly change the background color of a box from blue to green and back in a loop.
📋 What You'll Learn
Create a Tailwind CSS configuration file with a custom keyframe animation named colorchange that changes background color from blue to green and back.
Add a custom animation named colorchange that uses the keyframes with a duration of 4 seconds and infinite looping.
Create an HTML file with a div element styled as a box using Tailwind classes.
Apply the custom colorchange animation to the box so it animates continuously.
💡 Why This Matters
🌍 Real World
Custom animations help make websites more engaging and visually appealing by adding smooth transitions and effects.
💼 Career
Knowing how to extend Tailwind CSS with custom animations is useful for frontend developers to create unique UI experiences without writing raw CSS.
Progress0 / 4 steps
1
Create Tailwind CSS config with colorchange keyframes
Create a Tailwind CSS configuration file named tailwind.config.js. Inside the extend section of theme, add a keyframes object with a key named colorchange. Define the keyframes so that at 0% and 100% the background color is #3b82f6 (blue), and at 50% the background color is #22c55e (green).
Tailwind
Hint
Remember to put the keyframes inside extend under theme in tailwind.config.js.
2
Add custom animation named colorchange in Tailwind config
In the same tailwind.config.js file, inside the extend section of theme, add an animation object. Define an animation named colorchange that uses the keyframes colorchange with a duration of 4s, timing function ease-in-out, and infinite repetition.
Tailwind
Hint
Define the animation property with the same name as the keyframes and specify duration, easing, and infinite loop.
3
Create HTML file with a box div
Create an HTML file named index.html. Inside the body, add a div element with Tailwind classes w-40 and h-40 to make a square box. Also add rounded-lg for rounded corners and shadow-lg for a subtle shadow.
Tailwind
Hint
Use Tailwind classes to create a visible square box with rounded corners and shadow.
4
Apply the custom colorchange animation to the box
In the index.html file, add the Tailwind class animate-colorchange to the div box so it uses the custom animation you defined.
Tailwind
Hint
Just add the animate-colorchange class to the box div.
Practice
(1/5)
1. What is the main purpose of defining custom keyframe animations in Tailwind CSS?
easy
A. To create unique movement effects on webpage elements
B. To change the color palette of the website
C. To add new font families to the project
D. To optimize image loading speed
Solution
Step 1: Understand what keyframe animations do
Keyframe animations define how elements move or change over time.
Step 2: Relate to Tailwind's custom animations
Custom keyframe animations let you create unique movement effects beyond default animations.
Final Answer:
To create unique movement effects on webpage elements -> Option A
Quick Check:
Custom animations = unique movements [OK]
Hint: Think about what animations visually do on a page [OK]
Common Mistakes:
Confusing animations with color or font changes
Thinking animations improve loading speed
Mixing animations with layout changes
2. Which of the following is the correct way to define a custom keyframe animation in Tailwind's tailwind.config.js?
But the animation does not run on your element with class="animate-spinSlow". What is the likely problem?
medium
A. You forgot to restart the development server after changing the config
B. The keyframe percentages must be 0 and 1, not 0% and 100%
C. The animation name in the class should be spin-slow instead of spinSlow
D. Tailwind does not support custom animations
Solution
Step 1: Check Tailwind config syntax
The keyframe and animation syntax is correct with '0%' and '100%' and camelCase names.
Step 2: Understand Tailwind build process
Changes in tailwind.config.js require restarting the development server to apply.
Final Answer:
Forgot to restart the development server after config change -> Option A
Quick Check:
Restart server after config edits [OK]
Hint: Restart dev server after config changes to apply them [OK]
Common Mistakes:
Using wrong percentage syntax in keyframes
Wrong animation class name format
Believing Tailwind can't do custom animations
5. You want to create a custom Tailwind animation that moves an element up 20px and fades it in simultaneously over 1.5 seconds. Which config snippet correctly defines this animation?