Animations & Transitions
Animations and transitions add life and interactivity to your web designs.
Transforms
Transform elements without affecting layout.
Translate
Move elements around
.box {
transform: translateX(50px); /* Move right 50px */
transform: translateY(30px); /* Move down 30px */
transform: translate(50px, 30px); /* Move right 50px, down 30px */
}
Scale
Change size
.box {
transform: scaleX(1.5); /* 150% width */
transform: scaleY(0.8); /* 80% height */
transform: scale(1.2); /* 120% both dimensions */
}
.box:hover {
transform: scale(1.1); /* Slightly larger on hover */
}
Rotate
Spin elements
.box {
transform: rotate(45deg); /* Rotate 45 degrees */
transform: rotateX(45deg); /* Rotate on X axis */
transform: rotateY(45deg); /* Rotate on Y axis */
transform: rotateZ(45deg); /* Same as rotate() */
}
Skew
Slant elements
.box {
transform: skewX(10deg); /* Skew horizontally */
transform: skewY(10deg); /* Skew vertically */
transform: skew(10deg, 5deg); /* Both axes */
}
Multiple Transforms
.box {
transform: translate(50px, 30px) scale(1.2) rotate(15deg);
/* Transforms are applied in order */
}
Transform Origin
Control point around which transforms happen
.box {
transform-origin: center; /* Default */
transform-origin: top left;
transform-origin: 50% 50%;
transform-origin: right bottom;
transform: rotate(45deg);
/* Rotation happens around the origin point */
}
Transitions
Smooth changes between CSS property values
Basic Transition
.box {
background-color: blue;
width: 100px;
transition: all 0.3s ease;
}
.box:hover {
background-color: red;
width: 200px;
/* Change happens smoothly over 0.3 seconds */
}
Transition Properties
.box {
/* Specify which properties to transition */
transition-property: background-color, width;
/* How long the transition takes */
transition-duration: 0.3s;
/* Timing function */
transition-timing-function: ease;
/* Delay before transition starts */
transition-delay: 0.1s;
}
Transition Shorthand
.box {
transition: property duration timing-function delay;
transition: all 0.3s ease 0s;
}
/* Multiple transitions */
.box {
transition:
background-color 0.3s ease,
width 0.5s ease,
transform 0.2s ease;
}
Timing Functions
transition-timing-function: linear; /* Constant speed */
transition-timing-function: ease; /* Slow start, fast middle, slow end */
transition-timing-function: ease-in; /* Slow start */
transition-timing-function: ease-out; /* Slow end */
transition-timing-function: ease-in-out; /* Slow start and end */
transition-timing-function: cubic-bezier(0.17, 0.67, 0.83, 0.67);
Real-World Transition Example
<style>
.button {
padding: 12px 24px;
background-color: #0066cc;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-size: 16px;
font-weight: bold;
transition:
background-color 0.3s ease,
transform 0.2s ease,
box-shadow 0.3s ease;
}
.button:hover {
background-color: #0052a3;
transform: translateY(-2px);
box-shadow: 0 8px 16px rgba(0, 0, 0, 0.2);
}
.button:active {
transform: translateY(0);
}
</style>
<button class="button">Click Me</button>
Animations
Create complex, repeating movements
@keyframes Rule
Define animation frames
@keyframes slideIn {
0% {
opacity: 0;
transform: translateX(-100px);
}
50% {
opacity: 0.5;
}
100% {
opacity: 1;
transform: translateX(0);
}
}
.box {
animation: slideIn 0.5s ease-out;
}
Animation Properties
.box {
/* Name of animation */
animation-name: slideIn;
/* Duration */
animation-duration: 0.5s;
/* Timing function */
animation-timing-function: ease-out;
/* Delay before starting */
animation-delay: 0.2s;
/* Number of times to run */
animation-iteration-count: 1; /* once (default) */
animation-iteration-count: infinite; /* forever */
animation-iteration-count: 3; /* 3 times */
/* Direction */
animation-direction: normal; /* 0% to 100% */
animation-direction: reverse; /* 100% to 0% */
animation-direction: alternate; /* forward, backward, forward... */
animation-direction: alternate-reverse;
/* State when finished */
animation-fill-mode: forwards; /* Stay at 100% frame */
animation-fill-mode: backwards; /* Go back to 0% frame */
animation-fill-mode: both; /* Apply both */
}
Animation Shorthand
.box {
animation: name duration timing-function delay iteration-count direction fill-mode;
animation: slideIn 0.5s ease-out 0.2s 1 normal forwards;
}
Common Animation Examples
Fade In
@keyframes fadeIn {
from {
opacity: 0;
}
to {
opacity: 1;
}
}
.element {
animation: fadeIn 0.5s ease-out;
}
Bounce
@keyframes bounce {
0%, 100% {
transform: translateY(0);
}
50% {
transform: translateY(-20px);
}
}
.element {
animation: bounce 1s ease-in-out infinite;
}
Spin
@keyframes spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
.spinner {
animation: spin 2s linear infinite;
}
Pulse
@keyframes pulse {
0%, 100% {
opacity: 1;
}
50% {
opacity: 0.5;
}
}
.pulse {
animation: pulse 2s ease-in-out infinite;
}
Slide Up
@keyframes slideUp {
from {
opacity: 0;
transform: translateY(30px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
.element {
animation: slideUp 0.5s ease-out;
}
Real-World Animation Examples
Loading Spinner
<style>
.spinner {
width: 50px;
height: 50px;
border: 4px solid #f3f3f3;
border-top: 4px solid #0066cc;
border-radius: 50%;
animation: spin 1s linear infinite;
}
@keyframes spin {
0% { transform: rotate(0deg); }
100% { transform: rotate(360deg); }
}
</style>
<div class="spinner"></div>
Animated Button
<style>
.fancy-button {
padding: 12px 30px;
background: linear-gradient(90deg, #0066cc, #0052a3);
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
font-weight: bold;
position: relative;
overflow: hidden;
}
.fancy-button::before {
content: '';
position: absolute;
top: 0;
left: -100%;
width: 100%;
height: 100%;
background: rgba(255, 255, 255, 0.3);
transition: left 0.3s ease;
}
.fancy-button:hover::before {
left: 100%;
}
</style>
<button class="fancy-button">Hover Me</button>
Card Entrance Animation
<style>
.card {
background: white;
border-radius: 8px;
padding: 20px;
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
animation: slideUp 0.5s ease-out;
}
.card:nth-child(2) {
animation-delay: 0.1s;
}
.card:nth-child(3) {
animation-delay: 0.2s;
}
@keyframes slideUp {
from {
opacity: 0;
transform: translateY(30px);
}
to {
opacity: 1;
transform: translateY(0);
}
}
</style>
<div class="card">Card 1</div>
<div class="card">Card 2</div>
<div class="card">Card 3</div>
Animated Form Input
<style>
.input-group {
position: relative;
margin: 20px 0;
}
.input-group input {
width: 100%;
padding: 12px;
border: 2px solid #ddd;
border-radius: 4px;
font-size: 16px;
transition: border-color 0.3s ease;
}
.input-group input:focus {
outline: none;
border-color: #0066cc;
box-shadow: 0 0 0 3px rgba(0, 102, 204, 0.1);
}
.input-group label {
position: absolute;
top: 12px;
left: 12px;
background: white;
padding: 0 4px;
transition: all 0.3s ease;
}
.input-group input:focus + label,
.input-group input:not(:placeholder-shown) + label {
top: -10px;
font-size: 12px;
color: #0066cc;
}
</style>
<div class="input-group">
<input type="text" placeholder=" ">
<label>Email</label>
</div>
Performance Tips
-
Use GPU-accelerated properties
/* Good - GPU accelerated */
transform: translate(10px, 10px);
opacity: 0.5;
/* Bad - CPU intensive */
left: 10px;
top: 10px;
background-color: red; -
Reduce animation complexity
/* Good - simple animation */
animation: fadeIn 0.3s ease;
/* Bad - too many properties */
animation: complex 0.3s ease; -
Use
will-changefor optimization.element {
will-change: transform, opacity;
animation: slideIn 0.5s ease;
} -
Respect user preferences
@media (prefers-reduced-motion: reduce) {
* {
animation-duration: 0.01ms !important;
animation-iteration-count: 1 !important;
transition-duration: 0.01ms !important;
}
}
Best Practices
- Keep animations short - 300-500ms for interactions, up to 2s for attention
- Use easing functions - Never use linear for everything
- Animate intentionally - Don't animate just because you can
- Consider performance - Test on slower devices
- Respect user preferences - Honor prefers-reduced-motion
- Test across browsers - Ensure consistent behavior
- Use appropriate tools - CSS for simple animations, JavaScript for complex ones
Next Steps
Master animations and transitions, then learn:
- Advanced Techniques - Advanced effects
- Real-world projects - Apply everything