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 |
- The number after
rotate-is degrees:rotate-45tilts 45 degrees,rotate-90stands it on its side,rotate-180turns it upside down. - A minus sign at the front of the class turns the other way:
-rotate-12tilts 12 degrees anticlockwise. - All the styling is written as classes. No
styleattribute at all. - Tailwind v4 writes the rotation with the CSS
rotateproperty. Tailwind v3 wrote it astransform: rotate(12deg). On screen they look the same.
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 |
- The number in
scale-is a percentage.scale-100is the normal size. - The number in
translate-uses the same scale asp-andw-: one step = 0.25rem = 4px. Sotranslate-x-1= 4px, andtranslate-x-4= 16px, not 4px. - A fraction is calculated from the element's own size: a 64px box with
-translate-x-1/2shifts 32px to the left. A classic way to put an element exactly in the middle of its reference (arelativeparent) uses fractions:absolute top-1/2 left-1/2 -translate-x-1/2 -translate-y-1/2.top-1/2 left-1/2puts the element's top left corner at the middle point of the reference. Then the two translates shift the element left and up, by half its own width and half its own height.
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>
hover:scale-110only applies while the pointer is over the element: the box becomes 110%.transition-transformmakes the change gradual, over 150 milliseconds, instead of a jump. The plaintransitionclass animates transforms too.- Without a transition class, the box jumps to 110% the moment the pointer is over it.
⚠️ The minus goes in front, and the numbers aren't pixels
- Using the
styleattribute.style="transform: rotate(12deg)"does rotate, but this exercise practises classes. Writerotate-12in theclassattribute, with nostyle. - Thinking the number is pixels.
translate-x-4is 16px, not 4px. The 4px one istranslate-x-1. Same as padding:p-4= 16px. - The minus in the wrong place. It's
-rotate-12and-translate-x-4, with the minus at the front.rotate--12orrotate-(-12)generate no CSS. - Using a transform to make room.
scale-150makes a box grow towards its neighbours and it can cover them, but it doesn't push them. For a size that really takes up space, usew-,h-orsize-.
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-.