Transform y transiciones
Lección 13 de 37 · 4 min
Una caja se puede mover, rotar y escalar sin que el resto de la página se entere. Y con una línea más, ese cambio se anima en vez de saltar.
Mover, rotar y escalar
La propiedad transform recibe una o más funciones. Las tres de base son:
- Mover
translatetranslateX(24px)corre la caja 24px a la derecha;translate(24px, 8px)la mueve en los dos ejes. - Rotar
rotaterotate(45deg)la gira 45 grados en sentido horario. Un ángulo negativo gira al revés. - Escalar
scalescale(1.2)la hace un 20% más ancha y más alta, yscale(0.5)la deja a la mitad de cada lado.
Lo importante: la transformación no toca el layout. La caja sigue ocupando su lugar y las vecinas no se corren. En la demo, la ficha del medio se levanta del atril: su hueco punteado y las fichas vecinas se quedan quietos. La línea naranja mide cuánto la corre translateX.
- M3
- A1
- R1
.ficha {transform: translateX(24px) rotate(30deg) scale(1.2);transform-origin: center;transition: transform 300ms cubic-bezier(0.22, 1, 0.36, 1);}
<divclass="translate-x-6 rotate-30 scale-120 origin-centertransition-transform duration-300 ease-[cubic-bezier(0.22,1,0.36,1)]motion-reduce:transition-none">A</div>
En la versión corta el orden importa. Cada función trabaja sobre los ejes que deja la anterior: con rotate(45deg) translateX(24px) la caja avanza en diagonal, no en horizontal. Por eso conviene escribirlas siempre en el mismo orden: mover, rotar, escalar.
Ángulos y punto de origen
Los ángulos aceptan cuatro unidades. Una vuelta completa vale lo mismo en todas:
1turn = 360deg = 2π rad ≈ 6.2832rad = 400grad
En la práctica alcanza con deg y turn: un cuarto de vuelta es 90deg o 0.25turn. La rotación y la escala se hacen alrededor del punto de origen (transform-origin), que por defecto es el centro de la caja. Con top left la caja gira desde su esquina superior izquierda: el punto naranja lo marca.
CSS también tiene translate, rotate y scale como propiedades sueltas. Se pueden cambiar de a una sin repetir las otras, y el navegador las aplica siempre en ese orden: mover, rotar, escalar.
.ficha {translate: 24px 0;rotate: 30deg;scale: 1.2;}
Transiciones
Una transición (transition) anima el cambio de una propiedad entre su valor viejo y el nuevo. Tiene cuatro partes:
- Propiedad
transition-propertyQué se anima, por ejemplotransform. - Duración
transition-durationEnsoms.1ses igual a1000ms. - Curva
transition-timing-functionCómo se reparte la velocidad.linearva parejo;ease, el valor por defecto, arranca suave, acelera y frena despacio.cubic-bezier(0.22, 1, 0.36, 1)sale rápido y se asienta despacio: va bien para algo que se mueve. - Retardo
transition-delayCuánto espera antes de arrancar.
Animá transform y opacity: el navegador las cambia sin recalcular el layout. La ficha se pinta en una capa propia, encima de la página, y se mueve sola; animar width o top obliga a recalcular el layout en cada cuadro, y se nota.
Y respetá a quien pide menos movimiento desde su sistema:
.ficha {transition: transform 300ms ease-out;}@media (prefers-reduced-motion: reduce) {.ficha {transition: none;}}
En Tailwind
En Tailwind v4, translate-x-*, rotate-* y scale-* no escriben transform: usan las propiedades sueltas translate, rotate y scale. Como el navegador las aplica en ese orden, el resultado es el mismo que mover, rotar y escalar en el atajo.
| CSS | Tailwind |
|---|---|
| translate: 24px 0 | translate-x-6 |
| rotate: 30deg | rotate-30 · -rotate-30 |
| scale: 1.2 | scale-120 |
| transform-origin: top left | origin-top-left |
| transition-property: transform | transition-transform |
| transition-duration: 300ms | duration-300 · delay-150 |
| cubic-bezier(0.22, 1, 0.36, 1) | ease-[cubic-bezier(0.22,1,0.36,1)] |
| prefers-reduced-motion: reduce | motion-reduce:transition-none |
<buttonclass="transition-transform duration-300 ease-outhover:-translate-y-1 hover:scale-105motion-reduce:transition-none">Guardar</button>
Transform and transitions
Lesson 13 of 37 · 4 min
A box can move, rotate and scale without the rest of the page noticing. With one more line, that change animates instead of jumping.
Move, rotate and scale
The transform property takes one or more functions. The three basic ones are:
- Move
translatetranslateX(24px)shifts the box 24px to the right;translate(24px, 8px)moves it on both axes. - Rotate
rotaterotate(45deg)turns it 45 degrees clockwise. A negative angle turns it the other way. - Scale
scalescale(1.2)makes it 20% wider and taller, andscale(0.5)halves each side.
The key point: a transform doesn't touch the layout. The box keeps its place and its siblings don't shift. In the demo, the middle tile is picked up off the rack: its dashed slot and the neighbouring tiles stay put. The orange line measures how far translateX moves it.
- M3
- A1
- P3
.tile {transform: translateX(24px) rotate(30deg) scale(1.2);transform-origin: center;transition: transform 300ms cubic-bezier(0.22, 1, 0.36, 1);}
<divclass="translate-x-6 rotate-30 scale-120 origin-centertransition-transform duration-300 ease-[cubic-bezier(0.22,1,0.36,1)]motion-reduce:transition-none">A</div>
In the shorthand, order matters. Each function works on the axes left by the previous one: with rotate(45deg) translateX(24px) the box moves along the diagonal, not horizontally. That's why it pays to always write them in the same order: move, rotate, scale.
Angles and the origin point
Angles take four units. One full turn is the same in all of them:
1turn = 360deg = 2π rad ≈ 6.2832rad = 400grad
In practice deg and turn are enough: a quarter turn is 90deg or 0.25turn. Rotation and scaling happen around the origin point (transform-origin), which defaults to the center of the box. With top left the box turns from its top left corner: the orange dot marks it.
CSS also has translate, rotate and scale as individual properties. You can change one without repeating the others, and the browser always applies them in that order: move, rotate, scale.
.tile {translate: 24px 0;rotate: 30deg;scale: 1.2;}
Transitions
A transition (transition) animates a property's change from its old value to the new one. It has four parts:
- Property
transition-propertyWhat gets animated, for exampletransform. - Duration
transition-durationInsorms.1sequals1000ms. - Curve
transition-timing-functionHow the speed is spread out.linearstays even;ease, the default, starts gently, speeds up and slows down at the end.cubic-bezier(0.22, 1, 0.36, 1)leaves fast and settles slowly: a good fit for something that moves. - Delay
transition-delayHow long it waits before starting.
Animate transform and opacity: the browser changes them without recalculating layout. The tile is painted on its own layer, above the page, and moves on its own; animating width or top forces a layout pass on every frame, and it shows.
And respect people who ask their system for less motion:
.tile {transition: transform 300ms ease-out;}@media (prefers-reduced-motion: reduce) {.tile {transition: none;}}
In Tailwind
In Tailwind v4, translate-x-*, rotate-* and scale-* don't write transform: they use the individual properties translate, rotate and scale. Since the browser applies them in that order, the result is the same as moving, rotating and scaling in the shorthand.
| CSS | Tailwind |
|---|---|
| translate: 24px 0 | translate-x-6 |
| rotate: 30deg | rotate-30 · -rotate-30 |
| scale: 1.2 | scale-120 |
| transform-origin: top left | origin-top-left |
| transition-property: transform | transition-transform |
| transition-duration: 300ms | duration-300 · delay-150 |
| cubic-bezier(0.22, 1, 0.36, 1) | ease-[cubic-bezier(0.22,1,0.36,1)] |
| prefers-reduced-motion: reduce | motion-reduce:transition-none |
<buttonclass="transition-transform duration-300 ease-outhover:-translate-y-1 hover:scale-105motion-reduce:transition-none">Save</button>