SVG transforms: translate, rotate, scale and skew

Move, rotate, scale and skew SVG shapes with the transform attribute. Covers order of operations, pivot points, transform-box and when to use CSS instead.

A transform changes where an element is drawn without changing its own coordinates. Instead of recalculating every point in a path to move it 50 units right, you leave the path alone and add transform="translate(50 0)". That keeps shapes reusable, makes animation easier, and lets you group things and move them as one.

The transform functions

The transform attribute takes a list of functions, separated by spaces or commas. MDN lists these:

Function What it does
translate(x [y]) Moves by x and y. If y is omitted it is 0.
scale(x [y]) Scales by x and y. If y is omitted it equals x.
rotate(a [x y]) Rotates by a degrees, around the origin or around the point (x, y).
skewX(a) Skews along the x axis by a degrees.
skewY(a) Skews along the y axis by a degrees.
matrix(a b c d e f) A six-value matrix that can express any of the above.

In the attribute, numbers are unitless. Rotation and skew angles are in degrees, and translate and scale values are in the current user coordinate system. rotate(100) means 100 degrees.

Each function can also be written with the pieces separated by commas, such as translate(50, 20). Pick one style and stay consistent.

Here are the four common ones side by side. Edit the numbers in each transform to see the effect.

All four squares are drawn with x="0" y="0". The transform positions them. That is a useful habit: define a shape at the origin, then place it.

Order matters

Transforms are not commutative. Rotating and then moving is different from moving and then rotating. The CSS Transforms specification says the resulting transform is the matrix multiplication of the list, and that the functions are multiplied from left to right. In practice, this means the list reads from the outside in, and a point is affected by the rightmost function first.

MDN gives a simple case:

<rect width="20" height="20" transform="translate(50 0) rotate(45)"/>

The rectangle is rotated 45 degrees about the origin, then moved 50 units to the right. If you reverse the order, the rectangle is moved first and then rotated around the origin, which swings it along an arc and places it somewhere else.

Compare the two below. The only difference is the order of the two functions.

The faint lines mark the origin. The cobalt square is rotated where it was drawn, at the origin, then moved to the right. The magenta one is moved along the x axis and then rotated, so it is carried in an arc away from the line.

Rotating around a point

rotate(a) pivots on the origin of the current coordinate system, which is often the top-left corner. To pivot elsewhere, add the center: rotate(a x y). MDN’s example, rotate(100, 10, 10), pivots on (10, 10). The CSS Transforms specification explains the three-value form as equivalent to a translation by the center, the rotation, then a translation back by the negative center.

For a square drawn at x="40" y="30" width="40" height="40", the center is (60, 50), so the rotation about its own middle is rotate(30 60 50).

The second square uses plain rotate(30), so it swings around (0, 0) and ends up far from its starting place. This is the most common transform surprise in SVG.

Transforms on groups

Put transform on a <g> and it applies to everything inside. Child transforms are applied inside the parent’s, so nesting builds up like a chain of coordinate systems. This is the cleanest way to build a figure out of parts: draw each part around its own origin and place it with a group.

<g transform="translate(100 60)">
  <circle r="30" fill="#1f3fe0"/>
  <g transform="rotate(45)">
    <rect x="-10" y="-50" width="20" height="30" fill="#d4006a"/>
  </g>
</g>

Here the whole figure is centered at (100, 60), and the inner group rotates around that center because its origin is already there.

Transform attribute or CSS transform

You can also set transform from CSS. There are real differences, according to MDN:

  • If both the attribute and the CSS property are set, the CSS property wins.
  • CSS requires units, like rotate(45deg) and translate(10px, 0), while the attribute uses bare numbers. MDN warns that CSS transform functions are supposed to work in the attribute, but that compatibility is not guaranteed. Keep attribute syntax in the attribute.
  • MDN lists the attribute as animatable, and CSS can animate the property too. See animation for both approaches.

The three-value rotate(a x y) form has no equivalent in CSS transform functions, per the CSS Transforms specification. In CSS you set a pivot with transform-origin.

transform-origin and transform-box

For SVG elements, CSS percentages for transform-origin need a reference box, and transform-box chooses it. MDN lists the values: content-box, border-box, fill-box (the object bounding box), stroke-box (the stroke bounding box) and view-box (the nearest SVG viewport). The initial value is view-box. That is why a CSS rotation on an SVG shape often pivots around the middle of the whole drawing instead of the shape.

MDN’s fix is to use transform-box: fill-box together with transform-origin: 50% 50%, so the pivot is the middle of the element itself. MDN marks transform-box as widely available across browsers since January 2020.

.gear {
  transform-box: fill-box;
  transform-origin: 50% 50%;
  animation: spin 3s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

Common mistakes

  • Expecting rotate to spin in place. Without a center it pivots on the origin. Add x y, or draw the shape around the origin and translate it.
  • Writing the order backwards. Read the list and ask what happens to a point at the origin of the shape, starting from the rightmost function.
  • Mixing units. rotate(45deg) inside the attribute may not work everywhere. Use rotate(45).
  • Stacking attribute and CSS transforms. Because CSS wins, an attribute you set will be ignored the moment a stylesheet sets transform on the same element.
  • Scaling strokes unintentionally. A transform scales the stroke width along with the shape. A scale(3) makes a 2-unit stroke 6 units wide.

When not to use transforms

If an element only needs a different position and you are writing it once, setting x and y (or cx and cy) is simpler and easier to read. Transforms earn their place when you group, reuse, rotate or animate.

To control the coordinate grid that all of these transforms work inside, read the viewBox guide. To reveal only part of a transformed shape, continue to masks and clipping. Shape outlines themselves are covered in paths.

Sources

Checked against these references on 2026-11-21.