Lesson 15 / Motion

CSS Transforms and Filters

Time
55 min
Type
Reading + Interactive
Level
Intermediate
Use
Core

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.

Normal position
Transformed

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.

NeedBetter ToolWhy
Place a badge in a card cornerpositionThe badge needs to attach to a parent.
Move a button up slightly on hovertransformThe motion is visual and should not push other elements.
Build the main page structureNormal flow, Flexbox, or GridTransforms should not be used for page layout.
Zoom an image on hovertransformThe 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.

FunctionUse ForExample
translate()Move an element visuallytranslateY(-0.25rem)
rotate()Turn an elementrotate(6deg)
scale()Resize an element visuallyscale(1.05)
skew()Slant an elementskewX(-8deg)
transform-originChange the pivot pointtransform-origin: left center
perspectiveCreate 3D depthperspective: 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

PatternUseful CSS
Button lifttransform: translateY(-0.25rem);
Image zoomtransform: scale(1.06);
Rotated arrowtransform: rotate(90deg);
Card tilttransform: rotate(1deg) translateY(-0.25rem);
Reveal panel start positiontransform: 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

FilterUse ForCaution
blur()Background depth or soft focusLarge blurs can be expensive and can reduce clarity.
grayscale()Muted or inactive imageryDo not rely on grayscale alone to show state.
brightness()Lightening or darkening mediaCheck text contrast if text sits on top.
contrast()Making imagery punchierToo much contrast can look harsh.
drop-shadow()Shadow around transparent shapesUse 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 transform and opacity for 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-motion when transforms are used for animated effects.
@media (prefers-reduced-motion: reduce) {
  .motion-effect {
    transform: none;
  }
}

Common Mistakes

MistakeWhy It HappensFix
Expecting transforms to move nearby layouttransform changes visual rendering, not document flowUse margin, padding, Flexbox, Grid, or positioning when layout should change.
Using huge scale() valuesThe element can overflow its containerUse smaller scale values and test at mobile sizes.
Forgetting transform order mattersFunctions are applied in sequenceReorder transform functions and compare the result.
Making hover effects too dramaticLarge movement can feel jumpyKeep transforms subtle and purposeful.
Filtering text heavilyEffects like blur() or low contrast() hurt readabilityFilter 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 translateY and compare it to position: relative.
  • Rotate an arrow icon with rotate.
  • Zoom an image with scale while keeping the card size stable.
  • Change transform-origin and observe how the same rotation behaves differently.
  • Create a simple 3D card tilt with perspective and rotateY.
  • Apply a readable image treatment with brightness or contrast and check text contrast.
  • Find one transform order example where changing the order changes the result.