Transforms and 3D

Intermediate · 4 min

How transform moves, rotates, scales and skews an element without touching layout, why function order matters, and how perspective makes a card 3D.

Before thisClip-path, Masks and Blend ModesPositioning and Sticky

Outcome Unclear · 4 min

Transforms and 3D

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Outcome UnclearReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `transform` changes how an element is painted, not where layout thinks it is. The element keeps its original space, and neighbors don't move.
  • Transform functions apply right to left: `translateX(100px) rotate(90deg)` rotates first and then moves along the page's x axis, while `rotate(90deg) translateX(100px)` moves along the rotated axis, which is now vertical.
  • `transform-origin` is the pivot (default: the center). Rotation and scale happen around it, so changing it changes the result.
  • The individual properties `translate`, `rotate` and `scale` do the same jobs one at a time, so a hover can change one without rewriting the whole `transform` value.
  • 3D needs `perspective` on a parent (or `perspective()` in the function list) to create depth, and `transform-style: preserve-3d` for children to keep their own 3D positions. `backface-visibility: hidden` hides the back of a flipped element.

Move the picture, not the layout

Normal CSS layout works out where every box goes. A transform runs after that. It’s more like picking up a finished printed card and turning it on the table than rearranging the page. The card’s original footprint stays reserved, nothing around it shifts, and only the painted pixels move. That’s why transforms are cheap and why they are the property of choice for animation, a topic the transitions lesson builds on.

The 2D functions

.card:hover {
  transform: translate(0, -4px) scale(1.03) rotate(-1deg);
}
  • translate(x, y) moves. Percentages refer to the element’s own size, so translate(-50%, -50%) re-centers something absolutely positioned at top: 50%; left: 50%, a pattern from positioning.
  • scale(n) resizes around the origin. scale(1.1) is 10% bigger.
  • rotate(angle) turns clockwise for positive angles, in deg, turn or rad.
  • skew(x, y) slants along each axis.

All of these are shorthand for a 2D matrix of six numbers. You will rarely write matrix() by hand.

Two limits apply. A transform has no effect on a non-replaced inline element such as a <span> in text, and rotating one in a Chromium test left its bounding box unchanged. Give it display: inline-block. A transform also creates a stacking context, and it becomes the containing block for position: fixed descendants.

Order matters

When you list several functions, they apply from right to left, each in the coordinate system left by the previous one:

.a { transform: translateX(100px) rotate(90deg); }  /* rotate in place, then slide right */
.b { transform: rotate(90deg) translateX(100px); }  /* turn the axes, then slide "right" = down */

In a browser, starting from the same 100 by 50 box, .a’s center ended 100px to the right of where it started, and .b’s center ended 100px below it. This is the usual source of “my transform goes the wrong way” bugs: after a rotate(), the x axis is no longer horizontal.

The pivot: transform-origin

Rotation and scale happen around a point. By default it is the center (50% 50%). Move it and the same angle looks different:

.door { transform-origin: left center; transform: rotate(-30deg); }  /* hinge on the left */
.menu { transform-origin: top; transform: scaleY(0); }               /* grows from the top */

Individual properties

The three most common functions also exist as their own properties. They were added so you can change one piece without rewriting the others:

.icon { translate: 0 0; rotate: 0deg; scale: 1; transition: rotate 0.2s, scale 0.2s; }
.icon:hover { rotate: 15deg; scale: 1.1; }

Each property can transition or animate on its own, which is awkward with a single transform string. They have been in every major browser since August 2022. The browser applies them in the fixed order translate, rotate, scale, then transform.

Adding depth: perspective and 3D

Rotate a flat box around the Y axis without perspective and it just looks narrower, like a door seen from straight on. To get depth, tell the browser how far away the viewer is: a smaller perspective is a closer eye and stronger distortion.

.scene { perspective: 800px; }                         /* on the parent */
.card  { transform: rotateY(40deg); }                  /* the child leans away */

The perspective property on a parent applies to its children. The perspective() function inside transform applies to that one element, so two siblings each get their own vanishing point. perspective-origin moves the viewer’s eye.

For nested 3D, such as a card with a front and a back face, the parent needs transform-style: preserve-3d. Without it, the browser flattens children onto the parent’s plane before drawing it. A flip card:

.flip       { perspective: 800px; }
.flip-inner { transform-style: preserve-3d; transition: transform 0.6s; }
.flip:hover .flip-inner { transform: rotateY(180deg); }
.face       { position: absolute; inset: 0; backface-visibility: hidden; }
.back       { transform: rotateY(180deg); }

backface-visibility: hidden stops a face drawing when it’s turned away. Without it the front and back would show through each other.

What the video covers

Outcome Unclear’s “Master CSS transforms and perspective - Explained Visually” runs just over four minutes and is from June 2023. Its description says it goes from the basics, starting with translateX and translateY, into the perspective property, using visual diagrams. I could not read a transcript, so I describe no more than that. This is the weakest pick in the group: a very small channel (about 1,400 subscribers) and a short runtime. It was chosen because it is the one video that spans 2D transforms through perspective. The two CSSnippets runner-ups are more recent and cover transform-origin, transform-box, transform-style and 2D function-combining. The ordering, individual-property and flip-card material above is the prose’s own.

Where this fits

Transforms move positioned elements and shapes you clipped in clip-path, masks and blend modes. The next group of lessons, starting with transitions, animates them.

Check yourself

  1. 01You rotate a card with `transform: rotate(5deg)`. Do its neighbors move to make room?

    No. Transforms are applied after layout, so the card's layout box is unchanged and neighbors stay put. The rotated card may overlap them. Use margin or a different layout approach if you need the space adjusted.

  2. 02What's the difference between `transform: translateX(100px) rotate(90deg)` and `rotate(90deg) translateX(100px)`?

    Functions apply right to left to the element's own coordinate system. In the first, the box rotates and then moves 100px to the right. In the second, the rotation happens first and moves the x axis with it, so `translateX(100px)` pushes the box down the page.

  3. 03You set `perspective: 600px` on a card and rotate its child on the Y axis, but it looks flat. What did you miss?

    `perspective` affects the parent's direct children. If the rotating element is a grandchild, the element in between needs `transform-style: preserve-3d` so the perspective reaches it. Otherwise put `perspective` on the rotating element's direct parent.

  4. 04Why does `transform: rotate(10deg)` do nothing on a `<span>` in the middle of a sentence?

    Non-replaced inline elements can't be transformed. Set `display: inline-block` (or flex, grid, block) on the span first, and the transform applies.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark