Question

Apply Multiple CSS Transforms: rotate() and translate()

csscss-transforms

Question

Using CSS, how can I apply more than one transform to the same element? In this example, only the translation appears to be applied, while the rotation is not:

li:nth-child(2) {
  transform: rotate(15deg);
  transform: translate(-20px, 0px);
}

How can both the rotation and translation be applied?

Short Answer

You will learn why separate transform declarations overwrite one another, how to combine transform functions in one declaration, and why the order of transforms changes the final result.

Concept

The CSS transform property changes how an element is visually rendered without changing its normal position in the document layout. It can rotate, move, scale, skew, and perform 3D transformations.

A CSS rule can contain many properties, but assigning the same property twice does not combine its values. The later declaration usually wins because of the CSS cascade.

transform: rotate(15deg);
transform: translate(-20px, 0);

Both declarations target transform, so the second one replaces the first. To apply multiple effects, put multiple transform functions into one transform declaration:

transform: rotate(15deg) translate(-20px, 0);

Transform order is important. CSS applies transform functions from right to left. Therefore, changing the order can produce a different position and orientation.

Mental Model

Think of transform as a single instruction card handed to the browser.

If you hand in one card that says “rotate 15 degrees,” then hand in another card that says “move left 20 pixels,” the newer card replaces the old one. The browser follows only the second card.

Instead, write one card containing a sequence of instructions:

transform: rotate(15deg) translate(-20px, 0);

The sequence matters. Moving an object and then rotating it can place it somewhere different from rotating it and then moving it.

Take Quiz

Syntax and Examples

Use a single transform property followed by one or more transform functions separated by spaces.

selector {
  transform: function-one(...) function-two(...) function-three(...);
}

For example, rotate the second list item and move it 20 pixels left:

li:nth-child(2) {
  transform: rotate(15deg) translate(-20px, 0);
}

Common 2D transform functions include:

.card {
  transform: translateX(20px) rotate(5deg) scale(1.1);
}
  • translateX(20px) moves the element right.
  • rotate(5deg) turns the element clockwise.
  • scale(1.1) makes the element 10% larger.

Order example

These rules are not necessarily equivalent:

/* The translation is performed first, then the result is rotated. */
.first {
  transform: rotate(45deg) translateX(100px);
}

/* The rotation is performed first, then the element is translated. */
.second {
  transform: translateX(100px) rotate(45deg);
}

Because transforms are applied right to left, the first rule moves along the element's rotated axis, while the second rule moves along the page's horizontal axis before rotation affects its appearance.

Step by Step Execution

Consider this rule:

.badge {
  transform: rotate(15deg) translateX(-20px);
}

The functions are applied from right to left:

  1. translateX(-20px) moves the badge 20 pixels to the left.
  2. rotate(15deg) rotates that moved badge 15 degrees clockwise around its transform origin.
  3. The browser draws the final transformed result.

Now reverse the functions:

.badge {
  transform: translateX(-20px) rotate(15deg);
}

This time:

  1. rotate(15deg) rotates the badge first.
  2. translateX(-20px) then moves it left relative to the page's horizontal axis.
  3. The final position differs from the first version.

You can also control the point around which rotation and scaling happen:

.badge {
  transform-origin: center;
  transform: rotate(15deg) translateX(-20px);
}

The default transform-origin is the element's center.

Real World Use Cases

Multiple transforms are useful whenever an interface needs both movement and visual styling.

  • Hover effects: lift a button upward and enlarge it slightly.
    .button:hover {
      transform: translateY(-3px) scale(1.03);
    }
    
  • Cards and product tiles: rotate a decorative image while positioning it.
  • Dropdown menus: move a menu into place while scaling it from a smaller size.
  • Loading indicators: place dots around a circle and rotate them.
  • Mobile navigation: slide a panel into view with translateX().
  • Data visualizations: scale or position SVG elements without modifying their layout coordinates.

Transforms are often preferred for visual motion because translate() can be animated smoothly and does not require changing layout properties such as left or top.

Take Quiz

Real Codebase Usage

In real projects, developers commonly keep all transform functions for one visual state in a single declaration.

Base state and hover state

.card {
  transform: translateY(0) scale(1);
  transition: transform 180ms ease;
}

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

The hover rule replaces the complete base transform with a new complete transform. This avoids accidentally losing a rotation, scale, or translation that was part of the original state.

Reusable values with custom properties

CSS custom properties can make a complex transform easier to maintain:

.avatar {
  --tilt: -4deg;
  --offset-x: 8px;
  transform: rotate(var(--tilt)) translateX(var(--offset-x));
}

Separate wrapper elements for independently animated effects

A single transform property cannot independently preserve transforms from unrelated rules. When separate components need separate motion, use nested elements:

<div class="floating">
  <img class="tilted-logo" src="logo.png" alt="Company logo">
</div>
.floating {
  animation: float 2s ease-in-out infinite alternate;
}

.tilted-logo {
  transform: rotate(-8deg);
}

@keyframes float {
  to {
    transform: translateY(-12px);
  }
}

The parent handles floating, while the child keeps its rotation.

Common Mistakes

Declaring transform twice

/* Only translate() remains active. */
.item {
  transform: rotate(15deg);
  transform: translateX(-20px);
}

Avoid it: combine the functions in one declaration.

.item {
  transform: rotate(15deg) translateX(-20px);
}

Assuming transform order does not matter

transform: rotate(45deg) translateX(100px);

This does not produce the same result as:

transform: translateX(100px) rotate(45deg);

Avoid it: test the order and remember that CSS applies the rightmost function first.

Expecting transforms to move surrounding layout

.notice {
  transform: translateY(30px);
}

The notice looks lower, but surrounding elements still use its original layout space.

Avoid it: use margins, padding, flexbox, grid, or positioning when the layout itself must change. Use transforms for visual movement and animation.

Overwriting a transform in a modifier class

.icon {
  transform: rotate(20deg);
}

.icon--active {
  transform: scale(1.2);
}

When both classes are present, the active rule can replace the rotation.

Avoid it: include all needed functions in the winning rule, use custom properties, or split effects across wrapper elements.

Forgetting units

/* Invalid values */
transform: rotate(15) translateX(20);

Avoid it: use angle units such as deg and length units such as px, %, rem, or vw.

transform: rotate(15deg) translateX(20px);

Comparisons

ApproachWhat it doesBest use
transform: rotate(...) translate(...);Combines visual transformations in one property valueRotate, move, scale, or skew one element
Two transform declarationsThe later declaration replaces the earlier oneUsually avoid this when both effects are needed
translate()Visually moves an element without changing layout flowAnimation, hover movement, panels
margin, top, leftChanges spacing or positioning rules, which can affect layoutPermanent layout placement
Nested elementsLets parent and child have independent transform propertiesSeparately animated rotation, scaling, and movement
Individual rotate, translate, scale propertiesLets those transform components be declared separately in modern CSSCases where independent transform components improve readability

Modern CSS also supports individual transform properties in supported browsers:

.item {
  rotate: 15deg;
  translate: -20px 0;
}

However, transform: ... remains widely used, especially for ordered transform sequences and existing codebases.

Take Quiz

Cheat Sheet

/* Combine transform functions in one declaration */
.element {
  transform: rotate(15deg) translateX(-20px) scale(1.1);
}
  • Multiple transform: declarations do not merge; the later one wins.
  • Separate functions with spaces, not commas.
  • Functions are applied from right to left.
  • translateX(-20px) moves left; positive values move right.
  • translateY(-20px) moves up; positive values move down.
  • rotate(15deg) rotates clockwise; negative angles rotate counterclockwise.
  • scale(1.2) makes an element 20% larger.
  • Use transform-origin to change the rotation or scaling pivot.
.element {
  transform-origin: top left;
  transform: rotate(15deg) scale(1.1);
}
  • Transforms affect visual rendering, not normal document layout.
  • For independent transforms, use nested elements or carefully use individual transform properties.

FAQ

Why does only the last CSS transform apply?

transform is one CSS property. When it is declared more than once in the same rule, the later declaration overrides the earlier declaration.

How do I rotate and translate an element in CSS?

Place both functions in one value:

transform: rotate(15deg) translateX(-20px);

Does the order of CSS transforms matter?

Yes. CSS applies transform functions from right to left, so changing their order can change the final position.

Can I use commas between CSS transform functions?

No. Write transform functions with spaces:

transform: translateX(20px) rotate(10deg);

Does translate() affect the layout of other elements?

No. It visually moves the transformed element but does not make neighboring elements reflow around its new visual location.

How can I rotate an element around a different point?

Use transform-origin:

transform-origin: top left;
transform: rotate(30deg);

Can a parent and child each have their own transform?

Yes. Each element has its own transform property, so nesting is a useful way to compose independent effects.

Take Quiz

Mini Project

Description

Create a small set of profile cards with a subtle hover interaction. Each card should lift upward, grow slightly, and rotate by a small amount. This demonstrates how several transform functions work together in a realistic interface effect.

Goal

Build cards whose hover state combines translation, scaling, and rotation in one transform declaration.

Requirements

Create at least three profile cards with a name and role. Add a transition for each card's transform. On hover, move each card upward and make it slightly larger. Apply a small rotation as part of the same hover transform. Use a different rotation direction for at least one card.

Take Quiz

Keep learning

Related questions

View more