CSS Reference ยท Transforms, Transitions and Animations
The transform-box property
Reference for the CSS transform-box property: choose which box percentages and transform-origin refer to, and fix SVG shapes that rotate around the wrong point.
The transform-box property chooses the reference box that transform-origin and percentage values in transforms are measured against. For ordinary HTML elements the answer is always the border box, so you rarely touch it there. Its main job is to fix transforms on inline SVG shapes.
Inside an SVG, CSS transforms by default measure from the top-left corner of the SVG viewport, not of the shape. A rotated <rect> would swing around that far-away corner. Setting transform-box: fill-box makes the shape's own bounding box the reference, so transform-origin: center behaves as you expect.
svg .gear { transform-box: fill-box; transform-origin: center; }
Try it
In the first drawing the square swings around the SVG's top-left corner and ends up away from its dashed outline (the default origin there is 0 0). In the second it turns in place.
Syntax
transform-box: content-box | border-box | fill-box | stroke-box | view-box;
- Initial value:
view-box - Applies to: transformable elements
- Inherited: no
- Animatable: no (discrete)
- Computed value: as specified
Values
| Value | Description |
|---|---|
view-box |
The nearest SVG viewport (the viewBox); for non-SVG elements it uses the border box. |
fill-box |
The object bounding box of the SVG shape (its geometry, without stroke). |
stroke-box |
The shape's bounding box including its stroke. |
content-box |
The content box of the element (for CSS boxes). |
border-box |
The border box of the element (for CSS boxes). |
Usage notes
- Use
fill-boxwithtransform-origin: centerfor spinning icons and logo parts drawn in SVG. - No effect on most HTML. For layout boxes
view-boxbehaves as the border box, so HTML transforms already pivot around the element. - Percentages follow the box. After switching to
fill-box,50% 50%means the middle of the shape, which is why it is so convenient in animations. - SVG attribute transforms (
transform="rotate(30 100 100)") have their own coordinate rules and are not affected by this property's origin in the same way.
Browser support
Supported in current versions of all major browsers. Older browsers do not know it, and SVG origin behavior there is inconsistent; check a compatibility table for legacy targets.
Related
transform-origin,transformrotate- Lesson: 2D Transforms