CSS
Transitions and Transforms
Add smooth movement and shape changes to elements with CSS transitions and transforms.
The WanderLust team wants to make their destination cards more interactive. When visitors hover over a beautiful beach photo, they want it to smoothly grow larger and maybe rotate slightly. That magic happens with two CSS superpowers: transitions (which control timing) and transforms (which control shape and movement). Think of a transition like a slow-motion video — instead of an element instantly changing from small to big, it smoothly morphs over time. Transforms are like digital origami — you can move, rotate, scale, or skew elements without affecting the page layout around them.CSS Transform Basics
The `transform` property moves or reshapes elements in 2D or 3D space. Picture taking a photo and editing it — you can rotate it, make it bigger, or stretch it without changing the original file.Scale
Make bigger or smaller
Rotate
Spin clockwise or counter-clockwise
Translate
Move up, down, left, right
Skew
Tilt at an angle
/* Scale makes the destination card 20% larger on hover */
.destination-card:hover {
transform: scale(1.2); /* 1.0 = normal size, 1.2 = 20% bigger */
}
/* Rotate spins the card 5 degrees clockwise */
.destination-card:hover {
transform: rotate(5deg); /* positive = clockwise, negative = counter */
}What just happened?
The cards instantly snap to their new size and rotation when you hover. The transform moves the element visually but doesn't push other elements around. Try this: combine multiple transforms with spaces between them!
Combining Multiple Transforms
You can apply several transforms at once by listing them with spaces. The transforms execute from left to right, which can affect the final result./* Combine scale, rotate, and translate for WanderLust cards */
.destination-card:hover {
transform: scale(1.1) rotate(-2deg) translateY(-10px);
}
/* Each transform function does one thing:
scale(1.1) = 10% bigger
rotate(-2deg) = 2 degrees counter-clockwise
translateY(-10px) = move up 10 pixels */CSS Transitions — Adding Time
Transforms change how elements look, but transitions control how long those changes take. Without transitions, transforms happen instantly (like flipping a light switch). With transitions, changes happen smoothly over time (like a sunrise). The basic `transition` property has three main parts:/* Smooth transform changes for WanderLust destination cards */
.destination-card {
transition: transform 0.3s ease-out; /* property duration timing */
transform: scale(1) rotate(0deg); /* starting state */
}
.destination-card:hover {
transform: scale(1.1) rotate(-2deg) translateY(-10px); /* end state */
}
/* Now the transform changes smoothly over 0.3 seconds */What just happened?
The cards now smoothly animate to their hover state over 0.3 seconds using an ease-out curve (starts fast, slows down). When you move your mouse away, they smoothly return to normal. Try this: change the duration to 1s for a slower effect!
Timing Functions — Animation Curves
Timing functions control the speed curve of your transition. Think of a car accelerating — it might start slow and speed up, or start fast and slow down.ease-out
Starts fast, slows down — feels natural
ease-in
Starts slow, speeds up — dramatic
linear
Same speed throughout — robotic
ease-in-out
Slow start and end — smooth
/* Different timing functions for WanderLust buttons */
.btn-book {
transition: all 0.3s ease-out; /* natural feeling */
background: #0ea5e9;
transform: scale(1);
}
.btn-book:hover {
background: #f97316;
transform: scale(1.05); /* slightly bigger */
}
.btn-explore {
transition: all 0.5s ease-in-out; /* smooth and elegant */
}Transitioning Multiple Properties
You can transition any CSS property that has numeric values — colors, sizes, positions, opacity. The `all` keyword transitions every changing property, or you can list specific ones./* Transition multiple properties for WanderLust pricing cards */
.pricing-card {
background: white;
box-shadow: 0 2px 8px rgba(0,0,0,0.1);
border-radius: 12px;
transform: translateY(0);
opacity: 0.9;
/* Transition specific properties with different timings */
transition:
transform 0.3s ease-out,
box-shadow 0.3s ease-out,
opacity 0.2s ease-in;
}
.pricing-card:hover {
transform: translateY(-8px); /* lift up */
box-shadow: 0 12px 32px rgba(0,0,0,0.15); /* bigger shadow */
opacity: 1; /* fully visible */
}What just happened?
Three properties animate simultaneously: the card lifts up (transform), the shadow grows (box-shadow), and it becomes fully opaque (opacity). Each has its own timing. The effect creates depth and draws attention. Try this: use `transition: all 0.3s` for simpler code!
Transform Origin — Setting the Pivot Point
By default, transforms happen from the center of an element. The `transform-origin` property lets you change the pivot point — like moving the center of a spinning wheel./* WanderLust photo gallery with different rotation origins */
.photo-card {
transition: transform 0.4s ease-out;
transform-origin: bottom left; /* rotate from bottom-left corner */
}
.photo-card:hover {
transform: rotate(5deg) scale(1.05);
}
/* Other transform-origin options:
top left, top center, top right
center left, center, center right
bottom left, bottom center, bottom right */Advanced Transform Functions
Beyond the basic four transforms, CSS offers more specialized functions for complex effects. These are perfect for creative hover states and interactive elements./* Advanced transforms for WanderLust interactive elements */
.skew-card {
transition: transform 0.3s ease-out;
}
.skew-card:hover {
transform: skewX(-5deg) skewY(2deg); /* tilt horizontally and vertically */
}
.flip-card:hover {
transform: rotateY(180deg); /* flip like a coin */
}
.perspective-card {
transform: perspective(1000px) rotateX(15deg); /* 3D tilt effect */
}Performance Tips
Some transforms and transitions perform better than others. Browsers can optimize certain properties using the GPU (graphics card), making them silky smooth even on slower devices.GPU-Accelerated Properties: transform, opacity, filter. These animate smoothly because the browser can use hardware acceleration. Stick to these for the best performance.
Avoid Animating These:
width, height, top, left, margin, padding. These cause the browser to recalculate layout, which is slow. Use transform: scale() instead of width/height, and transform: translate() instead of top/left.
Quiz
1. The WanderLust team wants a destination card to become 20% larger and rotate 10 degrees clockwise on hover. Which CSS is correct?
2. What does the duration value in a CSS transition control?
3. Which CSS properties are best for smooth animations because they use GPU acceleration?
Up Next: Pseudo-Classes
Target elements based on their state or position with :hover, :focus, :nth-child and more powerful selectors.