Updated November 29, 2026  ·  11 min read  ·  SVGVector

Rotate, Flip and Crop an SVG Around Its Center

An SVG has no single centre, so "rotate it around the centre" is not one instruction — it is a choice between three different points, and the choice is measurable. In a 200 by 200 stage holding one 130 by 80 piece, turning it 30 degrees about the user-space origin walked its ink 62.59 px and pushed 1,896 of its 8,440 pixels off the frame; about the viewBox centre it walked 3.87 px; about the bounding-box centre 3.9 px; about the ink centroid 0.01 px. The distance it travels is not a mystery either — it is 2·d·sin(θ/2), where d is the gap between the pivot and the centroid, which is why a 180 degree turn moved the same piece 25.08 px. Thirty-four cases later, the rule for flipping and cropping falls out of the same arithmetic: a mirror is a transform that moves the piece unless you translate it back, and a crop changes four numbers, not a size.

Top row: one 130 by 80 piece turned 30 degrees about four different pivots, with the distance its ink centroid travelled under each. Bottom row: the same drawing cropped by a viewBox, by preserveAspectRatio none and by a CSS overflow box, each with the ink it painted.
The piece never changed; the pivot did. The four turns on the top row paint between 6,544 and 8,560 pixels of the same 8,440-pixel drawing, because a wrong pivot walks part of it off the frame. The crops on the bottom row do the opposite: same markup, 4 to 6 times the ink.

Three centres in one picture

The test piece is a filled rectangle with a triangle stuck to its left side, which is deliberately unbalanced. Its bounding box runs from (20,60) to (150,140), so the box centre is (85,100). Its ink centroid — the average position of every painted pixel — sits at (92.54,100), because the triangle carries less weight than the bar. The viewBox of the stage puts its own centre at (100,100). One drawing, three candidates, and 15 units between the outermost two.

That gap is the whole problem. Whichever point you hand to rotate(), the piece turns around it and the ink moves — unless the point sits on the centroid, where a turn is by definition a turn in place. Each case below is the same markup with one attribute changed, measured by counting painted pixels, so "slightly off" becomes a distance.

rotate(30) turns about the origin, not about the piece

The transform attribute with one number is a rotation about (0,0) of the user coordinate system. For a piece drawn at x 20 to 150 that is nowhere near the origin, the result is not a rotation in place, it is a rotation plus a decision about where the piece now lives. Turned 30 degrees, the copy's client box starts at x = −53: more than a third of the piece is outside the frame, and what remains is the bottom-left corner of it. Ink fell from 8,440 to 6,544 pixels — 1,896 pixels, 22.5% of the drawing, quietly clipped by the edge of the svg element.

The ink centroid tells the same story in one number: 62.59 px away from where it started. That is the number to watch, because it does not depend on how the piece is shaped, only on how far the pivot is from the mass.

CSS transform on an SVG child is not the box model

This is where most of the surprise lives. On an HTML element, transform: rotate(30deg) turns about the middle of the box, because transform-origin defaults to 50% 50%. On an SVG child, Chrome reports a computed transform-origin of 0px 0px and transform-box: view-box — so the same CSS produces the same 62.59 px walk as the bare attribute, down to the pixel count of 6,544. Nothing about the CSS box model applies until you ask for it:

// The centre you almost always want: the middle of the painted box, in user units.
function pivotOf(el) {
  const b = el.getBBox(); // user units, before the element's own transform
  return { cx: b.x + b.width / 2, cy: b.y + b.height / 2 };
}
const p = pivotOf({ getBBox: () => ({ x: 20, y: 60, width: 130, height: 80 }) });
// p is { cx: 85, cy: 100 } — the bbox centre of the test piece

Ask for it explicitly and the behaviour changes: transform-box: fill-box plus transform-origin: 50% 50% resolves the origin to 65px 40px — half of the 130×80 bbox — and the result becomes identical to rotating about the bbox centre, 8,557 ink pixels and 3.9 px of drift. Identical again to the long-hand translate(cx,cy) rotate() translate(-cx,-cy), which measured 3.9 px. Three spellings, one geometry.

// rotate(deg) turns about (0,0). To turn about a point, bracket it.
function rotateAbout(deg, cx, cy) {
  return `translate(${cx} ${cy}) rotate(${deg}) translate(${-cx} ${-cy})`;
}
// 30 degrees about the bbox centre of the test piece:
//   translate(85 100) rotate(30) translate(-85 -100)

The pivot that holds is the centroid, not the bounding box

A bbox centre is easy to compute and it is what every tutorial recommends, but it is the centre of the box, not of the ink. On this piece the two are 7.54 units apart, and a 30 degree turn converts that into 3.9 px of drift — small, visible on a 24 px icon, invisible on a poster. Handing the same function the centroid instead:

// Any point d away from the pivot travels 2*d*sin(deg/2). At 0 it is 0, at 180 it is 2*d.
function drift(d, deg) {
  const r = (deg * Math.PI) / 180;
  if (deg === 180) return 2 * d;
  return 2 * d * Math.sin(r / 2);
}
// A piece whose ink centroid sits 7.54 units from the pivot, turned 30 degrees:
//   drift(7.54, 30)  = 3.90  (measured 3.90 on the bbox-centre case)
// A 180 turn doubles the distance instead of hiding it:
//   drift(12.54, 180) = 25.08 (measured 25.08)

gives 0.01 px of measured drift, which is antialiasing. The formula is worth keeping because it predicts every case in this article: d is the distance from the pivot to the centroid, and at θ = 30 degrees the multiplier is 0.518, so a piece with its mass 20 units off the pivot walks 10 px. At 90 degrees the multiplier is 1.41; at 180 it is exactly 2, which makes a half turn the cheapest way to detect a pivot that is wrong.

Symmetry hides all of this. The same code on a plain 80×80 bar, whose centroid and bbox centre coincide at (110,100), produced a 110×110 bbox and left the centroid exactly where it was — 0 px of drift, because there was nothing to rotate around.

Four quarter turns prove nothing; one half turn proves everything

Rotating 90 degrees four times returns any piece to its starting position, whatever pivot you chose, because four turns of 90 is 360 degrees about the same point. The measured result is 0.00 px of drift and 8,440 painted pixels, indistinguishable from the untouched control. A designer who checks with quarter turns will sign off on a pivot that is 20 units wrong.

A single 180 degree turn doubles the error instead: the same piece rotated about (80,100), five units to the left of the bbox centre, came back with exactly the same bounding box — 130×80 — and 8,440 painted pixels, but shifted 25.08 px left. Same size, same area, different position: the only signal is the centroid.

A mirror is not a centring move

scale(-1,1) reflects about the y axis, which in user space is the line x = 0. The piece sits at x 20 to 150, so its reflection lands at x −20 to −150 and paints nothing: 0 ink pixels, and the element's client box starts at x = −150. The CSS form is no better — transform: scaleX(-1) with the default origin also paints 0 pixels, for the same reason the rotation went wrong.

Add the translation and it behaves, in three interchangeable spellings:

// A mirror about the vertical line x = k is a scale plus a translate.
function mirrorAbout(k) {
  return `translate(${2 * k} 0) scale(-1 1)`;
}
// k = 100 → "translate(200 0) scale(-1 1)"; k = 92.54 → "translate(185.08 0) scale(-1 1)"
// Leave out the translate and the copy lands at negative x, off the canvas.

All three paint 8,440 pixels again, mirrored, with the centroid at 106.96 — which is 2·99.5 − 92.04, the mirror of the original position about the stage centre, 14.92 px from where it began. A mirror is not a move to the centre; it is a reflection about a line, and the piece only stays put if that line passes through the centroid. Mirroring about x = 92 left 1.08 px of drift, and about the centroid itself 0.07 px.

Flipping text mirrors the glyphs

A mirrored group mirrors everything inside it, including type. The measured text block kept its size — 139×30 px, 2,325 pixels against 2,326 — and its baseline, and moved 9.8 px, and that is the entire visible difference if you only compare areas. Compare the column profile of the ink instead and the answer is unambiguous: correlation with the reversed original is 1.0000, correlation with the original is 0.5948. Every glyph is a mirror image of itself.

// Elements that a mirrored group turns into unreadable copies of themselves.
function mirroredText(svgMarkup) {
  const doc = new DOMParser().parseFromString(svgMarkup, 'image/svg+xml');
  const bad = [...doc.querySelectorAll('text, tspan, foreignObject')].map((n) => n.tagName);
  const ce = doc.documentElement;
  if (ce.querySelector('parsererror')) return ['parsererror'];
  return bad;
}
// mirroredText('<svg xmlns="http://www.w3.org/2000/svg"><g><rect/><text>PLUMB</text></g></svg>')
//   → ['text']   mirror the art, redraw the label unmirrored

The fix is structural, not stylistic: mirror the artwork, keep the label outside the mirrored group and reposition it by hand, or mirror about the centroid so the label lands on top of itself and stays readable.

Cropping is a viewBox change, not a resize

A crop with four numbers is the cheapest edit in SVG and the one that surprises people, because the path data does not change at all. The same drawing — four corner squares and a 34-unit circle, 5,984 painted pixels — re-framed six ways:

Frame Scale Ink What changes
200 box, viewBox 0 0 200 200 1.0× 5,984 the control
200 box, viewBox 70 70 60 60 3.33× 36,592 the window shrinks, everything grows
200 box, viewBox 70 70 100 40 2.0× 14,264 letterboxed, art still spills over
same, preserveAspectRatio none 2× / 5× 23,287 stretched, the circle is an ellipse
120 box, overflow:hidden 1.0× 3,182 cut, not scaled, not centred
… with a −40 px offset 1.0× 3,680 centred by hand, still 1:1
clipPath 70 70 60 60 1.0× 3,304 exactly 60×60 px survives

Two of those rows are the ones people get wrong. Shrinking the viewBox is a zoom, not a cut: a 60-unit window in a 200 px box draws everything 3.33 times larger, so the ink rises to 36,592 px, 6.1 times the control, and the corners that used to define the frame are now outside it. And the default preserveAspectRatio letterboxes rather than clips to the window: the 100×40 window maps to rows 60 to 140, yet the 68-unit circle still paints 136 px tall and spills to rows 52 to 187, because clipping happens at the viewport, not the window.

// A viewBox is x y width height — four numbers, in that order, and the scale is
// the box size divided by the viewBox size.
function viewBoxOf(x, y, w, h, boxW, boxH) {
  const s = Math.min(boxW / w, boxH / h);
  return { viewBox: `${x} ${y} ${w} ${h}`, scale: +s.toFixed(3),
           drawn: [+(w * s).toFixed(2), +(h * s).toFixed(2)],
           fits: w * s <= boxW + 0.01 && h * s <= boxH + 0.01 };
}
// viewBoxOf(70, 70, 100, 40, 200, 200) → scale 2, drawn 200 x 80, fits true
// viewBoxOf(70, 70, 60, 60, 200, 200)  → scale 3.333, drawn 200 x 200, fits true

Turn the aspect ratio off and the distortion is measurable: a 100×100 window in a 200×100 box with preserveAspectRatio="none" draws the circle 136×68 px, exactly twice as wide as tall, while the default meet draws it 68×68 — ink of 7,356 against 3,680.

overflow:hidden crops without scaling

A CSS crop is a different animal: the box shrinks, the drawing does not. At 120 by 120 with no offset the result is 3,182 painted pixels — the top-left 120 px of a 200 px drawing, cut at the right and bottom edges, no zoom, no centring. Shifting the svg by −40 px in both axes centres the window on the drawing and paints 3,680 px, which is the circle and nothing else. If you want the crop to be automatic on every screen size, use the viewBox; if you want a fixed 1:1 cut inside a card, overflow:hidden is cheaper. Embedding as an image adds a third behaviour: object-fit: cover on a 2:1 source inside a 120 px square painted 4,128 px after throwing away both side blocks, and contain painted 2,772 px with 30 px bars above and below.

When the numbers above are the only thing you need, a form is quicker than a spec: the crop, flip and rotate tool writes the same viewBox and transform attributes and shows the result before you commit. For photo work rather than vector work, the browser photo editor crops and flips in one tab without installing anything.

// A crop that keeps the aspect ratio: grow the short side until w/h matches,
// centred on the same point.
function cropWindow(x, y, w, h, aspect) {
  if (w / h > aspect) {                 // too wide: grow the height
    const h2 = w / aspect;
    return { x, y: +(y - (h2 - h) / 2).toFixed(4), w, h: +h2.toFixed(4) };
  }
  const w2 = h * aspect;                // too tall: grow the width
  return { x: +(x - (w2 - w) / 2).toFixed(4), y, w: +w2.toFixed(4), h };
}
// cropWindow(20, 60, 130, 80, 1) → { x: 20, y: 35, w: 130, h: 130 }
// which writes viewBox="20 35 130 130": square, same centre, nothing stretched.

Transform order changes the shape, not the area

Rotate then stretch, or stretch then rotate: the two orders paint 12,800 and 12,797 pixels, three pixels apart, and different shapes — bboxes of 172×170 and 152×200. Area tells you nothing; the box tells you everything. A chain that mixes a non-uniform scale with a rotation has to be read right to left, the way SVG applies it.

What getBBox reports, and what it hides

In all eleven rotation cases getBBox() returned the same four numbers — 20, 60, 130, 80 — because it reports the geometry before the element's own transform. The client rect moved every time. So a debugging session that prints the bbox sees an immovable object, and a session that prints the client rect sees a piece that jumps. Use getBBox() to find the pivot, and the client rect to verify where the ink ended up. It also ignores clip-path: the 60×60 clip case still reported 168×168 of geometry while painting 3,304 px inside a 60 px square.

The recipe

Four steps, in this order, and the piece stays where you put it:

  1. Measure getBBox() in user units. If the piece is asymmetric, compute the centroid from the painted pixels — the browser will not do it for you, but a one-off canvas read will.
  2. Prefer transform-box: fill-box; transform-origin: 50% 50% if CSS is available, and the three-part translate / rotate / translate if the attribute is what you ship. Both measured 3.9 px on a piece whose centroid was 7.54 units off centre.
  3. For a mirror, translate by twice the mirror line — translate(2k 0) scale(-1 1) — and count any text inside the group before you ship it.
  4. For a crop, change the viewBox and recompute the centre, don't just move the corner. If the container's aspect ratio differs from the window's, decide between meet, slice and none deliberately, because the last one stretches the drawing by the ratio of the two scales.

Related SVG Workflows

Try These Free Online Tools

No download required — convert, compress & optimize SVGs right in your browser

Transform

Crop, Flip, Rotate

Set the viewBox and the transform, see the result before you ship it.

Open tool
Edit

SVG Editor

Drag nodes and nudge geometry without re-exporting from the design file.

Open editor
Resize

Resize SVG

Change width and height while the viewBox keeps the proportions.

Open resizer
Code

SVG Code Editor

Paste a file, watch the attributes, copy the cleaned markup.

Open editor
Render

SVG to PNG

Count the pixels the crop really paints, at the size you need.

Render it
Validate

SVG Validator

Catch the missing namespace or a broken path before the browser hides it.

Open validator