Lesson 15 / Motion

CSS Transforms

Time
41 min
Type
Reading + Practice
Level
Intermediate

Use CSS transforms and filters to create dynamic visual effects without disrupting page layout.

Teacher Notes / In-Class Use

Demo Live

  • Model the core workflow from the lesson using a small class example.
  • Connect the example back to the first goal: Explain how transforms change the visual rendering of an element

Try In Class

  • Move an element with translate.
  • 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 transforms change the visual rendering of an element
  • Use translate, rotate, scale, skew, matrix, and 3D transforms
  • Apply filters with accessibility and performance in mind

Introduction

CSS Transforms offer a powerful way to manipulate the appearance and positioning of elements on a web page without affecting their layout.

By applying transformations, you can create dynamic and engaging effects that enhance the user experience.

Understanding CSS Transforms

The transform property is used to apply transformations to elements.

Transform functions manipulate how an element is rendered visually.

  • translate: Moves an element to a new position relative to its current location.
  • rotate: Rotates an element around a specified point.
  • scale: Resizes an element to a new width and height.
  • skew: Skews an element along the X or Y axis.
  • matrix: Applies a 2D transformation matrix to an element.

Applying Transforms to Elements

To apply a transform to an element, use the transform property and specify the desired transform function and its parameters.

.element {
  transform: translate(50px, 100px) rotate(45deg) scale(2);
}

Hardware Acceleration

Using translate3d(x, y, 0) can trigger GPU compositing in many browsers, which may improve performance for animations and transitions.

Use this carefully. Hardware acceleration is useful, but it is not a magic fix for every animation problem.

.element {
  transform: translate3d(50px, 100px, 0);
}

Moving Elements

You can use translate to move elements in pixels or percentages relative to their current position.

The first value moves the element on the X axis. The second value moves it on the Y axis.

.element {
  transform: translate(50px, 100px);
}

.element-percent {
  transform: translate(50%, 20%);
}

Rotating Elements

Use rotate to rotate an element clockwise or counterclockwise. This is especially useful for creating interactive effects or rotating icons.

The transform-origin property changes the point an element rotates around.

.element {
  transform: rotate(45deg);
}

.corner-rotate {
  transform-origin: top left;
  transform: rotate(45deg) translate(20px, 20px);
}

Scaling Elements

Scaling allows you to resize elements proportionally or along specific axes. This is useful for responsive design and hover effects.

.element {
  transform: scale(1.2);
}

.wide-short {
  transform: scale(1.5, 0.8);
}

Skewing Elements

Skew transformations tilt elements along the X or Y axis, adding a dynamic visual twist.

.element {
  transform: skew(15deg, 5deg);
}

3D Transforms

3D transforms let you manipulate elements in three-dimensional space. Combine perspective, rotation, and positioning to create immersive interfaces.

.scene {
  perspective: 500px;
}

.element {
  transform-style: preserve-3d;
  transform: rotateY(30deg);
}

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;
  transition: transform 400ms ease;
}

.card:hover {
  transform: rotateY(180deg);
}

.card-face {
  backface-visibility: hidden;
}

.card-back {
  transform: rotateY(180deg);
}

3D Cube

A 3D cube is built by positioning six faces in 3D space and rotating the parent container.

.cube {
  position: relative;
  width: 150px;
  height: 150px;
  transform-style: preserve-3d;
  transform: rotateX(-20deg) rotateY(35deg);
}

.cube-face {
  position: absolute;
  inset: 0;
}

.cube-front {
  transform: translateZ(75px);
}

.cube-back {
  transform: rotateY(180deg) translateZ(75px);
}

CSS Filters

CSS filters apply visual effects to elements. These can include blur, grayscale, contrast adjustments, and more.

Filters can enhance visual design and interaction, but they should not make content harder to read.

.image {
  filter: blur(5px) grayscale(50%);
}

Accessibility and Performance

  • Ensure color contrast is maintained when applying filters.
  • Allow users to disable motion-heavy effects when needed.
  • Use translate3d carefully for better performance.
  • Avoid animating layout-affecting properties such as width, height, top, left, and margin.
  • Prefer transform and opacity for smoother interface motion.
.transition-example {
  transition: transform 200ms ease, opacity 200ms ease;
}

@media (prefers-reduced-motion: reduce) {
  .transition-example {
    transition: none;
  }
}

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 an element with translate.
  • Rotate an icon or small graphic.
  • Create a hover state that uses scale and transition.
  • Build a simple card flip.
  • Add a reduced-motion fallback for animated transform effects.