CSS transforms

Quick reference for CSS transforms: function order, the untransformed layout box, and the new containing block that transform creates.

参考页正文目前为英文。

transform paints an element in a different place, shape, or orientation without changing how it is laid out in normal flow. The layout box stays where it was; overflowing pixels can overlap neighbours, but those neighbours do not move to make room. That is the difference between a visual transform and a margin or a relative offset that participates in flow.

The Position page already uses transform as an example of an ancestor that establishes a containing block for fixed or absolute descendants. This page starts from the transform functions themselves and points back there for sticky, inset, and absolute containing-block rules rather than repeating them. A non-none transform also creates a stacking context; that role is on the stacking context page.

Examples are complete HTML and CSS fragments. They were parsed with PostCSS and a selector AST parser for syntax and structure. They were not rendered in a browser here, and this is not a visual layout test.

Official sources: MDN and the CSS specification. Fragments were checked with PostCSS 8 for syntax and structure. That is not a CSS conformance suite, and this is not a browser render or visual test.

CSS reference catalog → · CSS tutorial overview →

Function order and the layout box

A transform list such as translateX(3rem) rotate(45deg) is applied from the right: the right-most function runs first in the local coordinate system. So translateX(3rem) rotate(45deg) rotates first, then translates; rotate(45deg) translateX(3rem) translates along an already rotated axis. Swapping the two functions is not a no-op.

The used layout size and position in normal flow are those of the untransformed box. Siblings laid out after it still start from that original space. Individual translate, rotate, and scale properties from Transforms Level 2 can be set separately and compose with the transform list; this page uses the transform list, which is the common source of order mistakes.

Syntax or propertyInitial / defaultInheritedApplies toNotes
transformnonenotransformable elementsA list of transform functions, or none. none does not transform the element and does not, by itself, create the containing block or stacking context described below.
transform-origin50% 50%notransformable elementsThe origin for rotation and scaling. It does not change the fact that flow layout still uses the untransformed box.

Order matters; the flow box does not move

.rotated-first { transform: translateX(3rem) rotate(45deg); }
.translated-first { transform: rotate(45deg) translateX(3rem); }

The two lists swap function order, so they are not the same transform. Both elements still occupy their untransformed layout boxes in normal flow.

  • The explanation describes the specified transform list and the normal-flow rule. It is not a screenshot of the two boxes.
html
<div class="stage">
  <div class="box rotated-first">rotate first</div>
  <div class="box translated-first">translate first</div>
</div>
<style>
.stage { width: 16rem; font-family: sans-serif; }
.box { width: 4rem; height: 4rem; margin-bottom: 2rem; background: #F3EFE4; border: 1px solid #0B5853; }
.rotated-first { transform: translateX(3rem) rotate(45deg); }
.translated-first { transform: rotate(45deg) translateX(3rem); }
</style>

What this markup means

rotated-first applies rotate(45deg) first, then translateX(3rem) in the parent’s horizontal axis. translated-first applies translateX(3rem) first along the element’s x-axis, then rotates the result, so the translation moves diagonally. The margin-bottom: 2rem still collapses or separates the untransformed 4rem boxes; the visual rotation does not shrink that layout gap. Neighbours in normal flow are not reflowed to follow the painted square.

Related: Position (containing blocks), Box model

MDN documentation → · CSS specification →

A non-none transform creates a containing block

If transform is not none, the element becomes a containing block for fixed and absolutely positioned descendants, even when its own position is static. The Position page is the place to read how inset is measured and why sticky can appear to fail; the surprise this page records is that a transform, like some filter or perspective values, is enough to create that containing block.

The same non-none transform also creates a stacking context. A containing block and a stacking context are different roles: one answers “offsets are measured against what?”, the other answers “in what local list is z-index compared?”. A property can establish both without making the two ideas the same.

Syntax or propertyInitial / defaultNotes
transform: none vs a function listnonenone keeps the usual fixed-position containing block (typically the viewport). Any function list, including a 0px translation, is not none and does establish the new containing block.

A transformed ancestor can contain a fixed descendant

.stage { transform: translateX(0); }
.pin { position: fixed; inset: 0 auto auto 0; }

The ancestor is statically positioned but has a transform other than none, so it is the containing block for the fixed child. This is not a second Position tutorial.

  • translateX(0) is a deliberate non-none value. It exists to show the containing-block trigger, not to move the box.
  • Inset and sticky overflow rules stay on the Position page.
html
<section class="stage">
  <div class="pin">Pinned inside the transformed section</div>
</section>
<style>
.stage { transform: translateX(0); background: #F3EFE4; padding: 1rem; font-family: sans-serif; }
.pin { position: fixed; inset: 0 auto auto 0; color: #0B5853; }
</style>

What this markup means

The section is position: static by default, but transform: translateX(0) is not none, so it establishes a containing block. The child’s position: fixed is then located against that section, not against the viewport. The zero translation is enough for the rule to apply. For how inset, sticky, and overflow interact with containing blocks, use the Position page; this example only records the transform trigger.

Related: Position: absolute containing blocks, Stacking context

MDN documentation → · CSS specification →