Transitions allow CSS property changes to occur gradually over time instead of happening instantly. This makes UI interactions feel smoother and more responsive.
transition-property: the CSS property to animatetransition-duration: time the animation laststransition-timing-function: acceleration style controlling the speed curve of the animation.ease — starts slow, speeds up, then slows again (default)linear — constant speed from start to finishease-in — slow start, speeds up at the endease-out — fast start, slows down at the endease-in-out — slow start and end, faster in the middlecubic-bezier(n,n,n,n) — custom curve definitionsteps(n, start|end) — creates a stepped transitiontransition-delay: how long to wait before startingtransition: background-color 0.3s ease-in 0.1s;This is a concise way to apply multiple transition settings at once.
Background Color Transition:
.transition-box {
width: 200px;
height: 100px;
background-color: lightblue;
transition: background-color 0.5s ease;
}
.transition-box:hover {
background-color: lightcoral;
}Width Transition:
.transition-width {
width: 100px;
height: 50px;
background-color: lightgreen;
transition: width 0.5s ease-in-out;
}
.transition-width:hover {
width: 200px;
}Button Transition:
.transition-button {
background-color: #4CAF50;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
transition: background-color 0.3s ease, transform 0.3s ease;
}
.transition-button:hover {
background-color: #45a049;
transform: scale(1.05);
}height: autotransform and opacity for better performanceCreate a button that smoothly changes its color and size when hovered using transition.
<button class="smooth-button">Hover Me</button>
<style>
.smooth-button {
background-color: #4CAF50; /* default green */
color: white;
padding: 12px 24px;
border: none;
border-radius: 6px;
font-size: 16px;
cursor: pointer;
transition: background-color 0.4s ease, transform 0.4s ease;
}
.smooth-button:hover {
background-color: #45a049; /* darker green */
transform: scale(1.1); /* slightly bigger */
}
</style>
You can copy and paste this code in your IDE to run!
Ask the AI if you need help understanding or want to dive deeper in any topic