CSS transitions allow you to change property values smoothly (over a given duration). They make animations simple and are widely used to create visual effects that improve the user experience.
Basic Syntax
The basic syntax for a CSS transition is:
element {
transition: property duration timing-function delay;
}
Here's what each part means:
- property: The CSS property you want to animate.
- duration: How long the transition should take (e.g., 2s for 2 seconds).
- timing-function: The speed curve of the transition (e.g., ease, linear, ease-in, ease-out).
- delay: How long to wait before the transition starts (optional).
Simple Example
Let's start with a simple example where we change the background color of a div when we hover over it:
<!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 100px;
height: 100px;
background-color: blue;
transition: background-color 2s;
}
.box:hover {
background-color: red;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
In this example:
- The
.box
class has abackground-color
of blue. - When the mouse hovers over the
div
(with classbox
), the background color changes to red. - The transition takes 2 seconds to complete.
Transitioning Multiple Properties
You can transition multiple properties by listing them separated by commas:
element {
transition: width 2s, height 2s, background-color 2s;
}
Example with Multiple Properties
Here's an example where we change the width, height, and background color of a div:
<!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 100px;
height: 100px;
background-color: blue;
transition: width 2s, height 2s, background-color 2s;
}
.box:hover {
width: 200px;
height: 200px;
background-color: red;
}
</style>
</head>
<body>
<div class="box"></div>
</body>
</html>
In this example:
- When the mouse hovers over the
div
(with classbox
), the width and height double, and the background color changes to red. - Each transition takes 2 seconds to complete.
Using Timing Functions
Timing functions define the speed curve of the transition. Here are some common values:
ease
: Starts slow, speeds up, and then slows down (default).linear
: Constant speed.ease-in
: Starts slow and then speeds up.ease-out
: Starts fast and then slows down.ease-in-out
: Starts slow, speeds up, then slows down.
Example with Timing Function
Here's an example where we use different timing functions:
<!DOCTYPE html>
<html>
<head>
<style>
.box {
width: 100px;
height: 100px;
background-color: blue;
margin-bottom: 20px;
}
.ease {
transition: width 2s ease;
}
.linear {
transition: width 2s linear;
}
.ease-in {
transition: width 2s ease-in;
}
.ease-out {
transition: width 2s ease-out;
}
.box:hover {
width: 200px;
}
</style>
</head>
<body>
<div class="box ease">Ease</div>
<div class="box linear">Linear</div>
<div class="box ease-in">Ease-In</div>
<div class="box ease-out">Ease-Out</div>
</body>
</html>
In this example, each box uses a different timing function for the width transition when hovered.
Conclusion
CSS transitions make it easy to create smooth animations. By understanding the basic syntax and experimenting with different properties and timing functions, you can create engaging visual effects for your web pages.