CSS transform-box Property
The CSS transform-box property defines the reference box used to calculate an element's CSS transformations.
It determines which box provides the dimensions and coordinate system for properties such as transform and transform-origin. The property is especially useful when transforming SVG graphics, where an element's painted bounds and the SVG viewport can differ significantly.
transform-box Syntax
The transform-box property accepts a single keyword that identifies the reference box used for transformations.
Basic Syntax
.example {
transform-box: border-box;
}
Available Values
.example {
transform-box: content-box;
}
.example {
transform-box: border-box;
}
.example {
transform-box: fill-box;
}
.example {
transform-box: stroke-box;
}
.example {
transform-box: view-box;
}
CSS-Wide Keywords
.example {
transform-box: initial;
}
.example {
transform-box: inherit;
}
.example {
transform-box: unset;
}
.example {
transform-box: revert;
}
.example {
transform-box: revert-layer;
}
Invalid Examples
.example {
transform-box: padding-box;
}
.example {
transform-box: margin-box;
}
.example {
transform-box: 100px;
}
.example {
transform-box: border-box fill-box;
}
The property does not accept lengths, percentages, multiple box keywords, or the padding-box and margin-box keywords.
Property Values
| Value | Description |
|---|---|
content-box |
Uses the element's content box as the transformation reference box. |
border-box |
Uses the element's border box as the transformation reference box. |
fill-box |
Uses the object's bounding box as the reference box, typically the fill bounds of an SVG element. |
stroke-box |
Uses the stroke bounding box as the reference box, including the bounds of an SVG stroke. |
view-box |
Uses the nearest SVG viewport as the reference box, accounting for its viewBox when one is specified. |
Property Characteristics
| Characteristic | Value |
|---|---|
| Initial value | view-box |
| Inherited | No |
| Applies to | Transformable elements |
| Computed value | As specified |
| Animation type | Discrete |
| Percentages | Not accepted as property values |
| Specification | CSS Transforms Module Level 1 |
Note: Although the initial value is view-box, elements with an associated CSS layout box generally use their border box when view-box is specified. The distinction is especially important for SVG elements without an associated CSS layout box.
How It Works
The transform-box property selects the box that the browser uses when calculating certain transformation measurements.
Step 1: Create an Element
<div class="transform-target">Transform Me</div>
Step 2: Define Its Dimensions
.transform-target {
width: 160px;
padding: 20px;
border: 5px solid #2563eb;
}
Step 3: Select a Reference Box
.transform-target {
transform-box: content-box;
}
The element's content box becomes the reference box for applicable transformation calculations.
Step 4: Apply a Transformation
.transform-target {
transform-box: content-box;
transform: translateX(50%);
}
The percentage translation is calculated using the width of the selected content box.
Step 5: Change the Reference Box
.transform-target {
transform-box: border-box;
transform: translateX(50%);
}
The percentage translation now uses the border box, which includes padding and borders.
What Changes?
The selected reference box can change the result of percentage-based translations and percentage-based transform origins. It does not change the element's actual width, padding, border, or position in normal document flow.
The content-box Value
The content-box value uses the element's content box as the reference for transformations.
Basic Example
.example {
transform-box: content-box;
}
Percentage Translation
.example {
width: 200px;
padding: 20px;
transform-box: content-box;
transform: translateX(50%);
}
With a 200-pixel content box, the horizontal translation is 100 pixels.
Using transform-origin
.example {
transform-box: content-box;
transform-origin: 0% 0%;
transform: rotate(20deg);
}
The transformation origin is positioned at the top-left corner of the content box.
Padding and Borders
The content box excludes padding and borders. This can produce a different transformation result from using the border box.
When to Use content-box
Use this value when transformations should be calculated relative to the content area rather than the element's outer border dimensions.
The border-box Value
The border-box value uses the element's border box as the reference for transformations.
Basic Example
.example {
transform-box: border-box;
}
Percentage Translation
.example {
width: 200px;
padding: 20px;
border: 5px solid #2563eb;
transform-box: border-box;
transform: translateX(50%);
}
With the default content-box sizing model, the border box is 250 pixels wide. A 50% translation therefore moves the element 125 pixels horizontally.
Rotation Around a Corner
.example {
transform-box: border-box;
transform-origin: top left;
transform: rotate(20deg);
}
Scaling Around the Center
.example {
transform-box: border-box;
transform-origin: center;
transform: scale(1.2);
}
When to Use border-box
Use border-box when transformations should be based on the element's outer border dimensions.
Important Distinction
The transform-box: border-box declaration does not change the CSS box-sizing model. It only selects the transformation reference box.
The fill-box Value
The fill-box value uses an object's bounding box as the transformation reference box. It is particularly useful for SVG graphics.
Basic Example
svg .shape {
transform-box: fill-box;
}
SVG Rotation
svg .shape {
transform-box: fill-box;
transform-origin: center;
transform: rotate(30deg);
}
The shape rotates around the center of its own bounding box rather than the center of the SVG viewport.
SVG Translation
svg .shape {
transform-box: fill-box;
transform: translateX(50%);
}
The percentage translation is calculated relative to the shape's bounding box.
Why It Matters for SVG
An SVG element may occupy only a small portion of a larger SVG viewport. Using fill-box makes transformations relative to the individual graphic.
When to Use fill-box
Use this value when an SVG shape should rotate, scale, or move relative to its own geometric bounds.
The stroke-box Value
The stroke-box value uses the stroke bounding box as the reference for transformations.
Basic Example
svg .outlined-shape {
transform-box: stroke-box;
}
Rotation With a Stroke
svg .outlined-shape {
stroke: #2563eb;
stroke-width: 12;
transform-box: stroke-box;
transform-origin: center;
transform: rotate(30deg);
}
Stroke Bounds
Unlike fill-box, the stroke bounding box accounts for the extent of the stroke. This can change the reference dimensions and transformation origin.
Percentage Translation
svg .outlined-shape {
transform-box: stroke-box;
transform: translateX(50%);
}
The translation is calculated relative to the stroke bounding box.
When to Use stroke-box
Use this value when the visible stroke should be included in the transformation reference dimensions.
Important Consideration
The difference between fill-box and stroke-box is most noticeable when an SVG shape has a thick or asymmetrical stroke.
The view-box Value
The view-box value uses the nearest SVG viewport as the transformation reference box. When the SVG viewport has a viewBox attribute, the reference box uses that coordinate system.
Basic Example
svg .shape {
transform-box: view-box;
}
SVG Viewport Example
<svg viewBox="0 0 300 200" xmlns="http://www.w3.org/2000/svg">
<rect class="shape" x="50" y="50" width="80" height="60" />
</svg>
Rotating Around the Viewport Center
svg .shape {
transform-box: view-box;
transform-origin: center;
transform: rotate(30deg);
}
The rotation origin is based on the SVG viewport rather than the individual rectangle's bounding box.
Percentage Translation
svg .shape {
transform-box: view-box;
transform: translateX(50%);
}
For an SVG with a viewBox width of 300 units, a 50% horizontal translation corresponds to 150 units in that coordinate system.
Default Behavior
The initial value of transform-box is view-box. For SVG graphics without an associated CSS layout box, this commonly means transformations use the SVG viewport by default.
When to Use view-box
Use this value when an SVG graphic should transform relative to the overall SVG coordinate system rather than its own bounds.
Using transform-box With HTML
For ordinary HTML elements, transform-box can select the content box or border box as the reference for transformations.
HTML Example
<div class="box">Example Box</div>
Content Box Reference
.box {
width: 180px;
padding: 20px;
border: 5px solid #2563eb;
transform-box: content-box;
transform: translateX(50%);
}
Border Box Reference
.box {
width: 180px;
padding: 20px;
border: 5px solid #2563eb;
transform-box: border-box;
transform: translateX(50%);
}
Comparing the Results
With the default box-sizing: content-box, the content width is 180 pixels and the border-box width is 230 pixels. A 50% translation therefore produces a 90-pixel movement with content-box and a 115-pixel movement with border-box.
HTML Reference Box Mapping
For elements with an associated CSS layout box, fill-box behaves as content-box, while stroke-box and view-box behave as border-box.
Important Consideration
The selected reference box affects transformation calculations. It does not change the actual dimensions of the element or its position in normal document flow.
Using transform-box With SVG
SVG graphics often have multiple meaningful coordinate systems. The transform-box property allows transformations to use the individual graphic's bounds or the overall SVG viewport.
SVG Example
<svg viewBox="0 0 300 200" xmlns="http://www.w3.org/2000/svg">
<rect class="svg-shape" x="40" y="60" width="100" height="70" fill="#93c5fd" />
</svg>
Using fill-box
.svg-shape {
transform-box: fill-box;
transform-origin: center;
transform: rotate(30deg);
}
The rectangle rotates around the center of its own bounding box.
Using view-box
.svg-shape {
transform-box: view-box;
transform-origin: center;
transform: rotate(30deg);
}
The rectangle rotates around the center of the SVG viewport.
Using stroke-box
.svg-shape {
stroke: #1d4ed8;
stroke-width: 10;
transform-box: stroke-box;
transform-origin: center;
transform: rotate(30deg);
}
The stroke bounding box provides the transformation reference dimensions.
Why SVG Behaves Differently
Unlike ordinary HTML boxes, SVG graphics may not have an associated CSS layout box. Their fill bounds, stroke bounds, and viewport dimensions can all differ.
When to Use SVG Reference Boxes
Use fill-box for transformations relative to an individual shape, stroke-box when stroke dimensions matter, and view-box for transformations relative to the SVG viewport.
Working With transform-origin
The transform-origin property specifies the point around which transformations occur. The transform-box property determines the reference box used to resolve that point.
Center of the Border Box
.example {
transform-box: border-box;
transform-origin: center;
transform: rotate(30deg);
}
Top-Left of the Content Box
.example {
transform-box: content-box;
transform-origin: top left;
transform: rotate(30deg);
}
Center of an SVG Shape
svg .shape {
transform-box: fill-box;
transform-origin: center;
transform: rotate(30deg);
}
Center of the SVG Viewport
svg .shape {
transform-box: view-box;
transform-origin: center;
transform: rotate(30deg);
}
Percentage Origin
.example {
transform-box: content-box;
transform-origin: 25% 75%;
transform: rotate(20deg);
}
The percentages are resolved relative to the selected reference box.
Important Distinction
transform-origin selects a point, while transform-box selects the box used to calculate that point.
Percentage Translations
Percentage-based translations use the dimensions of the selected transformation reference box.
Horizontal Translation
.example {
transform-box: content-box;
transform: translateX(50%);
}
The horizontal translation is 50% of the content box's width.
Vertical Translation
.example {
transform-box: border-box;
transform: translateY(50%);
}
The vertical translation is 50% of the border box's height.
Two-Axis Translation
.example {
transform-box: border-box;
transform: translate(50%, 25%);
}
SVG Translation
svg .shape {
transform-box: fill-box;
transform: translateX(100%);
}
The SVG shape moves horizontally by the width of its own bounding box.
Changing the Reference Box
.example {
transform-box: border-box;
transform: translateX(100%);
}
Using border-box instead of content-box can increase the translation distance when the element has padding or borders.
Important Consideration
Percentage translations are not calculated relative to the parent element's dimensions. They use the transformed element's selected reference box.
Related CSS Properties
The transform-box property works with several CSS properties that control transformations and their reference coordinates.
transform
.example {
transform: rotate(30deg);
}
Applies translation, rotation, scaling, skewing, or other transformations.
transform-origin
.example {
transform-origin: center;
}
Defines the point around which transformations occur.
transform-style
.example {
transform-style: preserve-3d;
}
Controls whether descendants retain their three-dimensional positioning.
translate
.example {
translate: 50% 0;
}
Provides an individual translation property that can use the selected reference box for percentage translations.
rotate
.example {
rotate: 30deg;
}
Provides an individual rotation property.
scale
.example {
scale: 1.2;
}
Provides an individual scaling property.
box-sizing
.example {
box-sizing: border-box;
}
Controls how the CSS width and height properties are calculated. It does not select the transformation reference box.
perspective
.example {
perspective: 800px;
}
Applies perspective to transformed descendants.
Complete Example
.example {
transform-box: border-box;
transform-origin: center;
transform: translateX(20%) rotate(15deg);
}
Inheritance
The transform-box property is not inherited. A child element does not automatically receive the parent's specified reference box.
Parent Element Example
.parent {
transform-box: content-box;
}
Child Element
.child {
transform-box: view-box;
}
The child uses its own specified reference box.
Explicit Inheritance
.child {
transform-box: inherit;
}
The child uses the parent's computed transform-box value.
Restore the Initial Value
.child {
transform-box: initial;
}
This restores view-box.
Using unset
.child {
transform-box: unset;
}
Because the property is not inherited, unset restores the initial value.
Important Consideration
Nested transformations can still affect how child elements appear, even though their transform-box values are calculated independently.
Practical Examples
Content-Based Translation
.content-shift {
transform-box: content-box;
transform: translateX(50%);
}
Border-Based Translation
.border-shift {
transform-box: border-box;
transform: translateX(50%);
}
Rotating Around the Content Box
.rotating-box {
transform-box: content-box;
transform-origin: center;
transform: rotate(20deg);
}
Rotating an SVG Icon
svg .icon-shape {
transform-box: fill-box;
transform-origin: center;
transform: rotate(45deg);
}
SVG Stroke-Based Rotation
svg .outlined-icon {
transform-box: stroke-box;
transform-origin: center;
transform: rotate(30deg);
}
SVG Viewport Rotation
svg .orbiting-shape {
transform-box: view-box;
transform-origin: center;
transform: rotate(45deg);
}
SVG Shape Translation
svg .moving-shape {
transform-box: fill-box;
transform: translateX(100%);
}
Animated SVG Rotation
svg .spinner {
transform-box: fill-box;
transform-origin: center;
animation: svg-spin 2s linear infinite;
}
@keyframes svg-spin {
from {
transform: rotate(0deg);
}
to {
transform: rotate(360deg);
}
}
Reduced Motion Alternative
@media (prefers-reduced-motion: reduce) {
svg .spinner {
animation: none;
}
}
Progressive Enhancement
@supports (transform-box: fill-box) {
svg .shape {
transform-box: fill-box;
transform-origin: center;
}
}
transform-box Example
This example compares content-box and border-box using two HTML elements with identical dimensions, padding, and borders. Both elements use translateX(50%), but the selected reference box changes the translation distance.
HTML Example
<div class="transform-box-demo">
<div class="transform-box-sample">
<strong>transform-box: content-box;</strong>
<div class="transform-box-stage">
<div class="transform-box-target content-reference">Content</div>
</div>
</div>
<div class="transform-box-sample">
<strong>transform-box: border-box;</strong>
<div class="transform-box-stage">
<div class="transform-box-target border-reference">Border</div>
</div>
</div>
</div>
CSS Example
.transform-box-demo {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(min(100%, 220px), 1fr));
gap: 16px;
}
.transform-box-sample strong {
display: block;
margin-bottom: 8px;
}
.transform-box-stage {
display: flex;
align-items: center;
justify-content: flex-start;
min-height: 130px;
padding: 12px;
overflow: hidden;
border: 1px dashed #94a3b8;
background: #f8fafc;
}
.transform-box-target {
box-sizing: content-box;
display: flex;
align-items: center;
justify-content: center;
width: 80px;
height: 40px;
padding: 10px;
border: 5px solid #2563eb;
background: #dbeafe;
color: #1e40af;
font-weight: bold;
font-size: 13px;
transform: translateX(50%);
}
.content-reference {
transform-box: content-box;
}
.border-reference {
transform-box: border-box;
}
Understanding the Results
Both elements have a content width of 80 pixels, 10 pixels of padding on each side, and 5-pixel borders.
content-box Result
The content box is 80 pixels wide. A 50% horizontal translation moves the element 40 pixels.
border-box Result
The border box is 110 pixels wide. A 50% horizontal translation moves the element 55 pixels.
Why the Results Differ
The transform-box property changes the dimensions used to resolve the percentage translation. The elements retain the same actual layout dimensions.
Experiment With Another Value
.content-reference {
transform-box: border-box;
}
Changing the first element to border-box makes both examples use the same reference dimensions.
Experiment With transform-origin
.content-reference {
transform-box: content-box;
transform-origin: top left;
transform: rotate(15deg);
}
This changes the example from a percentage translation to a rotation around the top-left corner of the content box.
Accessibility Considerations
The transform-box property does not directly change semantic meaning or keyboard behavior, but the transformations it controls can affect how content is presented and used.
Preserve Readability
Ensure that transformations do not make text difficult to read or move important information outside the visible area.
Avoid Unexpected Overlap
Transforms can visually overlap nearby content without changing normal document flow.
Respect Reduced Motion Preferences
Animated transformations should account for users who prefer reduced motion.
@media (prefers-reduced-motion: reduce) {
.animated-shape {
animation: none;
transition: none;
}
}
Maintain Keyboard Accessibility
Transformed interactive elements should remain visible and reachable through keyboard navigation.
Accessible SVG Graphics
Meaningful SVG graphics should provide appropriate accessible names or text alternatives. Decorative SVG graphics should not introduce unnecessary announcements.
Test at Different Zoom Levels
Verify that transformed content remains usable when browser zoom or text size increases.
Avoid Motion-Only Information
Do not rely on transformation animations as the only way to communicate important information.
Common Mistakes
Using Unsupported Box Keywords
.example {
transform-box: padding-box;
}
The padding-box keyword is not a valid value for transform-box.
Using margin-box
.example {
transform-box: margin-box;
}
The margin-box keyword is not supported by this property.
Using Multiple Values
.example {
transform-box: content-box border-box;
}
The property accepts only one reference-box keyword.
Using Lengths or Percentages
.example {
transform-box: 50%;
}
Lengths and percentages are not valid property values.
Confusing transform-box With box-sizing
The box-sizing property controls how width and height are calculated. The transform-box property selects a reference box for transformations.
Expecting transform-box to Apply a Transformation
The property selects a reference box but does not rotate, scale, or translate an element by itself.
Ignoring transform-origin
Changing the reference box can affect the position of the transformation origin, particularly when percentages or keywords are used.
Assuming SVG and HTML Behave Identically
SVG graphics can use fill bounds, stroke bounds, and viewport coordinates, while ordinary HTML elements generally use CSS layout boxes.
Assuming view-box Always Means the SVG Viewport
For elements with an associated CSS layout box, view-box generally behaves as border-box.
Ignoring Stroke Dimensions
Using fill-box instead of stroke-box may produce different results when an SVG element has a thick stroke.
Expecting Percentage Translations to Use the Parent Width
Percentage translations use the selected transformation reference box, not the parent element's dimensions.
Transforming Non-Transformable Elements
Ordinary non-replaced inline elements are not transformable. Change their display behavior when necessary.
.inline-icon {
display: inline-block;
transform-box: border-box;
transform: rotate(20deg);
}
Ignoring Browser Compatibility
Some reference-box values, especially those used with SVG, may require additional browser testing.
Important Considerations
Keep the following points in mind when using transform-box:
- The property selects the reference box used for transformations.
- The initial value is
view-box. - The property is not inherited.
- The property applies to transformable elements.
- The
content-boxvalue uses the content box. - The
border-boxvalue uses the border box. - The
fill-boxvalue uses the object's bounding box. - The
stroke-boxvalue uses the stroke bounding box. - The
view-boxvalue uses the SVG viewport reference box when applicable. - For elements with a CSS layout box,
fill-boxbehaves ascontent-box. - For elements with a CSS layout box,
stroke-boxandview-boxbehave asborder-box. - The property affects percentage-based translation calculations.
- The property affects how percentage-based transform origins are resolved.
- The property does not apply a transformation by itself.
- The property does not change the element's actual layout dimensions.
- The property does not change the CSS box-sizing model.
- SVG graphics may have different fill, stroke, and viewport bounds.
- SVG transformations can produce noticeably different results depending on the reference box.
- The
transform-originproperty works closely withtransform-box. - Percentage translations are relative to the selected reference box.
- Only one reference-box keyword is allowed per declaration.
- The property does not accept lengths or percentages as values.
- Animations between different
transform-boxvalues use discrete interpolation. - Test SVG transformations across supported browsers.
- Respect reduced motion preferences when animating transformed elements.
Browser Support
The CSS transform-box property is supported in modern browsers. Support for individual reference-box values and their behavior with SVG graphics has varied across browser versions, so compatibility should be checked when using advanced SVG transformations.
For current compatibility information, visit Can I Use? and search for transform-box.
Basic Declaration
.example {
transform-box: border-box;
}
SVG Declaration
svg .shape {
transform-box: fill-box;
transform-origin: center;
transform: rotate(30deg);
}
Feature Detection
@supports (transform-box: fill-box) {
svg .shape {
transform-box: fill-box;
transform-origin: center;
}
}
Fallback Behavior
If a browser does not recognize a transform-box declaration, it ignores that declaration and uses the value established by the cascade, typically the initial value view-box.
Testing Recommendations
Test percentage translations, SVG rotations, and transform origins in the browsers your website supports. Feature detection can confirm that a value is recognized, but actual rendering tests are still important.
Specifications
The transform-box property is defined in the CSS Transforms specification.
CSS Transforms Module Level 1 - Transform Reference Box
The specification defines the available reference-box keywords and how they affect transformation calculations for CSS layout boxes and SVG graphics.
For additional documentation and examples, consult MDN Web Docs.
