In this lecture, we’ll explore how to bring your web pages to life using CSS transitions and animations. These techniques allow you to create smooth, engaging effects that enhance user experience without requiring JavaScript.
CSS transitions enable you to smoothly change property values over a specified duration. They are ideal for creating hover effects, button animations, and other interactive elements.
To create a transition, you need to specify the CSS property to transition, the duration, and optional easing functions.
.button { background-color: #4CAF50; transition: background-color 0.3s ease; } .button:hover { background-color: #45a049; }
In this example, the background color of the button changes smoothly over 0.3 seconds when hovered.
You can transition multiple properties at once by separating them with commas.
.box { width: 100px; height: 100px; background-color: #333; transition: width 0.5s, height 0.5s, background-color 0.5s; } .box:hover { width: 150px; height: 150px; background-color: #555; }
This example smoothly changes the width, height, and background color of the box on hover.
Easing functions control the speed of the transition at different points, creating effects like easing in, easing out, or both.
CSS animations allow you to create more complex sequences of changes over time, beyond simple transitions. You can animate multiple properties, control the timing, and create keyframes for greater control.
To create an animation, define keyframes and apply them to an element using the animation property.
@keyframes example { 0% {background-color: red; left: 0px;} 50% {background-color: yellow; left: 100px;} 100% {background-color: green; left: 0px;} } .animate-box { position: relative; width: 100px; height: 100px; background-color: red; animation: example 5s infinite; }
In this example:
You can control the timing, delay, and iteration count of an animation.
.animate-box { animation: example 5s ease-in-out 1s 3 alternate; }
You can use transitions and animations together to create more dynamic effects.
.button { background-color: #4CAF50; transition: transform 0.3s ease; } .button:hover { transform: scale(1.1); } @keyframes pulse { 0% {transform: scale(1);} 50% {transform: scale(1.2);} 100% {transform: scale(1);} } .pulse-button { animation: pulse 1s infinite; }
This example:
Let’s combine transitions and animations to create a responsive, interactive button.
HTML:
CSS:
.animated-button { padding: 15px 30px; font-size: 16px; color: white; background-color: #008CBA; border: none; border-radius: 5px; cursor: pointer; transition: background-color 0.3s ease, transform 0.3s ease; } .animated-button:hover { background-color: #005f73; transform: translateY(-5px); } @keyframes shake { 0% { transform: translateX(0); } 25% { transform: translateX(-5px); } 50% { transform: translateX(5px); } 75% { transform: translateX(-5px); } 100% { transform: translateX(0); } } .animated-button:active { animation: shake 0.5s; }
In this example:
Next Up: In the next lecture, we’ll explore CSS Flexbox Deep Dive, where you’ll learn how to fully utilize Flexbox to create advanced, responsive layouts. Stay tuned!
Ridoy Hasan
Disclaimer: All resources provided are partly from the Internet. If there is any infringement of your copyright or other rights and interests, please explain the detailed reasons and provide proof of copyright or rights and interests and then send it to the email: [email protected] We will handle it for you as soon as possible.
Copyright© 2022 湘ICP备2022001581号-3