Transform — CSS

transform moves (translate), resizes (scale), turns (rotate) and slants (skew) an element without pushing other elements, because the element's own spot is…

Product cards in an online shop often grow a little when the cursor is over them (hover). The arrow icon in a menu turns when the menu opens. Oddly, the cards next to the card that grew don't move at all.

That is what the transform property does. transform can move an element (translate), make it bigger or smaller (scale), turn it (rotate) and slant it (skew). Only the way the element is drawn on the screen changes. Its own spot on the page is kept for it, so the elements around it don't move.

Moving without pushing the neighbours

This rule moves a box to the right:

.box {
  transform: translateX(100px);
}

The result: the box moves 100px to the right and can cover the element next to it. Its own spot stays empty and is kept for it, so the element next to it (its neighbour) doesn't move. This is different from margin-left: 100px, which pushes the neighbour too.

Four transform functions

Combining functions: order matters

A small puzzle first. Here are two transform values: translate(100px, 0) scale(2) and scale(2) translate(100px, 0). Do both move the box the same distance? The answer is at the end of this section.

The next example combines a move and a change of size:

.box {
  transform: translate(100px, 150px) scale(1.5);
}

Now the answer to the puzzle: not the same distance. In scale(2) translate(100px, 0), scale(2) comes first, so the 100px move becomes 200px. In translate(100px, 0) scale(2), the move stays 100px.

There are also separate properties named translate, rotate and scale, written without transform. You can see why with a card tilted by transform: rotate(-2deg). If its hover rule is transform: scale(1.05), the card stands straight again on hover, because the new transform replaces the whole old value. With the separate properties, the tilt and the growth both apply:

.card { rotate: -2deg; }
.card:hover { scale: 1.05; }

When to pick which:

⚠️ Transform ignored, or turning around the wrong point?

💡 Transform is made for animation. The browser does not have to recalculate the page layout when a transform changes, so the movement is smooth. For hover effects and animations, move elements with transform, not with margin or left.

🔬 Try it yourself

Move, turn, scale and slant the box below, and watch its transform code change.

TL;DR: transform moves (translate), resizes (scale), turns (rotate) and slants (skew) an element without pushing other elements, because the element's own spot is kept for it. All four are called functions. Several functions go in one transform, separated by spaces, because a second transform in the same rule replaces the first. The order matters: write translate() first, then scale(), so the move is not scaled up too.

⚡ Moving without pushing the neighbours

Write:

.box {
  transform: translateX(100px);
}

Meaning: translateX(100px) moves the box 100px to the right. Its own spot is kept for it, so the element next to it (its neighbour) doesn't move. This is different from margin-left, which pushes the neighbour too.

⚡ Move, resize, turn, slant

Meaning: The value of transform is made of functions (a name followed by brackets). translate(100px, 150px) moves the element 100px to the right and 150px down. scale(1.5) makes it 1.5 times its normal size (the number has no unit). rotate(45deg) turns it 45 degrees clockwise. skewX(10deg) slants it.

⚡ The order matters

Write:

.box {
  transform: translate(100px, 150px) scale(1.5);
}
Guess first: how far does scale(2) translate(100px, 0) move the box?

200px. scale(2) comes first, so the 100px move is doubled too.

Meaning: All the functions go in one transform, separated by spaces. Write the move (translate) first, then the size (scale), so the move is not scaled up. A second transform in the same rule replaces the first.

What you will learn