Keyframes
Add to favorites
Create beautiful animations in CSS using keyframes
CodeSandbox link
You can find the full code for this tutorial at https://codesandbox.io/s/css-keyframes-yuxkc.
What is CSS keyframe?
CSS @Keyframes determine what styles an element will have at a given time. They are responsible for controlling intermediate stages of an animation. A keyframe allows greater control of an animation's progress.
To utilize this property, import keyframes from styled-components. The syntax is provided below:
import styled, { keyframes } from "styled-components"
Keyframe syntax
Keyframes must have a Name that binds the animation to the element. This property has two values, "from" and "to," which determine the style of the animation at the start (0%) and end (100%).
const textfade = keyframes`
from {
opacity: 0;
transform: translateY(-10px);
}
to {
opacity: 1;
transform: translateY(0px);
}
`;
Animation name
The animation must be defined in the animation-name property of an element.
const Title = styled.h1`
animation-name: ${textfade};
animation-duration: 1s;
`
Multiple control points
You can also use percentages to have more control over the timeline of the animation. As a result, you will be able to control anything in-between like adding new properties at a specific point in the animation.
const textfade = keyframes`
0% {
opacity: 0;
transform: translateY(-10px);
}
80% {
opacity: 0.5;
transform: translateY(-10px);
}
100% {
opacity: 1;
transform: translateY(0px);
}
`;
CSS Animation Properties
Below are the sub-properties of CSS Animation :
- animation-name: Specifies the name of the @Keyframes
- animation-duration: Defines the length of an animation
- animation-delay: Determines the delay for the start of an animation
- animation-iteration-count: Sets how many times to play an animation
- animation-direction: Determines if the animation will be reversed on alternate cycles
- animation-play-state: Indicates whether the animation is running or paused
- animation-timing-function : Sets how an animation progresses through the duration of each cycle
const Title = styled.h1`
animation-name: textfade;
animation-duration: 1s;
animation-delay: 2s;
animation-iteration-count: 3;
animation-direction: reverse;
animation-play-state: paused;
animation-timing-function: ease-in-out;
`
Shorten the code
Your code will become very bulky if you add animations to every element, as you can see. Let's combine all the values. Values can be arranged in any order except when both delay and duration are used. By using this method you will have fewer lines of code.
const Title = styled.h1`
animation: ${textfade} 1s 0.1s forwards ease-in-out;
opacity: 0;
`;
Reusability
The keyframes are also reusable, so you can add them to another element.
const Caption = styled.h2`
animation: ${textfade} 1s 0.7s forwards ease-in-out;
opacity: 0;
`;
Multiple animations
You can have multiple animations for a specific element simply by separating the values by a "comma". In the example: you can see how it's exactly done.
const textslide = keyframes`
0% {
transform: translateX(-20px);
}
100% {
transform: translateX(0px);
}
`;
const Caption = styled.h2`
animation: ${textfade} 1s 0.7s forwards ease-in-out,
${textslide} 1s 0.7s forwards ease-in-out;
opacity: 0;
`;
Learn with videos and source files. Available to Pro subscribers only.
Purchase includes access to 50+ courses, 320+ premium tutorials, 300+ hours of videos, source files and certificates.
Templates and source code
Download source files
Download the videos and assets to refer and learn offline without interuption.
Design template
Source code for all sections
Video files, ePub and subtitles
Assets
Videos
Subtitles
1
Styled Components
React library for styling your components
5:36
2
Grid Layout
The best way to create a beautiful and responsive layout
16:09
3
Media Query
Introduction to Responsive Web Design
8:24
4
Keyframes
Create beautiful animations in CSS using keyframes
12:34
5
Glassmorphism Animation
Bring your frosted glass design to life with beautiful animations
14:21
6
CSS line animations
Learn how to create a CSS line animation
15:47
Meet the instructor
We all try to be consistent with our way of teaching step-by-step, providing source files and prioritizing design in our courses.
Willie Yam
Front-end/UI developer at Design+Code
I do UI coding. HTML/CSS/JS/SWIFTUI dev.
10 courses - 37 hours
![course logo](http://images.ctfassets.net/ooa29xqb8tix/6LKKWLFkx6JrlMY2mWPocd/2513a06a163424ba900bcff844c2c3fb/play-logo.png?w=400&q=50?fm=jpg&q=50)
Design and Prototype an App with Play
Build a completely functional prototype without writing a single line of code from your phone
3 hrs
![course logo](http://images.ctfassets.net/ooa29xqb8tix/1bYdUtniEAUH3jdqQd3Qc1/7cf21d20882bfe59f01d7bc74e81010d/react-logo.png?w=400&q=50?fm=jpg&q=50)
Create a 3D site with game controls in Spline
Build an interactive 3D scene implemented on a ReactJS site using Figma and Spline
2 hrs
![course logo](http://images.ctfassets.net/ooa29xqb8tix/4bWEQMRsA6PzjiHljsxryp/55d78d84da072e9c22caddbccd67396b/swift-logo.png?w=400&q=50?fm=jpg&q=50)
Build a Movie Booking App in SwiftUI
Learn how to create an iOS app based on a beautiful UI design from Figma with interesting animations and interactions, create a custom tab bar and use navigation views to build a whole flow
1 hrs
![course logo](http://images.ctfassets.net/ooa29xqb8tix/628IYmTv4uib8slYz9iuok/3de9010de04ae92a23c94f9885746db2/swift-logo.png?w=400&q=50?fm=jpg&q=50)
Build Quick Apps with SwiftUI
Apply your Swift and SwiftUI knowledge by building real, quick and various applications from scratch
11 hrs
![course logo](http://images.ctfassets.net/ooa29xqb8tix/0cjyplK9nhXoHTHN3XfTr/0e1edc5106a4c99ca2de7eeaaa772603/ui-logo.png?w=400&q=50?fm=jpg&q=50)
CSS Handbook
A comprehensive series of tutorials that encompass styled-components, CSS, and all layout and UI developments
1 hrs
![course logo](http://images.ctfassets.net/ooa29xqb8tix/1bYdUtniEAUH3jdqQd3Qc1/7cf21d20882bfe59f01d7bc74e81010d/react-logo.png?w=400&q=50?fm=jpg&q=50)
Advanced React Hooks
Learn how to build a website with Typescript, Hooks, Contentful and Gatsby Cloud
5 hrs
![course logo](http://images.ctfassets.net/ooa29xqb8tix/4k80OoiAH74E4JWnzT7iHv/d8b989084c313064df5be11f940ea48b/unity-logo.png?w=400&q=50?fm=jpg&q=50)
Unity for Designers
If you want to make a game and don't know where to start, you are in the right place. I will teach you how to use Unity, code in C# and share essential tips and tricks to make your first game.
5 hrs
![course logo](http://images.ctfassets.net/ooa29xqb8tix/1YISP6mvD83Gao3lQdhWqi/8b36d37d05f77a894dd0b7d30f8e6753/js-logo.png?w=400&q=50?fm=jpg&q=50)
Create a Javascript Game
Learn how to create a web game using Phaser 3, a popular javascript game engine. Draw a map using an editor, implement the player, make the player move, apply physics, collisions, and implement the enemies.
2 hrs
![course logo](http://images.ctfassets.net/ooa29xqb8tix/4bWEQMRsA6PzjiHljsxryp/55d78d84da072e9c22caddbccd67396b/swift-logo.png?w=400&q=50?fm=jpg&q=50)
Build an ARKit 2 App
Introduction to ARKit and learn how to make your own playground. You will be able to add models or even your own designs into the app and play with them
4 hrs
![course logo](http://images.ctfassets.net/ooa29xqb8tix/4bWEQMRsA6PzjiHljsxryp/55d78d84da072e9c22caddbccd67396b/swift-logo.png?w=400&q=50?fm=jpg&q=50)
Create a SpriteKit Game
Overview of SpriteKit a powerful 2D sprite-based framework for games development from Apple and learn how to create your very own platform
3 hrs