CSS keyframes
Created By: chatGPT
CSS keyframes are essential for creating animations in web development. They allow you to define styles at various points during an animation sequence. By doing so, you can animate properties like color, position, and size over time. Here’s how to create a basic keyframe animation:
/* Define the keyframes */
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
/* Applying the animation to an element */
.box {
width: 100px;
height: 100px;
background-color: blue;
animation: fadeIn 2s ease-in-out;
}
In the example above, we created an animation called fadeIn that transitions the opacity of an element from 0 to 1 over a duration of 2 seconds. The ease-in-out timing function makes the transition smoother. You can also define multiple keyframes to create more complex animations:
@keyframes slideIn {
0% {
transform: translateX(-100%);
}
50% {
transform: translateX(50%);
}
100% {
transform: translateX(0);
}
}
.box {
animation: slideIn 3s ease;
}
This example demonstrates a slideIn animation that moves an element from the left side of the screen into its final position. The use of percentage values in the keyframes allows for precise control over how the animation progresses, making your animations much more dynamic and visually appealing.
/* Adding to the CSS to see the slide effect */
.box {
width: 100px;
height: 100px;
background-color: red;
animation: slideIn 3s ease;
}