1. Introduction
This section is not normative.
The CSS visual formatting model describes a coordinate system within each element is positioned. Positions and sizes in this coordinate space can be thought of as being expressed in pixels, starting in the origin of point with positive values proceeding to the right and down.
This coordinate space can be modified with the transform property. Using transform, elements can be translated, rotated and scaled.
1.1. Module Interactions
This module defines a set of CSS properties that affect the visual rendering of elements to which those properties are applied; these effects are applied after elements have been sized and positioned according to the visual formatting model from [CSS2]. Some values of these properties result in the creation of a containing block, and/or the creation of a stacking context.
Transforms affect the rendering of backgrounds on elements with a value of fixed for the background-attachment property, which is specified in [CSS3BG].
Transforms affect the client rectangles returned by the Element Interface Extensions getClientRects() and getBoundingClientRect(), which are specified in [CSSOM-VIEW].
Transforms affect the computation of the scrollable overflow region as described by [CSS-OVERFLOW-3].
1.2. CSS Values
This specification follows the CSS property definition conventions from [CSS2] using the value definition syntax from [CSS-VALUES-3]. Value types not defined in this specification are defined in CSS Values & Units [CSS-VALUES-3]. Combination with other CSS modules may expand the definitions of these value types.
In addition to the property-specific values listed in their definitions, all properties defined in this specification also accept the CSS-wide keywords as their property value. For readability they have not been repeated explicitly. Terminology {#terminology}
==========================When used in this specification, terms have the meanings assigned in this section.
- transformable element
-
A transformable element is an element in one of these categories:
-
all elements whose layout is governed by the CSS box model except for non-replaced inline boxes, table-column boxes, and table-column-group boxes [CSS2],
-
all SVG paint server elements, the clipPath element and SVG renderable elements with the exception of any descendant element of text content elements [SVG2].
-
- transformed element
-
An element with a computed value other than none for the transform property.
- user coordinate system
- local coordinate system
-
In general, a coordinate system defines locations and distances on the current canvas. The current local coordinate system (also user coordinate system) is the coordinate system that is currently active and which is used to define how coordinates and lengths are located and computed, respectively, on the current canvas. The current user coordinate system has its origin at the top-left of a reference box specified by the transform-box property. Percentage values are relative to the dimension of this reference box. One unit equals one CSS pixel.
- transformation matrix
-
A matrix that defines the mathematical mapping from one coordinate system into another. It is computed from the values of the transform and transform-origin properties as described below.
- current transformation matrix (CTM)
-
A matrix that defines the mapping from the local coordinate system into the viewport coordinate system.
- 2D matrix
-
A 3x2 transformation matrix, or a 4x4 matrix where the items m31, m32, m13, m23, m43, m14, m24, m34 are equal to 0 and m33, m44 are equal to 1.
- identity transform function
-
A transform function that is equivalent to a identity 4x4 matrix (see Mathematical Description of Transform Functions). Examples for identity transform functions are translate(0), translateX(0), translateY(0), scale(1), scaleX(1), scaleY(1), rotate(0), skew(0, 0), skewX(0), skewY(0) and matrix(1, 0, 0, 1, 0, 0).
- post-multiply
- post-multiplied
-
Term A post-multiplied by term B is equal to A · B.
- pre-multiply
- pre-multiplied
-
Term A pre-multiplied by term B is equal to B · A.
- multiply
-
Multiply term A by term B is equal to A · B.
2. The Transform Rendering Model
This section is normative.
Specifying a value other than none for the transform property establishes a new local coordinate system at the element that it is applied to. The mapping from where the element would have rendered into that local coordinate system is given by the element’s transformation matrix.
The transformation matrix is computed from the transform and transform-origin properties as follows:
-
Start with the identity matrix.
-
Translate by the computed X and Y of transform-origin
-
Multiply by each of the transform functions in transform property from left to right
-
Translate by the negated computed X and Y values of transform-origin
An element has a transform property that is not none.
div {
transform-origin : 0 0 ;
transform : translate ( -10 px , -20 px ) scale ( 2 ) rotate ( 45 deg );
}
The transform-origin property is set to 0 0 and can be omitted. The transformation matrix TM gets computed by post-multiplying the <translate()>, <scale()> and <rotate()> <transform-function>s.
Transforms apply to transformable elements.
The coordinate space is a coordinate system with two axes: the X axis increases horizontally to the right; the Y axis increases vertically downwards.
Transformations are cumulative. That is, elements establish their local coordinate system within the coordinate system of their parent.
To map a point plocal with the coordinate pair xlocal and ylocal from the local coordinate system of an element into the parent’s coordinate system, post-multiply the transformation matrix TM of the element by plocal. The result is the mapped point pparent with the coordinate pair xparent and yparent in the parent’s local coordinate system.
From the perspective of the user, an element effectively accumulates all the transform properties of its ancestors as well as any local transform applied to it. The accumulation of these transforms defines a current transformation matrix (CTM) for the element.
The current transformation matrix is computed by post-multiplying all transformation matrices starting from the viewport coordinate system and ending with the transformation matrix of an element.
< svg xmlns = "http://www.w3.org/2000/svg" >
< g transform = "translate(-10, 20)" >
< g transform = "scale(2)" >
< rect width = "200" height = "200" transform = "rotate(45)" />
</ g >
</ g >
</ svg >
-
translate(-10, 20) computes to the transformation matrix T1
-
scale(2) computes to the transformation matrix T2
-
rotate(45) computes to the transformation matrix T3
The CTM for the SVG rect element is the result of multiplying T1, T2 and T3 in order.
To map a point plocal with the coordinate pair xlocal and ylocal from the local coordinate system of the SVG rect element into the viewport coordinate system, post-multiply the current transformation matrix CTM of the element by plocal. The result is the mapped point pviewport with the coordinate pair xviewport and yviewport in the viewport coordinate system.
Note: Transformations do affect the visual rendering, but have no effect on the CSS layout other than affecting overflow. Transforms are also taken into account when computing client rectangles exposed via the Element Interface Extensions, namely getClientRects() and getBoundingClientRect(), which are specified in [CSSOM-VIEW].
div {
transform : translate ( 100 px , 100 px );
}
This transform moves the element by 100 pixels in both the X and Y directions.
div {
height : 100 px ; width : 100 px ;
transform-origin : 50 px 50 px ;
transform : rotate ( 45 deg );
}
The transform-origin property moves the point of origin by 50 pixels in both the X and Y directions. The transform rotates the element clockwise by 45° about the point of origin. After all transform functions were applied, the translation of the origin gets translated back by -50 pixels in both the X and Y directions.
div {
height : 100 px ; width : 100 px ;
transform : translate ( 80 px , 80 px ) scale ( 1.5 , 1.5 ) rotate ( 45 deg );
}
The visual appearance is as if the div element gets translated by 80px to the bottom left direction, then scaled up by 150% and finally rotated by 45°.
Each <transform-function> can get represented by a corresponding 4x4 matrix. To map a point from the coordinate space of the div box to the coordinate space of the parent element, these transforms get multiplied in the reverse order:
-
The rotation matrix gets post-multiplied by the scale matrix.
-
The result of the previous multiplication is then post-multiplied by the translation matrix to create the accumulated transformation matrix.
-
Finally, the point to map gets pre-multiplied with the accumulated transformation matrix.
For more details see The Transform Function Lists.
Note: The identical rendering can be obtained by nesting elements with the equivalent transforms:
< div style = "transform: translate(80px, 80px)" >
< div style = "transform: scale(1.5, 1.5)" >
< div style = "transform: rotate(45deg)" ></ div >
</ div >
</ div >
For elements whose layout is governed by the CSS box model, the transform property does not affect the flow of the content surrounding the transformed element. However, the extent of the overflow area takes into account transformed elements. This behavior is similar to what happens when elements are offset via relative positioning. Therefore, if the value of the overflow property is scroll or auto, scrollbars will appear as needed to see content that is transformed outside the visible area. Specifically, transforms can extend (but do not shrink) the size of the overflow area, which is computed as the union of the bounds of the elements before and after the application of transforms.
For elements whose layout is governed by the CSS box model, any value other than none for the transform property results in the creation of a stacking context. Implementations must paint the layer it creates, within its parent stacking context, at the same stacking order that would be used if it were a positioned element with z-index: 0. If an element with a transform is positioned, the z-index property applies as described in [CSS2], except that auto is treated as 0 since a new stacking context is always created.
For elements whose layout is governed by the CSS box model, any value other than none for the transform property also causes the element to establish a containing block for all descendants. Its padding box will be used to layout for all of its absolute-position descendants, fixed-position descendants, and descendant fixed background attachments.
< style >
# container {
width : 300 px ;
height : 200 px ;
border : 5 px dashed black ;
padding : 5 px ;
overflow : scroll ;
}
# bloat {
height : 1000 px ;
}
# child {
right : 0 ;
bottom : 0 ;
width : 10 % ;
height : 10 % ;
background : green ;
}
</ style >
< div id = "container" style = "transform:translateX(5px);" >
< div id = "bloat" ></ div >
< div id = "child" style = "position:fixed;" ></ div >
</ div >
versus
< div id = "container" style = "position:relative; z-index:0; left:5px;" >
< div id = "bloat" ></ div >
< div id = "child" style = "position:absolute;" ></ div >
</ div >
When the background of an element is propagated to the canvas (see CSS Backgrounds 3 § 2.11.1 The Canvas Background and the Root Element and CSS Backgrounds 3 § 2.11.2 The Canvas Background and the HTML <body> Element), that background is not affected by any transform specified for that element or for the root element.
For elements that are effected by a transform (i.e. have a transform applied to them, or to any of their ancestor elements) and do not have their background propagated to the canvas, a value of fixed for the background-attachment property is treated as if it had a value of scroll. The computed value of background-attachment is not affected.
3. The transform Property
A transformation is applied to the coordinate system an element renders into through the transform property. This property contains a list of transform functions. The final transformation value for a coordinate system is obtained by converting each function in the list to its corresponding matrix like defined in Mathematical Description of Transform Functions, then multiplying the matrices.
Name: | transform |
---|---|
Value: | none | <transform-list> |
Initial: | none |
Applies to: | transformable elements |
Inherited: | no |
Percentages: | refer to the size of reference box |
Computed value: | as specified, but with lengths made absolute |
Canonical order: | per grammar |
Animation type: | transform list, see interpolation rules |
Any computed value other than none for the transform affects containing block and stacking context, as described in § 2 The Transform Rendering Model.
<transform-list> = <transform-function>+
3.1. Serialization of <transform-function>s
To serialize the <transform-function>s, serialize as per their individual grammars, in the order the grammars are written in, avoiding <calc()> expressions where possible, avoiding <calc()> transformations, omitting components when possible without changing the meaning, joining space-separated tokens with a single space, and following each serialized comma with a single space.
3.2. Resolved value of transform
The transform property is a resolved value special case property. [CSSOM]
When the computed value is a <transform-list>, the resolved value is one <matrix()> function computed by the following algorithm:
-
Let transform be a 4x4 matrix initialized to the identity matrix. The elements m11, m22, m33 and m44 of transform must be set to 1; all other elements of transform must be set to 0.
-
Post-multiply all <transform-function>s in <transform-list> to transform.
-
Serialize transform to a <matrix()> function.
For other computed values, the resolved value is the computed value.
4. The transform-origin Property
Name: | transform-origin |
---|---|
Value: | [ left | center | right | top | bottom | <length-percentage> ] | [ left | center | right | <length-percentage> ] [ top | center | bottom | <length-percentage> ] <length>? | [ [ center | left | right ] && [ center | top | bottom ] ] <length>? |
Initial: | 50% 50% |
Applies to: | transformable elements |
Inherited: | no |
Percentages: | refer to the size of reference box |
Computed value: | see background-position |
Canonical order: | per grammar |
Animation type: | by computed value |
The values of the transform and transform-origin properties are used to compute the transformation matrix, as described above.
If only one value is specified, the second value is assumed to be center. If one or two values are specified, the third value is assumed to be 0px.
If two or more values are defined and either no value is a keyword, or the only used keyword is center, then the first value represents the horizontal position (or offset) and the second represents the vertical position (or offset). A third value always represents the Z position (or offset) and must be of type <length>.
- <length-percentage>
-
A percentage for the horizontal offset is relative to the width of the reference box. A percentage for the vertical offset is relative to the height of the reference box. The value for the horizontal and vertical offset represent an offset from the top left corner of the reference box.
- <length>
-
A length value gives a fixed length as the offset. The value for the horizontal and vertical offset represent an offset from the top left corner of the reference box.
- top
-
Computes to 0% for the vertical position.
- right
-
Computes to 100% for the horizontal position.
- bottom
-
Computes to 100% for the vertical position.
- left
-
Computes to 0% for the horizontal position.
- center
-
Computes to 50% (left 50%) for the horizontal position if the horizontal position is not otherwise specified, or 50% (top 50%) for the vertical position if it is.
For SVG elements without associated CSS layout box the initial used value is 0 0 as if the user agent style sheet contained:
*:not ( svg), *:not ( foreignObject) > svg {
transform-origin : 0 0 ;
}
The transform-origin property is a resolved value special case property like height. [CSSOM]
5. Transform reference box: the transform-box property
Name: | transform-box |
---|---|
Value: | content-box | border-box | fill-box | stroke-box | view-box |
Initial: | view-box |
Applies to: | transformable elements |
Inherited: | no |
Percentages: | N/A |
Computed value: | specified keyword |
Canonical order: | per grammar |