The CSS transition-property
is a fundamental aspect of creating smooth animations. It specifies which CSS property or properties will be animated when their values change, such as on a hover effect. By default, its value is all
, meaning any animatable property will transition; however, you can list specific properties to optimize performance and control the effect.
Example 1: Transitioning a Single Property
/* This class defines the initial state of our element. */
.box {
width: 150px;
height: 150px;
background-color: #3498db;
transition-duration: 0.5s;
/* Only the 'width' property will animate over the transition duration. */
transition-property: width;
}
/* On hover, the width changes, triggering the specified transition. */
.box:hover {
width: 300px;
background-color: #e74c3c; /* This change happens instantly. */
}
Explanation This code sets up a transition exclusively for the width
property. When you hover over the .box
element, only its width will animate smoothly to 300px, while the background color changes instantly because it is not included in transition-property
.
Example 2: Transitioning Multiple Properties
/* This class sets up the initial state. */
.button {
padding: 15px 30px;
background-color: #2ecc71;
color: white;
border: none;
transition-duration: 0.4s;
/* We specify two properties to animate: background-color and color. */
transition-property: background-color, color;
}
/* On hover, both specified properties will animate smoothly. */
.button:hover {
background-color: #27ae60;
color: #f1c40f;
}
Explanation Here, transition-property
is given a comma-separated list of properties: background-color
and color
. This tells the browser to animate both the button's background and text color simultaneously when the user hovers over it.
Example 3: Using the 'all' Keyword
/* Initial state of the element. */
.card {
width: 200px;
padding: 20px;
background-color: white;
box-shadow: 0 4px 8px rgba(0,0,0,0.1);
transform: scale(1);
transition-duration: 0.3s;
/* 'all' is the default, animating any animatable property that changes. */
transition-property: all;
}
/* On hover, all changing properties will animate. */
.card:hover {
transform: scale(1.05);
box-shadow: 0 8px 16px rgba(0,0,0,0.2);
}
Explanation The all
keyword is a shorthand to animate every property that can be transitioned. In this example, both the transform
(scaling the card up) and the box-shadow
will animate smoothly on hover, creating a fluid lifting effect.
Example 4: Using the 'none' Keyword
/* Initial styling for the input field. */
.input-field {
border: 2px solid #ccc;
padding: 10px;
transition-duration: 0.5s; /* Duration is set, but... */
/* 'none' explicitly prevents any property from animating. */
transition-property: none;
}
/* On focus, the changes are applied instantly without animation. */
.input-field:focus {
border-color: #3498db;
box-shadow: 0 0 5px rgba(52, 152, 219, 0.5);
}
Explanation By setting transition-property
to none
, we disable all transitions for the element. Even though a transition-duration
is defined, the border-color
and box-shadow
will change instantly when the input field receives focus.
Example 5: Transitioning the 'transform' Property
/* Initial state of the icon. */
.icon {
font-size: 40px;
color: #9b59b6;
transition-duration: 0.4s;
/* We only want the transform (rotation) to be animated. */
transition-property: transform;
}
/* On hover, the icon rotates smoothly. */
.icon:hover {
transform: rotate(360deg);
}
Explanation This example isolates the transform
property for animation. When hovering over the icon, it will complete a full 360-degree rotation smoothly, demonstrating how to animate movement or orientation changes specifically.
Example 6: Animating Opacity and Visibility
/* Initially, the tooltip is hidden. */
.tooltip {
opacity: 0;
visibility: hidden;
background-color: #333;
color: white;
padding: 5px;
border-radius: 4px;
transition-duration: 0.3s;
/* We specify 'opacity' for a fade effect. */
transition-property: opacity;
}
/* The parent container for the tooltip. */
.tooltip-container:hover .tooltip {
opacity: 1;
visibility: visible;
}
Explanation This code creates a fade-in effect for a tooltip by transitioning the opacity
property. While visibility
changes instantly, the smooth change in opacity from 0 to 1 makes the tooltip appear gradually on hover.
Example 7: Animating Font Size
/* Initial style for the link. */
.nav-link {
color: #16a085;
font-size: 18px;
text-decoration: none;
transition-duration: 0.25s;
/* Animate only the font-size property. */
transition-property: font-size;
}
/* Make the text larger on hover. */
.nav-link:hover {
font-size: 22px;
color: #1abc9c; /* This color change will be instant. */
}
Explanation This example focuses the transition on the font-size
property. When a user hovers over the navigation link, the text will grow smoothly, providing clear visual feedback, while the color change is immediate.