Section 26: CSS Transforms
CSS transforms visually modify an element using translate(), rotate(), scale(), and skew(). The transform property does not change the space reserved for the element in normal document flow.
1. Translate
translate() moves an element horizontally and vertically.
.move-card { transform: translate(20px, 10px); }
.shift-up { transform: translateY(-8px); }2. Rotate
.rotate-card { transform: rotate(8deg); }Positive angles rotate clockwise in the usual CSS coordinate system.
3. Scale
.grow-card { transform: scale(1.1); }
.wide-card { transform: scaleX(1.2); }A scale value of 1 means unchanged; 1.1 means 110% of the original size.
4. Skew
.skew-card { transform: skewX(10deg); }5. Transform origin
.door { transform-origin: left center; transform: rotate(10deg); }transform-origin changes the point around which an element transforms.
Runnable example
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CSS Transforms</title>
<style>
body { font-family: Arial, sans-serif; padding: 30px; }
.card { width: 180px; padding: 24px; margin: 30px; background: #dbeafe; border-radius: 12px; transition: transform .25s ease; }
.card:hover { transform: translateY(-8px) scale(1.04) rotate(1deg); }
.pivot { transform-origin: left center; transform: rotate(4deg); }
</style>
</head>
<body>
<div class="card">Hover over this card</div>
<div class="card pivot">Custom transform origin</div>
</body>
</html>Important notes
- Transforms do not move neighboring elements out of the way.
- Multiple transform functions are applied in order, and order matters.
- Transforms can create a stacking context and affect how positioned descendants are laid out.
- Do not use transforms as a substitute for normal layout tools such as Flexbox or Grid.
Practice exercise
Create three cards: one moves upward, one rotates, and one grows on hover. Add a transition so each change looks smooth.
Knowledge check: 10 MCQs
- Which property applies a visual transformation? Answer: transform. Explanation: It accepts functions such as translate(), rotate(), scale(), and skew().
- What does translateY(-10px) do? Answer: Moves the element visually upward by 10px. Explanation: Negative Y moves upward in the usual CSS coordinate system.
- What does scale(1.2) mean? Answer: Scales to 120%. Explanation: A scale factor of 1.2 increases the dimensions by 20%.
- What does rotate(90deg) do? Answer: Rotates the element by 90 degrees. Explanation: The angle defines the amount of rotation.
- Which property sets the transformation pivot? Answer: transform-origin. Explanation: It specifies the origin point for transforms.
- Does transform normally reserve new layout space for the transformed shape? Answer: No. Explanation: The original layout position remains reserved.
- Which function slants an element? Answer: skew(). Explanation: Skew functions distort the element along an axis.
- Does transform order matter? Answer: Yes. Explanation: Combining translation, rotation, and scale in different orders can produce different results.
- Which tool is generally best for arranging a row of cards? Answer: Flexbox or Grid. Explanation: These are layout systems; transforms are primarily visual effects.
- Can a transform create a stacking context? Answer: Yes, when transform is not none. Explanation: This can influence z-index behavior.