Introduction to CSS Transitions and Animations
Written by
Anil Kumar
Front End Developer
Table of contents
Build with Radial Code
In this blog, we'll delve into the intricate details of these powerful CSS features, uncovering their properties and capabilities. By the end, you'll have a thorough understanding of how to wield transitions and animations to create captivating web experiences.
Transitions
A transition in web design refers to the gradual change in a CSS property from one state to another over a specified duration. It allows elements to smoothly transition between different styles, such as color, size, position, or opacity, creating a more polished and dynamic user experience.
Transitions are commonly used to enhance user interactions, such as hovering over a button or clicking on a link. By applying transitions, you can make these interactions more visually engaging by adding smooth animations. For example, a button changing color when hovered over can be achieved with a transition effect.
CSS transitions have several properties that you can use to control how the transition happens. Here are the main ones:
- Transition-property: This specifies the CSS property that you want to apply the transition effect to. You can name a specific property like coloror opacity, or use allto apply to all changeable properties.
- Transition-duration: This sets how long the transition takes to complete. It's usually measured in seconds (s) or milliseconds (ms).
- Transition-timing-function: This defines the speed curve of the transition, which affects how the transition will progress over its duration. Common values for transition-timing-function include linear,ease,ease-in, ease-out, and ease-in-out.
- Transition-delay: This causes a delay before the transition starts. Like duration, it's also measured in seconds or milliseconds.
Example:
.example {
transition-property: background-color;
}
Example:
.example {
transition-duration: 2s;
}
Example:
.example {
transition-timing-function: ease-in;
}
Example:
.example {
transition-delay: 1s;
}
You can also combine all these properties into a single transition shorthand property:
Example:
.example {
transition: background-color 2s ease-in 1s;
}
In this example, when the .example class is triggered (like on hover), the background-color will change over a period of 2 seconds following an ease-in timing function, but only after waiting for 1 second due to the transition-delay.
Here's a full example showing how to use these properties together:
HTML:
<button class="button">Button</button>
CSS:
.button {
padding: 10px 20px;
background-color: blue;
color: white;
border: none;
outline: none;
cursor: pointer;
/* Apply the transition to background-color and transform properties */
transition-property: background-color, transform;
/* The transition will take 0.5 seconds */
transition-duration: 0.5s;
/* The transition will start after a delay of 0.2 seconds */
transition-delay: 0.2s;
/* The transition will speed up at the start and slow down at the end */
transition-timing-function: ease-in-out;
}
.button:hover {
background-color: orange;
color:black;
/* Scale up the button slightly */
transform: scale(1.1);
}
When you hover over the button with your mouse, it will change from blue to green and grow slightly larger smoothly over half a second, after a short delay, and with an ease-in-out speed curve.
UI (This is an example and does not correspond to the UI code.):
CSS Animations: Dynamic Keyframe-based Effects
CSS animations enable more complex and dynamic effects by defining keyframes. Let's break down the properties:
- @keyframes: The @keyframes rule in CSS is used to define the animation sequence by specifying keyframes at various points in time. Keyframes represent the stages of an animation's timeline and define the style changes that occur at specific percentages of the animation's duration. Each keyframe consists of a selector followed by curly braces containing the CSS properties and values to be applied at that point in the animation.
- Animation-name: The animation-name property in CSS specifies the name of the keyframe animation defined using the @keyframes rule. Once you define an animation sequence using @keyframes, you can reference it using the animation-name property to apply the animation to elements on your webpage.
- Animation-duration: The animation-duration property in CSS sets the duration over which an animation should occur. It's similar to the transition-duration property, but instead of defining the duration of a transition effect, it specifies how long an animation lasts from start to finish. This duration determines the total time it takes for the animation to complete one cycle.
- Animation-timing-function:The animation-timing-function property in CSS determines the timing of an animation, controlling how it accelerates and decelerates over its duration. This property is analogous to the transition-timing-function property used for transitions. It allows you to specify the rate of change of the animation effect, influencing the perception of motion and smoothness.
.box {
animation-name: change-background;
}
.box {
animation-duration: 3s;
}
.box {
animation-timing-function: ease-in;
}
Here are the other properties of animation-timing-function:
- Ease: This is the default setting. It starts slow, gets faster in the middle, and slows down at the end.
- Linear:The animation moves at a constant speed from start to finish.
- Ease-in: The animation starts slowly and speeds up as it progresses.
- Ease-out: The animation starts fast and slows down towards the end.
- Ease-in-out: The animation starts and ends slowly, but is faster in the middle.
- Cubic-bezier: This is a custom function where you can define your own values to create a unique timing curve.
- Steps: This breaks the animation into steps, creating a jumping effect rather than a smooth transition.
You can apply these timing functions in your CSS code when defining animations to make them feel more natural.
- Animation-delay: The animation-delay property in CSS delays the start of an animation for a specified amount of time after it's triggered. This delay allows you to control when the animation begins playing relative to the time it's applied to an element. It functions similarly to the transition-delay property but applies to animations instead of transitions.
- Animation-iteration-count: The animation-iteration-count property in CSS controls how many times an animation cycle repeats before stopping. By default, animations play once, but you can adjust this behavior by specifying a specific number of repetitions or setting it to infinite for continuous looping. This property offers flexibility in defining the duration and repetition of animations, allowing you to tailor them to your design needs.
- Animation-direction: The animation-direction property in CSS specifies the direction in which an animation should play. It determines whether the animation progresses forwards (normal), backwards (reverse), alternates between forwards and backwards (alternate), or alternates and repeats in reverse (alternate-reverse).
- normal: The animation plays forward from the beginning to the end (default).
- reverse: The animation runs in reverse, playing from the end to the beginning.
- alternate: The animation plays forwards and then backwards in a continuous loop.
- alternate-reverse: The animation plays backwards and then forwards in a continuous loop.
- Animation-fill-mode: The animation-fill-mode property in CSS determines how the styles are applied to an element before and after an animation is executed. It specifies whether the styles defined in the keyframes should be applied before the animation starts (forwards), after it ends (backwards), both before and after (both) or not at all (none).
- none: No styles are applied before or after the animation executes. The element remains in its initial state.
- forwards: The styles defined in the last keyframe (100%) are applied after the animation ends and persist.
- backwards: The styles defined in the first keyframe (0%) are applied before the animation starts and persist until the animation begins.
- both: The styles are applied both before and after the animation execution, combining the effects of forwards and backwards.
.box {
animation-delay: 500ms;
}
.box {
animation-iteration-count: 3;
}
.box {
animation-direction: alternate;
}
Here are the other properties of animation-direction:
.box {
animation-fill-mode: forwards;
}
Here are the other properties of animation-fill-mode:
Conclusion:
CSS transitions and animations are indispensable tools for creating engaging and interactive web experiences. By mastering their properties and understanding their nuances, you can unleash your creativity and elevate your designs to new heights. Experiment with different combinations of properties, durations, and timing functions to craft unique and compelling animations that captivate your audience.
Start incorporating these techniques into your projects today, and watch as your web designs come to life with dynamic flair and sophistication.