Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

matrix(): a two-dimensional affine transform

Combine scaling, skewing, rotation and translation in six matrix coefficients.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

transform: matrix(2, 0, 0, 3, 10, 20); transform-origin: 0 0;

matrix(a, b, c, d, e, f) represents a two-dimensional affine transformation. Ignoring transform-origin for the moment, a point maps to x′ = a·x + c·y + e and y′ = b·x + d·y + f. The first four coefficients describe the linear part; e and f are translations expressed as unitless CSS-pixel numbers. Their positions matter: swapping b and c generally changes the transformation.

The transform acts around transform-origin, so the same six numbers can place a box differently when its origin changes. CSS transforms affect visual geometry without rewriting the normal layout width and height. A transformed bounding rectangle is therefore not interchangeable with an element's untransformed layout dimensions. Multiple transforms also depend on their composition order.

The example uses origin 0 0 and matrix(2, 0, 0, 3, 10, 20) on a 40 by 30 pixel box. It moves the top-left corner by 10 and 20 pixels and produces an 80 by 90 rendered rectangle, while the layout box remains 40 by 30. Keeping skew and rotation at zero makes those measurements easy to interpret.

Examples

const element=document.createElement('div');element.style.cssText='position:fixed;left:0;top:0;width:40px;height:30px;transform-origin:0 0;transform:matrix(2,0,0,3,10,20)';document.body.append(element);
const rect=element.getBoundingClientRect();
console.log('position='+rect.x+','+rect.y+'; rendered='+rect.width+'x'+rect.height+'; layout='+element.offsetWidth+'x'+element.offsetHeight+'; transform='+getComputedStyle(element).transform);

Result

position=10,20; rendered=80x90; layout=40x30; transform=matrix(2, 0, 0, 3, 10, 20)

Rendered dimensions and layout dimensions intentionally differ; the translation is not a margin change.

Sources