4.1 The transition Property
CSS transitions let you animate changes to CSS property values, creating smooth transitions between their start and end states. It's a powerful tool that enhances usability by adding dynamics to web pages. Let's dive into the key aspects of transitions, including properties, duration, and timing functions.
The transition property combines all aspects of a transition into one property. It can include:
- Properties to be animated.
- Animation duration.
- Timing function.
- Delay before the animation starts (optional).
Syntax:
element {
transition: property duration timing-function delay;
}
Where:
property: the property to which the transition is applied (e.g.,width,height,background-color)duration: the transition duration (e.g.,2sfor two seconds or500msfor 500 milliseconds)-
timing-function: timing function that defines the speed changes during the animation (e.g.,ease,linear,ease-in,ease-out,ease-in-out) delay: delay before the transition starts (e.g.,1sfor one second)
Example:
Content
.box {
width: 100px;
height: 100px;
background-color: #3498db;
transition: width 2s ease-in-out, background-color 1s ease;
}
Code Explanation:
-
transition: width 2s ease-in-out,background-color 1s ease: defines two transitions: one for thewidthproperty with a duration of 2 seconds and a timing function ofease-in-out, and another for thebackground-colorproperty with a duration of 1 second and a timing function ofease
4.2 Transition Properties
1. The transition-property
The transition-property property defines which element properties will have transitions applied to them.
Syntax:
element {
transition-property: property1, property2 ...;
}
Example:
.box {
transition-property: width, height, background-color;
}
2. The transition-duration
The transition-duration sets the duration of the transition.
Syntax:
element {
transition-duration: time;
}
Example:
.box {
transition-duration: 2s, 1s, 3s;
}
3. The transition-timing-function
The transition-timing-function defines the speed curve of the animation over time.
Syntax:
element {
transition-timing-function: function;
}
Example:
.box {
transition-timing-function: ease-in, linear, ease-out;
}
4. The transition-delay
The transition-delay sets the delay before the transition starts.
Syntax:
element {
transition-delay: time;
}
Example:
.box {
transition-delay: 1s, 0s, 500ms;
}
4.3 Transition Duration
The transition duration determines how long the animation of the property change will last. It's specified in seconds (s) or milliseconds (ms).
Example:
.box {
width: 100px;
height: 100px;
background-color: #3498db;
transition: width 2s, height 1s, background-color 500ms;
}
Code Explanation:
-
transition: width 2s,height 1s,background-color 500ms: specifies that the transition for thewidthproperty will last 2 seconds, forheight— 1 second, and forbackground-color— 500 milliseconds.
4.4 Timing Functions
Timing functions define how the property value changes over time. They allow you to create various animation effects.
Main timing functions:
ease: animation starts slow, speeds up in the middle, and slows down at the endlinear: animation moves at a constant speed from start to finishease-in: animation starts slow and then speeds upease-out: animation starts fast and then slows downease-in-out: animation starts and ends slow, speeds up in the middle
Example:
.box {
width: 100px;
height: 100px;
background-color: #2ecc71;
transition: width 2s ease-in-out, height 1s linear, background-color 500ms ease;
}
Code Explanation:
ease-in-out: smooth transition, starting and ending slowlinear: transition with a constant speedease: transition with a slow start and end, speeding up in the middle
GO TO FULL VERSION