日本語

Change the Center of CSS transform-origin Rotation and Scaling

​​CSS​Animation,Playwright,SVG

This article was translated from the Japanese original with AI. 日本語の原文を読む

When you use rotate() or scale() with the CSS transform property, the element normally rotates or grows around its center. You can change this center with transform-origin. It is used for effects such as a dropdown menu that opens downward from its top edge, a clock hand that rotates from its base, and an SVG loading icon that spins in place.

Note

My test environment was Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I placed a 100px square box with its top-left corner at (200, 200), applied transform and transform-origin to it, read the transformed bounding box with getBoundingClientRect(), and read the computed transform-origin value with getComputedStyle(). I did not test Firefox or Safari, or 3D transforms (such as rotateX() or the z value of a third component).

For HTML elements, the center is the middle if nothing is specified

The initial value of transform-origin is 50% 50% (CSS Transforms specification). For the 100px square box, getComputedStyle() returned 50px 50px, the same as when I wrote transform-origin: center.

The following figure shows the same rotate(45deg) applied with only the center position changed.

Three stacked diagrams. In the top one, with transform-origin: center, the light blue box is rotated 45 degrees around the pink dot at its middle, while still overlapping the gray dotted outline of its original position. In the middle one, with top left, the box rotates around the pink dot at its top-left corner and swings down to the left. In the bottom one, with bottom right, the box rotates around its bottom-right corner and swings up to the right.
A box rotated by rotate(45deg) with transform-origin set to center, top left, and bottom right. The pink dot marks the transform-origin position.

I measured the bounding box in the test environment. The original box had left 200, top 200, right 300, and bottom 300.

transform-origin Bounding box after rotation (left, top, right, bottom)
Not written / center (179.29, 179.29, 320.71, 320.71)
top left (129.29, 200, 270.71, 341.42)
bottom right (229.29, 158.58, 370.71, 300)

With center, the bounding box grew equally in all four directions. With top left, the top-left corner stayed at (200, 200), and the box swung down to the left as if hanging from that corner. With bottom right, the bottom-right corner (300, 300) did not move. The point you choose as the center stays in place during rotation.

Changing the scaling center changes the direction of growth

The same applies to scale(). With scale(2), the bounding box changed depending on the center as follows.

transform-origin Bounding box after scaling (left, top, right, bottom)
Not written / center (150, 150, 350, 350)
top left (200, 200, 400, 400)
top (150, 200, 350, 400)
right bottom (100, 100, 300, 300)
Two stacked diagrams. In the top one, with transform-origin: center, the enlarged light blue box grows equally in all directions, surrounding the gray dotted outline of its original position. In the bottom one, with top left, the pink dot at the top-left corner stays at its original position, and the box grows only to the right and downward.
A box scaled by scale(1.6), with transform-origin set to center and top left.

With top left, the top-left corner does not move, and the box grows only to the right and downward. If you write only top, the center is the middle of the top edge. The box grows left, right, and downward, and does not extend above the original top edge.

To open a menu that appears below a button so that it extends downward from its top edge, use this top value.

css
.menu {
  transform: scaleY(0);
  transform-origin: top; /* Extend downward without moving the top edge */
  transition: transform 0.2s;
}

.menu.is-open {
  transform: scaleY(1);
}

When I measured the bounding box at scaleY(0.5) during the opening transition, with transform-origin: top the top edge stayed at 200 and the bottom edge was at 250. Without transform-origin, the top edge was at 225 and the bottom edge at 275, so the top edge moved too. Because the center is at the vertical middle, the menu extends from both the top and bottom, and it also grows toward the button.

Writing values

transform-origin accepts keywords (left, center, right, top, bottom), percentages, and lengths. According to the specification, the keywords left and top correspond to 0%, right and bottom to 100%, and center to 50%, and positions are measured from the top-left corner of the element. These are the computed values I read in the test environment (the box is 100px square).

Value written Computed value Meaning
left 0px 50px Middle of the left edge
top 50px 0px Middle of the top edge
top left / left top 0px 0px Top-left corner
25% 75% 25px 75px 25% from the left, 75% from the top
20px 80px 20px 80px 20px from the left, 80px from the top
right 10px 100px 10px On the right edge, 10px from the top
50% top 50px 0px Middle of the top edge

If you write only one value, the other becomes center. left means the middle of the left edge, and top means the middle of the top edge. When you place two keywords side by side, top left and left top gave the same position in either order.

When you write two percentages or lengths, the first is horizontal and the second is vertical (specification description of transform-origin). When you mix keywords and numbers, write the horizontal value first and the vertical value second. right 10px and 50% top worked correctly. However, bottom 10px and 10px left, which put the vertical value first, returned false from CSS.supports(). An invalid declaration has no effect, so the computed value stayed at the initial 50px 50px. Four values, such as right 10px bottom 20px, were not valid in transform-origin either.

Percentages are based on the size including padding and border

A percentage is a fraction of the element's border box, which is the outer size including padding and border. The MDN description of transform-box says that the initial value view-box acts as border-box for HTML elements that have a CSS box.

For a box with width: 100px, padding: 20px, and border: 10px, transform-origin: 100% 100% had a computed value of 160px 160px. Since 100 + 20 × 2 + 10 × 2 = 160, this is the bottom-right corner of the outer border edge. When I added box-sizing: border-box to the same box, the outer size was 100px, so the computed value became 100px 100px.

Placing the center outside the element

Percentages can exceed 100% or be negative. 150% 50% became 150px 50px, and -50px 50% became -50px 50px, which are points outside the element.

With transform-origin: 50% 200%, the center is at (250, 400), a point one box height below the bottom edge of the box. When I rotated this box, it swung widely around that point.

rotate() Bounding box (left, top, right, bottom)
0deg (200, 200, 300, 300)
90deg (350, 350, 450, 450)
180deg (200, 500, 300, 600)
270deg (50, 350, 150, 450)

Seen from the point (250, 400), the box moved to the right at 90 degrees, directly below at 180 degrees, and to the left at 270 degrees. It travels around a circle of radius 150px centered on (250, 400).

Rotating around an end, like a clock hand

If you use one end of a long, thin element as the center, it moves like a clock hand. I rotated a bar 10px wide and 100px tall with transform-origin: bottom center.

css
.hand {
  width: 10px;
  height: 100px;
  transform-origin: bottom center; /* Use the middle of the bottom end as the axis */
  transform: rotate(90deg);
}
rotate() Bounding box (left, top, right, bottom)
0deg (200, 200, 210, 300)
90deg (205, 295, 305, 305)
180deg (200, 300, 210, 400)

At 0 degrees the bar pointed up. At 90 degrees it pointed right, and at 180 degrees it pointed down. At every angle, the middle of the bottom end (205, 300) remained the axis.

translate is not affected by transform-origin

transform-origin matters for transforms like rotation and scaling, where the result depends on the center. translate(50px, 0) produced the bounding box (250, 200, 350, 300) whether transform-origin was not written or set to top left, so it moved the same distance to the right in both cases.

The individual properties rotate and scale also transform around transform-origin. When I applied rotate: 45deg together with transform-origin: top left, the bounding box was (129.29, 200, 270.71, 341.42), the same as transform: rotate(45deg) with top left. With scale: 2, the result was (200, 200, 400, 400), the same as transform: scale(2) with top left.

For SVG shapes, also set transform-box: fill-box

<rect> and <path> elements inside SVG have different initial values from HTML elements. As the MDN description of transform-origin explains, for SVG elements other than the root <svg>, the initial value of transform-origin is 0 0. In addition, with the initial value view-box for transform-box, the reference is not the shape itself but the area of the SVG's viewBox.

I placed a <rect> with x="250" y="50" and a width and height of 100 inside an SVG with viewBox="0 0 400 400", and applied rotate(45deg).

html
<svg width="400" height="400" viewBox="0 0 400 400">
  <rect id="r" x="250" y="50" width="100" height="100" />
</svg>
CSS Bounding box after rotation (left, top, right, bottom) Computed transform-origin
transform: rotate(45deg) only (70.71, 212.13, 212.13, 353.55) 0px 0px
Adding transform-origin: center (270.71, 129.29, 412.13, 270.71) 200px 200px
Also adding transform-box: fill-box (229.29, 29.29, 370.71, 170.71) 50px 50px

The original bounding box was (250, 50, 350, 150). With transform alone, the shape rotated around the SVG's top-left point (0, 0) and moved far to the lower left. Adding transform-origin: center made the center the middle of the viewBox, (200, 200), so the shape still moved away from its original position. Adding transform-box: fill-box made the center the middle of the shape itself, so the bounding box grew equally around its original position, giving a rotation in place.

The same applies to an icon that keeps spinning. When I stopped the animation below at 45 degrees and measured, the bounding box with transform-box and transform-origin set matched the fill-box row in the table above, (229.29, 29.29, 370.71, 170.71). Without them, it was (70.71, 212.13, 212.13, 353.55), which means the shape rotated around the SVG's top-left corner.

css
.spinner-shape {
  transform-box: fill-box;   /* Use the size of the shape itself as the reference */
  transform-origin: center;  /* Rotate around its middle */
  animation: spin 1s linear infinite;
}

@keyframes spin {
  to {
    transform: rotate(360deg);
  }
}
Point

To rotate or scale a shape inside SVG in place, write transform-origin: center and transform-box: fill-box together.

The same holds for scale(2). With only transform-origin: center, and no transform-box: fill-box, the bounding box was (300, -100, 500, 100), extending above the SVG. With transform-box: fill-box added, it was (200, 0, 400, 200), growing outward in all directions from the original position.

​Posted by 86