Rotate an SVG shape around its center with CSS

Choose the reference box that keeps an off-center shape from moving when it turns.

Asset preparation1 min read

A bar in the upper-left of an SVG should turn in place. You set transform-origin: center and rotate it 90°. The bar turns, but it also moves across the canvas. The point called center depends on the reference box.

Find the pivot

Open the editable SVG comparison. Both drawings use a 160 × 140 viewBox. The gray bar starts at (24, 30), measures 64 × 16, and has its center at (56, 38). With the default transform-box: view-box, the rotation uses the center of the SVG viewport’s viewBox at (80, 70). The orange bar’s center ends up at (112, 46).

Use the shape’s bounds

Set transform-box: fill-box on the bar and keep transform-origin: center. The reference box is now the shape’s object bounding box. The mint bar turns 90° and its center stays at (56, 38). CSS Transforms reference boxes.

CSS
.pointer {
  transform-box: fill-box;
  transform-origin: center;
  transform: rotate(90deg);
}

Apply the class to the shape inside the SVG. If several pieces should rotate together, apply the transform to their group and check the group’s bounds. Adding a piece can move that group’s center. If the artwork needs a specific pivot, such as a clock hand attached at one end, specify that point instead of relying on the center.

Image tools
All guides