Skip to content
Tutorials Center
My learning

Topics

Lesson 98 of 139 ยท Transforms and Animation

2D Transforms

Move, rotate, scale and skew elements with CSS transforms. Learn transform-origin, why function order matters, and the translate, rotate and scale properties.

A transform changes how an element is drawn without changing where it sits in the layout. You can slide it, spin it, grow it or slant it, and the elements around it don't move an inch. The browser simply paints the box somewhere else, or in a different shape.

That makes transforms perfect for interface effects: a card that lifts on hover, an icon that rotates when a menu opens, a badge that pulses. Combined with transitions, they are also the smoothest way to animate, because the browser does not need to recompute the layout of the page on each frame.

In this lesson you will learn the four 2D transform functions, how transform-origin changes the pivot point, why the order of functions matters, and the newer individual translate, rotate and scale properties.

The transform property

The transform property takes one or more transform functions, separated by spaces:

CSS
.badge {
  transform: rotate(-8deg);
}

.card:hover {
  transform: translateY(-4px) scale(1.02);
}

There are four families of 2D functions:

Function What it does Examples
translate() Moves the element translate(20px, 10px), translateX(50%), translateY(-1rem)
rotate() Turns it around a point rotate(45deg), rotate(-0.25turn)
scale() Makes it bigger or smaller scale(1.5), scale(2, 0.5), scaleX(-1)
skew() Slants it skew(10deg), skewX(-15deg), skewY(5deg)

Positive angles rotate clockwise. Angles can use deg, turn, rad or grad; 0.25turn equals 90deg. A scale of 1 means normal size, 0.5 half and 2 double, and a negative scale flips the element, so scaleX(-1) mirrors it horizontally.

The four transform functions

Look closely at the "scale" box: it grows past its dashed outline and overlaps the border, but the grid itself does not move. Transformed elements keep their original space in the layout. Overflow caused by a transform can still create scrollbars, though, so keep an eye on elements translated off the edge.

Transforms don't affect layout

This is the most important thing to understand about transforms. The browser lays out the page first, as if no transforms existed. Then it paints each transformed element moved, rotated or scaled from that position.

Two practical consequences follow:

  • Neighbors don't react. If you scale a card up by 10%, it overlaps the cards next to it. If you need surrounding content to make room, change real sizes like width, or use layout tools such as Flexbox.
  • It is cheap to animate. Because nothing needs to be re-measured, browsers can animate transforms smoothly on the graphics card.

Transforms also change a few other behaviors. Any element with a transform other than none creates a new stacking context, and becomes the containing block for descendants with position: fixed, which then scroll with it instead of sticking to the viewport.

Percentages in translate

In translate(), percentages refer to the element's own size, not its parent's. translateX(100%) moves an element exactly one of its own widths to the right. That gives a famous centering trick for absolutely positioned elements of unknown size:

CSS
.popup {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

top and left put the popup's top-left corner in the center of its parent, and the translation pulls it back by half its own width and height. Today Grid's place-items: center is often simpler, as shown in Centering Anything, but you'll see this pattern in plenty of real code.

transform-origin: the pivot point

Rotation, scaling and skewing all happen around a fixed point called the transform origin. By default, it is the element's center (50% 50%). Change it with transform-origin, using keywords, percentages or lengths:

CSS
.door  { transform-origin: left center; }   /* swings like a hinged door */
.flag  { transform-origin: top left; }
.hand  { transform-origin: 50% 100%; }      /* bottom middle, like a clock hand */

Translation is not affected by the origin, because moving an element shifts every point by the same amount.

Changing the transform origin

Order matters

When you list several functions, the browser applies them from right to left. Another way to think about it: each function transforms the element's coordinate system for the functions that follow it, reading left to right. So these two are different:

CSS
transform: translateX(100px) rotate(45deg); /* move right, then spin in place */
transform: rotate(45deg) translateX(100px); /* spin the axes, then move along the tilted x axis */

In the second line, rotating first turns the x axis 45 degrees, so the 100px move goes diagonally down and to the right. The same thing happens with scale: scale(2) translateX(50px) actually moves the element 100px, because the move is measured in the scaled coordinate system.

Same functions, different order

Individual transform properties

Writing everything inside one transform value has a drawback: to change one function, you must repeat all of them. Suppose a button is rotated, and on hover you want to scale it too:

CSS
.icon       { transform: rotate(-10deg); }
.icon:hover { transform: rotate(-10deg) scale(1.2); } /* must repeat the rotate */

Modern CSS has three standalone properties, translate, rotate and scale, which you set independently:

CSS
.icon       { rotate: -10deg; }
.icon:hover { scale: 1.2; } /* rotate is kept automatically */

The syntax drops the parentheses and commas: translate: 20px 10px, rotate: 45deg, scale: 1.2 or scale: 2 0.5. They also make it easy to transition or animate each part with its own timing.

The browser always applies them in a fixed order: translate first, then rotate, then scale, and finally anything in transform. If you need a different order, use the transform property. All three have been supported in every major browser since 2022.

Combining individual transform properties

Skew and matrix

skew() slants an element along one or both axes, turning rectangles into parallelograms. It is popular for decorative banners and "speed" effects. Text gets slanted too, so a common trick is to skew a container and counter-skew its content: skewX(-12deg) on the wrapper and skewX(12deg) on the text inside.

You may also meet matrix(a, b, c, d, e, f) in DevTools. Every 2D transform can be expressed as a single 3ร—3 matrix, and the Computed panel shows transforms that way. You almost never write it by hand.

Inline elements can't be transformed. Transforms apply to block-level and atomic inline boxes, like blocks, flex items, grid items and inline-block elements. A plain <span> with display: inline ignores transform. That is why the arrow above uses display: inline-block.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. An element is scaled with scale(1.5). What happens to the elements next to it?

    Transforms don't affect layout. The element keeps its original space and is only painted bigger.
  2. 2. What does the percentage in transform: translateX(-50%) refer to?

    Percentages in translate are relative to the element's own size, which is why translate(-50%, -50%) centers it on a point.
  3. 3. Which property moves the point that an element rotates around?

    transform-origin sets the pivot for rotate, scale and skew. The default is the center (50% 50%).
  4. 4. In what order are translate, rotate, scale and transform applied?

    The individual properties always apply in a fixed order, followed by the transform property.
  5. 5. Why might transform have no effect on a <span>?

    Non-replaced inline boxes ignore transforms. Changing display to inline-block or block fixes it.

Summary

  • transform moves, rotates, scales and skews an element when it is painted, without changing layout.
  • translate() percentages use the element's own size; positive rotate() angles turn clockwise.
  • transform-origin sets the pivot for rotate, scale and skew; the default is the center.
  • Functions in one transform value apply right to left, so their order changes the result.
  • The individual translate, rotate and scale properties can be set and animated independently.
  • Transforms create a stacking context and need block-level or inline-block boxes.

Next: 3D Transforms โ€” add depth with perspective and build a card that flips over.