When you are designing modern web pages, animations play a vital role in enhancing user experience. One of the most powerful ways to create animations in CSS is by using keyframes in CSS. In this blog, you will learn what keyframes are, how to use them, and some practical examples to make your websites more dynamic.
Table of Contents:
What are Keyframes in CSS?
Keyframes in CSS let you create animations on a webpage. They tell the browser what styles an element should have at different times during the animation, like the beginning, middle, and end.
For example, you can use keyframes in CSS to move an object, change its color, or make it fade in. You write the steps using @keyframes, and then apply the animation to an element with the animation property.
CSS Keyframes Syntax
Here is the syntax of CSS keyframes:
@keyframes animation-name {
0% {
/* styles at the start */
}
100% {
/* styles at the end */
}
}
Explanation:
- @keyframes: This is the rule that defines the animation.
- animation-name: The name you assign to your animation. You’ll use this name when applying the animation to an element.
- 0%, 50%, 100%: Animation progress is shown by the percentages:
- 0% is the start
- 100% is the end
- You can also add in-between steps like 25%, 50%, etc.
- Inside each { }, you write the CSS styles you want at that point.
Example of Keyframes in CSS
Given below is a CSS keyframe example which shows how to move a box from left to right.
Output:
Learn to Build Stunning Websites and Apps from Scratch
Master Web Development
Keyframes Timing Functions
Timing functions control how the speed of an animation changes over time, whether it starts slow, speeds up, or stays the same. They’re used with the animation-timing-function property. Some common timing functions are given below:
| Function |
Description |
| linear |
Constant speed throughout the animation |
| ease |
Starts slow, accelerates, then slows down (default) |
| ease-in |
Starts slow and accelerates to full speed |
| ease-out |
Starts fast and decelerates to stop |
| ease-in-out |
Starts slow, accelerates, then decelerates |
| steps(n) |
Jumps between n discrete states (good for frame-by-frame animations) |
| cubic-bezier(x,y,x,y) |
Custom speed curve defined by control points |
CSS Keyframes vs CSS Transitions
Given below, we have provided a difference table for CSS Keyframes vs Transitions for your reference:
| Feature |
CSS Keyframes |
CSS Transitions |
| Purpose |
Used to create complex, multi-step animations. |
Used to smoothly change a property from one state to another. |
| Control |
Allows you to define multiple stages of an animation. |
Only animates from the initial state to the final state. |
| Trigger |
Runs automatically based on the animation property; can loop or repeat. |
Runs only when a property changes, like on hover, click, or focus. |
| Flexibility |
Very flexible; you can animate multiple properties and create sequences. |
Less flexible; usually handles simple one-step property changes. |
| Duration & Timing |
Set using animation-duration, animation-timing-function, animation-iteration-count, etc. |
Set using transition-duration, transition-timing-function, transition-delay. |
| Looping |
Can loop infinitely or for a specific number of times. |
Does not loop automatically; runs once per trigger. |
| Best Use |
Complex animations like bouncing, moving, and color changes in sequence. |
Simple effects like hover buttons, fade-ins, or smooth size changes. |
Using Keyframes with Animation
You need two parts to create an animation in CSS.
- @keyframes – @keyframes in CSS are used to define what happens during the animation (the steps).
- animation properties – tell the element how to play the animation.
Given below are some CSS animation examples.
For example:
You want a box to fade in and move to the right using keyframes in CSS.
Output:
Animation properties used:
- animation-name: the name of the keyframes (fadeAndMove).
- animation-duration: how long the animation takes (3s).
- animation-timing-function: how the speed changes (ease-in-out means slow → fast → slow).
- animation-iteration-count: how many times it runs (1 means once).
CSS Animation Properties
Here, the CSS animation properties are explained below in tabular format:
| Property |
What It Does |
Example |
| animation-name |
The name of your @keyframes animation |
animation-name: slide; |
| animation-duration |
How long the animation runs |
animation-duration: 2s; |
| animation-timing-function |
Controls the speed curve of the animation |
animation-timing-function: ease; |
| animation-delay |
The delay before the animation begins |
animation-delay: 1s; |
| animation-iteration-count |
Number of times the animation repeats |
animation-iteration-count: infinite; |
| animation-direction |
Direction of animation (normal, reverse, alternate) |
animation-direction: alternate; |
| animation-fill-mode |
Styles to use before/after animation |
animation-fill-mode: forwards; |
| animation-play-state |
Determines whether the animation is running or paused |
animation-play-state: paused; |
NOTE: You can use multiple properties in one shorthand. For example,
animation: fadeIn 3s ease-in 1s 2 alternate forwards;
(That means: name, duration, timing, delay, count, direction, fill mode)
Multiple Animations
You define multiple @keyframes and then list their names and properties in the animation or related properties.
Example: We will make a box blue, turn it red, and move horizontally to the right and the left.
Output:
NOTE:
- Each animation property (like animation-name, animation-duration, etc.) can accept multiple values, separated by commas.
- The first value applies to the first animation, the second to the second, and so on.
- All animations run at the same time unless you add different animation-delay values.
Examples of CSS Keyframes
Some CSS examples for beginners are given below for your reference:
1. Rotate
We are going to spin an object circularly by CSS keyframes.
Output:
Explanation:
- body defines display: flex, align-items: center, and justify-content: center to center the element with the class of .box inside the viewport.
- height: 100vh makes sure the body fills up the whole screen height.
- The .box spins continuously in the center.
2. Pulse (Heartbeat)
We will make an object move like a heartbeat using keyframes in CSS.
Output:
Explanation:
- border-radius: 50%; makes the box a circle.
- animation: pulse 1s ease-in-out infinite; loops the pulse animation elegantly and endlessly.
- transform: scale(…); enlarge and undo the enlargement of the circle as the animation goes on.
3. Flip Card Effect
CSS keyframes will be used to flip an object, such as a card.
Output:
Explanation:
- animation: flip 2s ease-in-out infinite alternate; runs the flip animation smoothly, back and forth, forever.
- transform: rotateY(…); rotates the element clockwise or counterclockwise on the Y-axis to make a flipping effect.
- transform-style: preserve-3d; Maintains a 3D appearance when the flip occurs, which means it is lifelike.
The following are some of the tips on how to easily and effectively use CSS animations and keyframes:
- Only animate easy things: Try to animate opacity (fading) and transform (moving, scaling, rotating). These are fast for the browser.
- Don’t animate size or position: Changing things like width, height, top, or left can slow down your website.
- Don’t animate too many things at once: If lots of things move at the same time, your site may get laggy, especially on phones.
- Use a special hint (will-change) only when needed: It can help, but using it too much can hurt performance.
- Use CSS animations instead of JavaScript when you can: They’re faster and smoother most of the time.
- Short animations: Animations that last very short (such as 0.2 to 0.3 seconds) appear quicker and responsive.
- Test on slow devices as well: On your computer, everything may look smooth, but it does not mean slow phones will be smooth as well.
- Let the browser do its job: Don’t make too many changes at once. The browser needs time to draw everything smoothly.
CSS vs JS Animations
A very simple comparison of CSS animations vs JavaScript animations is as follows:
| Feature |
CSS Animations |
JavaScript Animations |
| Ease of use |
Easy to write with basic code |
Needs more code and logic |
| Performance |
Usually smoother and faster |
Can be smooth, but depends on how it’s written |
| Control |
Less flexible (can’t react to user input easily) |
Very flexible (can respond to clicks, scrolls, etc.) |
| Timing functions |
Built-in (ease, linear, etc.) |
Customizable with math or libraries |
| Interactivity |
Limited (triggers like hover or load) |
Great for interactive effects |
| Browser handling |
Runs on GPU, often very efficient |
Runs on the main thread, may slow down if not optimized |
| Best for |
Simple animations (fade, slide, scale) |
Complex, dynamic animations |
Get 100% Hike!
Master Most in Demand Skills Now!
Conclusion
Learning keyframes in CSS makes it much easier to bring your web pages to life. With keyframes, you can control exactly how elements change over time, creating smooth and eye-catching animations. Whether it’s a small hover effect or a more complex movement, keyframes give you the power to make your site more engaging. Start experimenting with them, and you’ll see how even simple animations can make a big difference in how your website feels and looks.
You can also check CSS Interview Questions and our Web Development course to learn more about CSS3.
Keyframes in CSS – FAQs
Q1. Can CSS keyframes be used to animate multiple properties at once?
Yes, you can animate several properties together by defining them inside the same keyframe.
Q2. Are CSS keyframes supported in all modern browsers?
Yes, all major browsers support CSS keyframes, though some may need vendor prefixes for older versions.
Q3. Can I control the speed of individual steps in a keyframe animation?
Yes, you can adjust timing using percentages and easing functions for each stage.
Q4. Can keyframes be used to create looping animations?
Yes, by setting animation-iteration-count to infinite or a specific number of times.
Q5. Is it possible to trigger keyframe animations on hover only?
Yes, you can start animations on hover by applying the animation in a :hover selector.
Q6. Can keyframes animate non-visual properties like width or padding?
Yes, keyframes can animate any animatable CSS property, including size, padding, and margins.
Q7. Can keyframes be nested inside other CSS rules?
No, keyframes must be defined at the top level of your CSS, not inside selectors.
Q8. Do keyframes work with CSS variables?
Yes, you can animate CSS variables by updating their values in keyframes.