Transform — Tailwind CSS

A transform rotates, scales or shifts how an element is drawn, without changing its place in the layout: its neighbours don't move. rotate-12 tilts an element…

A product card that grows a little when the pointer is over it, an arrow icon that spins, a discount sticker stuck on at an angle. All three are made with transforms.

A transform changes how an element is drawn: rotated, scaled up, or shifted, without changing its place in the layout. In Tailwind, each kind of transform has its own class: rotate-12 rotates, scale-110 scales up, translate-x-4 shifts. Picture a photo pinned to a notice board. You can tilt it, enlarge it, or slide it a little, but its spot on the board stays the same size, and the photo next to it doesn't move. The photo is the element, and the notice board is the page layout.

Rotating: rotate

This is the same tilted box as in the exercise:

<div id="rotated" class="rotate-12 bg-blue-500 text-white p-4 w-32">
  Tilted!
</div>
Class CSS it generates Meaning
rotate-12 rotate: 12deg; tilted 12 degrees clockwise
bg-blue-500 background-color: var(--color-blue-500); blue background from the palette
text-white color: var(--color-white); white text (#fff)
p-4 padding: 1rem; 16px of padding
w-32 width: 8rem; 128px wide

Scaling and shifting: scale and translate

Class CSS it generates Meaning
scale-75 scale: 75% 75%; shrinks to 75%
scale-110 scale: 110% 110%; grows to 110%
scale-150 scale: 150% 150%; grows to 150%
translate-x-1 translate: 0.25rem 0; shifts 4px to the right
translate-x-4 translate: 1rem 0; shifts 16px to the right
translate-y-2 translate: 0 0.5rem; shifts 8px down
-translate-x-1/2 translate: -50% 0; shifts left by half its own width

A transform doesn't push its neighbours

This is the same row of three boxes as in the Lab:

<div class="flex gap-4">
  <div class="size-16 grid place-items-center bg-slate-300">A</div>
  <div class="size-16 grid place-items-center bg-blue-500 text-white scale-150">B</div>
  <div class="size-16 grid place-items-center bg-slate-300">C</div>
</div>

The browser lays out the page first: A, B and C are 64px each, 16px apart. Only after that is B drawn at 150%, so 96px. B spreads 16px to the left and 16px to the right of its centre. That is exactly the width of the gap between the boxes, so B's edges reach the edges of A and C. But B's spot in the row is still 64px, so A and C don't move a single pixel.

If you want B to really push C, change B's size, for example to size-24 (96px), not its transform. rotate- and translate- are the same: they change only how the element is drawn, not the space it takes.

By default, an element rotates and grows from its centre. Want it from a corner? Add origin-top-left (the turning point at the top left corner), and B grows towards the bottom right from its top left corner.

Growing on hover, with a transition

This is a box that grows when the pointer is over it:

<div class="transition-transform hover:scale-110">
  Grows when hovered
</div>

⚠️ The minus goes in front, and the numbers aren't pixels

TL;DR: A transform rotates, scales or shifts how an element is drawn, without changing its place in the layout: its neighbours don't move. rotate-12 tilts an element 12 degrees clockwise (-rotate-12 the other way). scale-110 grows it to 110%. translate-x-1 shifts it 4px to the right, because each number in translate- = 4px. -translate-x-1/2 shifts it left by half its own width. An element turns and grows from its centre, unless you add origin-top-left (the turning point at the top left corner). For a smooth hover effect, pair hover:scale-110 with transition-transform. In the exercise, all the styling is written as classes: rotate-12 and p-4 (16px padding), with no style attribute.

⚡ Turning a box

Write:

<div id="rotated" class="rotate-12 bg-blue-500 text-white p-4 w-32">
  Tilted!
</div>

Meaning: rotate-12 turns the box 12 degrees clockwise: the number in rotate- is degrees. A minus at the front of the class turns it the other way: -rotate-12.

In the exercise, all the styling is written as classes, with no style attribute.

⚡ The number isn't pixels

Guess first: translate-x-4 shifts an element to the right. How many pixels?

16px, not 4px. Each number in translate- = 4px, just like in p-4 (16px of padding).

Meaning: scale-110 grows an element to 110%, because the number in scale- is a percentage. -translate-x-1/2 shifts an element left by half its own width.

⚡ The neighbours don't move

Write:

<div class="flex gap-4">
  <div class="size-16 grid place-items-center bg-slate-300">A</div>
  <div class="size-16 grid place-items-center bg-blue-500 text-white scale-150">B</div>
  <div class="size-16 grid place-items-center bg-slate-300">C</div>
</div>
Guess first: three 64px boxes sit in a row, 16px apart, and B gets scale-150. Are A and C pushed aside?

No. B is only drawn bigger, at 96px, until its edges touch A and C. B's place in the row stays 64px.

Meaning: A transform (rotate-, scale-, translate-) only changes how an element is drawn, not the space it gets in the row. For a size that really takes up space, use w-, h- or size-.

What you will learn