CSS Transitions – How to Create Smooth Transitions between CSS Values
CSS transitions provide a smooth and gradual way to change a specific CSS property’s value.
So, instead of allowing browsers to change a property’s value immediately, CSS transitions cause the change to happen smoothly over a specified period of time.
For instance, suppose you wish to change an element’s size on hover. In that case, you have two options:
- Implement the change without CSS transitions.
- Use CSS transitions to transition smoothly from the element’s initial size to its new state.
Let’s see some examples of the two options.
How to Change an Image’s Size on Mouse Hover without Using CSS Transitions
The code above instantaneously changes the image’s size from its initial width (40%
) to its new dimension (100%
) because we did not use CSS transitions.
With CSS transitions, you will get a much more pleasing experience. Let’s see an example below.
How to Change an Image’s Size on Mouse Hover with CSS Transitions
We used the transition
property to create a smooth and gradual transition from width: 40%
to width: 100%
.
Don’t worry if you are unfamiliar with the transition code above. In the following sections, we will discuss how CSS transitions work.
Let’s start by discussing what CSS transition properties are.
What Are the CSS Transition Properties?
The CSS transition properties define how browsers should apply transition effects on an HTML element.
Categories of CSS Transition Properties
We have two categories of CSS transition properties:
- Required transition properties
- Optional transition properties
Let’s discuss the two.
What Are the Required CSS Transition Properties?
The two required properties you need to create CSS transitions are:
transition-property
transition-duration
What is the CSS transition-property
?
The CSS transition-property
specifies the CSS property you wish to transition from its initial to its new state.
What is the CSS transition-duration
property?
The CSS transition-duration
property defines the length of time in which browsers should complete the selected element’s transition. In other words, transition-duration
sets the total start-to-finish time.
Note the following:
- The
transition-duration
property only accepts time in milliseconds (ms) or seconds (s). - Zero seconds (
0s
) is thetransition-duration
’s default value. Therefore, no transition event will occur if you do not define atransition-duration
property. transition-duration
accepts only a zero (0
) or a positive numeric value. Browsers will ignore the duration declaration if you provide anything else.
Examples of the required CSS transition properties
Below are some examples of the two required CSS transition properties.
How to transition an element’s width within three seconds
Here’s what we did in the snippet above:
- The
transition-property
tells browsers to transition theimg
’swidth
from its initial value (40%
) to its new state (100%
). - We used the
transition-duration
property to define three seconds (3s
) duration from the start to the end of the transition.
Therefore, instead of an instantaneous change from the image’s initial width (40%
) to its new size (100%
), browsers will transition smoothly between the old and new state in three seconds (3s
).
How to transition a font’s size within five seconds
Here’s what we did in the snippet above:
- The
transition-property
informs browsers to transition thep
element’sfont-size
. - We used the
transition-duration
property to define five seconds (5s
) duration from the start to the end of the transition.
Therefore, instead of an immediate change from the paragraph’s initial font size (1rem
) to its new size (7rem
), browsers will transition smoothly between the old and new state in five seconds (5s
).
Let’s now discuss the optional CSS transition properties.
Design in Figma, launch in Webflow
What Are the Optional CSS Transition Properties?
The two optional CSS transition properties are:
transition-timing-function
transition-delay
What is the CSS transition-timing-function
property?
The CSS transition-timing-function
property defines the implementation timing (speed) of the selected element’s transition.
In other words, the transition-timing-function
specifies the speed for implementing the transition at various intervals of its duration.
The values the transition-timing-function
property accepts are:
ease
: Starts the transition slowly. Then, speeds it up and ends it slowly.ease
is thetransition-timing-function
property’s default value. It is equivalent tocubic-bezier(0.25,0.1,0.25,1)
.ease-in
: Starts the transition slowly with a gradual increase in speed. It is equal tocubic-bezier(0.42,0,1,1)
.ease-out
: Start fast and end the transition slowly. It is equivalent tocubic-bezier(0,0,0.58,1)
.ease-in-out
: Starts and ends the transition slowly. It is equivalent tocubic-bezier(0.42,0,0.58,1)
.linear
: Starts and ends the transition using a consistent speed throughout the transition’s duration. It is equivalent tocubic-bezier(0,0,1,1)
.cubic-bezier(p1, p2, p3, p4)
: Allows you to define the values of the cubic-bezier curve.
What is the CSS transition-delay
property?
The CSS transition-delay
property defines how long the browser should wait before it starts the transition.
Note the following:
- The
transition-delay
property must be in milliseconds (ms) or seconds (s). 0s
is thetransition-delay
’s default value. It causes the transition to start immediately from the moment browsers apply it to an HTML element.- A negative value causes the transition to start immediately from the specified time. For instance, suppose an element’s
transition-delay
value is-3s
. In that case, the transition will begin immediately at 3 seconds. - A positive value causes the transition to start after the specified delay time has elapsed. For instance, suppose an element’s
transition-delay
value is3s
. In that case, the transition will begin after a 3-second delay.
Examples of the optional CSS transition properties
Below are some examples of the two optional CSS transition properties.
How to transition an element’s width with an ease-out speed
Here’s what we did in the snippet above:
- The
transition-property
informs browsers to transition theimg
element’s width. - We used the
transition-duration
property to define three seconds (3s
) duration from the start to the end of the transition. - We used the
transition-timing-function
property to begin the transition quickly and end it slowly.
How to transition an element’s width with a linear speed
Here’s what we did in the snippet above:
- The
transition-property
informs browsers to transition theimg
element’s width. - We used the
transition-duration
property to define three seconds (3s
) duration from the start to the end of the transition. - The
transition-timing-function
property tells browsers to transition from the element’s initial width to its new size using a consistent transition speed throughout.
Create NPM Package like a pro
How to transition an element’s width with a two-second delay
Here’s what we did in the snippet above:
- The
transition-property
informs browsers to transition theimg
element’s width property. - We used the
transition-duration
property to define three seconds (3s
) duration from the start to the end of the transition. - The
transition-timing-function
property transitions theimg
element’s width using a consistent transition speed. - We used the
transition-delay
property to apply a two-second (2s
) delay to the starting time of the transition.
Now that we know the CSS transition properties, we can discuss defining them with a shorthand syntax.
Shorthand for Defining the CSS Transition Properties
We use the transition
property as shorthand for the transition-property
, transition-duration
, transition-timing-function
, and transition-delay
properties.
In other words, instead of writing:
You can alternatively use the transition
property to shorten your code like so:
Here is the transition
property’s syntax:
Note that you can use the transition
property to transition the state of multiple CSS properties.
Here’s an example:
The snippet above used commas (,
) to separate each of the transitional properties we are applying to the img
element.
Important Stuff to Know about CSS Transitions
- You can’t animate all CSS properties. Have a look at MDN’s Animatable CSS properties article to see the ones you can animate. It’s not all CSS properties you can animate.
- You can use CSS transitions to make JavaScript functionality smooth.
- CSS transition is an expensive operation for most CSS properties—except
opacity
andtransform
. In other words, applying transitions to any CSS box model property is inherently a CPU-intensive task. Therefore, animate onlyopacity
, andtransform
properties if you are concerned about your page’s performance. - Be mindful of the layout repainting issues that CSS transitions may cause through your elements’ stacking order.
CSS Transitions vs. CSS Animations: What’s the Difference?
Below are five key differences between CSS transitions and CSS animations.
1. What is the purpose of CSS transitions and animations?
CSS transitions create smooth transitions from one CSS value to another.
CSS animations animate the style change from one CSS keyframe to another.
2. Do CSS transitions and animations need triggers?
You need triggers to run CSS transitions.
For instance, you can use the :hover
pseudo-class to run transitions when a user’s pointer hovers over an element.
Alternatively, you can trigger transition effects using JavaScript to add or remove a class.
On the other hand, CSS animations do not need triggers. CSS animations run automatically by default. You can optionally use triggers to make it run only when an element is in a specific state.
3. How many states do CSS transitions and animations have?
CSS transitions have only two states: an initial and a final state. You cannot create intermediate steps.
CSS animations allow you to create multiple states.
4. Can CSS transitions and animations run multiple times?
CSS transitions run only once. But you can run multiple CSS animations iterations—even to infinity.
5. What is the recommended use of CSS transitions and animations?
CSS transitions are best used for basic style changes, while CSS animations are suitable for dynamic style changes.