CSS transition
Created By: chatGPT
CSS transitions allow you to change property values smoothly (over a given duration) from one state to another. They are often triggered by hovering or focusing on an element. The CSS properties involved can include colors, sizes, and positions, among others.
.box {
width: 100px;
height: 100px;
background-color: blue;
transition: background-color 0.5s ease;
}
.box:hover {
background-color: green;
}
In the example above, the
.box
class creates a square that changes color from blue to green when you hover over it. The transition effect takes 0.5 seconds and has an ease timing function..fade {
opacity: 1;
transition: opacity 0.5s;
}
.fade:hover {
opacity: 0;
}
Here, we have a class
.fade
that smoothly changes its opacity from 1 (fully visible) to 0 (invisible) when hovered. This can create a fading effect for images or text..scale {
transform: scale(1);
transition: transform 0.3s;
}
.scale:hover {
transform: scale(1.1);
}
The
.scale
class enlarges the element slightly when it’s hovered over. By setting the transform property and applying a transition, you can achieve a smooth zoom effect..position {
position: relative;
transition: top 0.5s, left 0.5s;
}
.position:hover {
top: 10px;
left: 10px;
}
In this example, the
.position
class demonstrates how to transition the position of an element. Upon hover, the element moves 10 pixels down and to the right, creating a smooth transition effect.:root {
--main-color: blue;
}
.color {
background-color: var(--main-color);
transition: background-color 0.4s;
}
.color:hover {
background-color: red;
}
Using CSS variables can make it easier to control the theme colors of your transitions. The
.color
class transitions between blue and red when hovered. This way, you only need to change the variable for different effects..all-in-one {
width: 100px;
height: 100px;
background: green;
border-radius: 15%;
transition: width 0.5s, height 0.5s;
}
.all-in-one:hover {
width: 150px;
height: 150px;
}