Lesson 15 / Motion
CSS Transforms and Filters
Use CSS transforms and filters to move, rotate, scale, tilt, and visually adjust elements without disrupting page layout.
Course Role
Core
Part of the main course path. Prioritize this before moving to optional polish or deeper references.
Teacher Notes / In-Class Use
Demo Live
- Walk through the interactive demo before students start changing their own project files.
- Connect the demo back to the first goal: Explain how `transform` changes visual rendering without moving surrounding layout
Try In Class
- Move a button visually with `translateY` and compare it to `position: relative`.
- Have students make one visible change, save, refresh, and explain what changed.
Submit Or Check
- Ask students to show the work in the browser, not only in the editor.
- Have students commit their progress with a clear message when the checkpoint is stable.
Watch For
- Students copying code without checking file paths, spelling, or capitalization.
- Visual changes that work locally but break when the project is published.
Learning Goals
- Explain how `transform` changes visual rendering without moving surrounding layout
- Use `translate`, `rotate`, `scale`, `skew`, `transform-origin`, and 3D transforms intentionally
- Apply `filter` effects with readability, accessibility, and performance in mind
Interactive Demo
How to use this demo.
Use the demo as a small lab. Change one thing, observe the result, then connect it back to your own project.
What To Try
- Adjust translate, rotate, scale, and transform-origin.
- Compare the transformed element with its original layout position.
What Changes
- The visual position changes without changing the element position in normal flow.
- The generated CSS combines multiple transform functions in one property.
What To Notice
- Transforms are visual changes, not layout recalculations.
- Transform origin changes the pivot point for rotation and scaling.
Apply It
- Add a small transform to one hover or focus state in your project.
Interactive Demo
CSS Transform Studio
Adjust transform functions and origin without changing normal document flow.
This demo uses extra JavaScript for teaching. The code sample shows the pattern to practice. View full demo source.
Introduction
transform changes how an element is visually rendered. It can move, rotate, scale, or tilt an element without changing the space that element occupies in the normal page layout.
filter changes how an element looks, such as making an image blurry, grayscale, brighter, darker, or higher contrast.
Lesson 16 focuses on transition. This lesson focuses on what transforms and filters do.
Transform vs Position
position changes how an element is placed in relation to the document, a parent, or the viewport.
transform changes the visual rendering of an element after layout has already happened. Other elements still behave as if the transformed element is in its original spot.
| Need | Better Tool | Why |
|---|---|---|
| Place a badge in a card corner | position | The badge needs to attach to a parent. |
| Move a button up slightly on hover | transform | The motion is visual and should not push other elements. |
| Build the main page structure | Normal flow, Flexbox, or Grid | Transforms should not be used for page layout. |
| Zoom an image on hover | transform | The image can visually grow without recalculating layout. |
Understanding CSS Transforms
The transform property accepts one or more transform functions. Each function changes the element visually in a specific way.
| Function | Use For | Example |
|---|---|---|
translate() | Move an element visually | translateY(-0.25rem) |
rotate() | Turn an element | rotate(6deg) |
scale() | Resize an element visually | scale(1.05) |
skew() | Slant an element | skewX(-8deg) |
transform-origin | Change the pivot point | transform-origin: left center |
perspective | Create 3D depth | perspective: 800px |
Applying Transforms
You can combine multiple transform functions in one transform declaration.
The browser applies transform functions in order, so changing the order can change the final result.
.element {
transform: translateX(2rem) rotate(6deg) scale(1.05);
} Transform Order Matters
transform: translateX(4rem) rotate(15deg) does not behave the same as transform: rotate(15deg) translateX(4rem).
When a transform looks strange, check the order of the functions before changing the values.
.first {
transform: translateX(4rem) rotate(15deg);
}
.second {
transform: rotate(15deg) translateX(4rem);
} Moving Elements with Translate
translate moves an element visually on the X axis, Y axis, or both. It is great for small interface movement because it does not push nearby content around.
.button:hover {
transform: translateY(-0.25rem);
}
.card-label {
transform: translate(1rem, -0.5rem);
} Rotating Elements
rotate turns an element clockwise or counterclockwise. It is useful for arrows, icons, labels, stickers, and playful interface details.
.arrow-icon {
transform: rotate(90deg);
}
.tilted-label {
transform: rotate(-4deg);
} Scaling Elements
scale visually resizes an element. Values above 1 make it larger, and values below 1 make it smaller.
Large scale values can cause overflow, so test the surrounding layout when an element grows.
.image-card:hover img {
transform: scale(1.06);
}
.small-badge {
transform: scale(0.9);
} Skewing Elements
skew slants an element on the X axis, Y axis, or both. Use it sparingly because it can make text harder to read.
.accent-panel {
transform: skewX(-8deg);
} Transform Origin
transform-origin changes the point an element transforms around. The default is the center of the element.
This is useful when an arrow should rotate from its center, a door should swing from one edge, or a menu should grow from the top.
.door {
transform-origin: left center;
transform: rotateY(-20deg);
}
.dropdown-panel {
transform-origin: top center;
transform: scaleY(0.95);
} Practical Transform Patterns
| Pattern | Useful CSS |
|---|---|
| Button lift | transform: translateY(-0.25rem); |
| Image zoom | transform: scale(1.06); |
| Rotated arrow | transform: rotate(90deg); |
| Card tilt | transform: rotate(1deg) translateY(-0.25rem); |
| Reveal panel start position | transform: translateY(-0.5rem); |
3D Transforms
3D transforms add depth. Use perspective on the parent and 3D transform functions, such as rotateY, on the child.
Start simple. 3D effects can become confusing quickly because you are working with depth as well as horizontal and vertical movement.
.scene {
perspective: 800px;
}
.card {
transform-style: preserve-3d;
transform: rotateY(12deg);
} Demo: Card Flip
A card flip uses perspective on the parent, transform-style on the inner card, and rotateY to reveal the back side.
.card-scene {
perspective: 800px;
}
.card {
transform-style: preserve-3d;
transform: rotateY(180deg);
}
.card-face {
backface-visibility: hidden;
}
.card-back {
transform: rotateY(180deg);
} CSS Filters
filter applies visual effects to an element. Filters are especially common on images and decorative media.
Filters can improve mood and hierarchy, but they should not make content harder to read.
.image {
filter: grayscale(50%) contrast(110%);
} Common Filter Functions
| Filter | Use For | Caution |
|---|---|---|
blur() | Background depth or soft focus | Large blurs can be expensive and can reduce clarity. |
grayscale() | Muted or inactive imagery | Do not rely on grayscale alone to show state. |
brightness() | Lightening or darkening media | Check text contrast if text sits on top. |
contrast() | Making imagery punchier | Too much contrast can look harsh. |
drop-shadow() | Shadow around transparent shapes | Use box-shadow for normal rectangular boxes. |
Readable Image Treatment
A darkened image can make light text easier to read, but the final contrast still needs to be checked.
.hero-image {
filter: brightness(65%) contrast(105%);
} Performance and Motion Notes
- Prefer
transformandopacityfor visual movement because they usually avoid layout recalculation. - Avoid using transforms to fake an entire page layout. Use layout tools for layout.
- Avoid heavy
blur()on large areas unless the effect is really needed. - Keep motion subtle. Large movement and dramatic scaling can distract users.
- Respect
prefers-reduced-motionwhen transforms are used for animated effects.
@media (prefers-reduced-motion: reduce) {
.motion-effect {
transform: none;
}
} Common Mistakes
| Mistake | Why It Happens | Fix |
|---|---|---|
| Expecting transforms to move nearby layout | transform changes visual rendering, not document flow | Use margin, padding, Flexbox, Grid, or positioning when layout should change. |
Using huge scale() values | The element can overflow its container | Use smaller scale values and test at mobile sizes. |
| Forgetting transform order matters | Functions are applied in sequence | Reorder transform functions and compare the result. |
| Making hover effects too dramatic | Large movement can feel jumpy | Keep transforms subtle and purposeful. |
| Filtering text heavily | Effects like blur() or low contrast() hurt readability | Filter images and decorative media more often than body text. |
Checkpoint
Before moving on, make sure these feel true.
- I can explain the main concept in my own words.
- I can apply this lesson to my current project.
- I can verify the result in the browser.
- I can commit the change with a clear message.
Project Connection
This lesson supports current class projects.
Practice
- Move a button visually with
translateYand compare it toposition: relative. - Rotate an arrow icon with
rotate. - Zoom an image with
scalewhile keeping the card size stable. - Change
transform-originand observe how the same rotation behaves differently. - Create a simple 3D card tilt with
perspectiveandrotateY. - Apply a readable image treatment with
brightnessorcontrastand check text contrast. - Find one transform order example where changing the order changes the result.