Matrix() - Function CSS
Summary of characteristics of the matrix()
function
Description of the matrix()
function.
The function matrix()
calculates a composite transformation on an element. This transformation can involve translations, rotations, scaling, or inclinations.
matrix()
can therefore replace all other transformation functions: translate()
, rotate()
, scale()
, and skew()
.
However, its use is more complex than that of the separate functions, particularly in determining the values of the parameters.
All transformations are serialized (stored) in the form matrix()
.
The matrix()
value can be used with the following properties:
transform
: Applies one or more geometric transformations (rotations, enlargements, etc.).
Syntaxes of the matrix()
function..
- transform: matrix(1, 0, 0, 1, 0, 0); a b c d e f
The parameters, from
a
tof
are six dimensionless numbers.a
andd
define the scaling ratios, respectively in the horizontal and vertical directions.
Between 0 and 1, they reduce the size of the element; greater than 1, they enlarge it. Negative values are accepted and cause a mirror effect on the element.
Scaling an element (zoom effect) corresponds to the work of the propertyscale()
and its derivatives.
Examples:matrix(2,0,0,1,0,0)
doubles the width of the element.
matrix(-1,0,0,1,0,0)
flips the element horizontally.
b
andc
define the tilts applied to the element (we can also talk about shear forces). If these two values are set to 0, no deformation will be applied. Negative values are accepted.
To better understand the effect of these two parameters, test the simulator below by specifying only one of the two values each time. For example, try the following values:
matrix(1, 0.25, 0, 1, 0, 0)
matrix(1, 0, 0.25, 1, 0, 0)
e
andf
define a translation that will be applied to the element, respectively in the horizontal direction and in the vertical direction. Negative values are allowed.
Test the following syntaxes in the simulator below:
matrix(1, 0, 0, 1, 100, 0)
matrix(1, 0, 0, 1, 0, 50)
Note: the rotation of the element is done by a combination of scaling and tilting parameters. For example, to rotate the element by an angle
a
, use the following values:
matrix(cos(a), sin(a), -sin(a), cos(a), 0, 0)
This results in 30°:
matrix(0.87, 0.5, -0.5, 0.87, 0, 0)
Mathematical explanation.
If matrix calculation is not your cup of tea, you can easily skip this explanation. For others, know that the matrix()
function performs
the multiplication of a 3x3 matrix created with the function's parameters and a vector composed of the xy coordinates of a point.
A vector containing the new coordinates of the point is then obtained. This calculation is performed for each corner of the element.
x
andy
are the original coordinates of the point.x'
andy'
are the final coordinates of the point (after transformation).scx, skx, sky, scy, trx, try
are the values passed as parameters to the functionmatrix()
.
Matrix calculation gives the following formulas for the computation. The multiplication symbol is the dot, to avoid confusion with x.
x' = scx . x + sky . y + trx
y' = skx . x + scy . y + try
Simulator.
The simulator below provides you with the parameters of the function matrix()
equivalent to a basic transformation (translation, rotation, etc.).
It does not allow for the accumulation of multiple basic transformations, which is normally possible.
Browsers compatibility with matrix()
function.
Currently, the vast majority of browsers handle the function matrix()
correctly as well as all 2D transformations.
matrix()
function that can be used with the transform
property to apply any 2D transformation.Notes:
(1) Does not support 2D transformations on SVG elements. Use the transform
attribute instead.
transformations
matrix()
function
Browsers on computers :
Mobile browsers :
Outdated or marginal browsers :

Internet Explorer

KaiOS Browser

Opéra Mobile

Opéra

Firefox pour Androïd

Samsung Internet

Chrome

Edge

Firefox

Androïd Brower

Chrome pour Androïd

Baidu Browser

QQ Browser

Safari

Safari sur IOS

UC Browser pour Androïd

Opéra mini
Historic of the matrix()
function.
-
CSS Transforms Module Level 1
First level of specification concerning 2D transformations.
Presentation of different transformation functions such asmatrix()
.February 28, 2012Working Draft.February 14, 2019Candidate Recommendation. -
CSS Transforms Module Level 2
Level 2 of the Transforms specification, introducing 3D transformations.
Addingmatrix3d()
function.
No change directly concerning thematrix()
function.March 03, 2020Working Draft.
See also, regarding the transformations.
The W3C organizes the CSS specifications into modules. TheCSS Transforms Module encompasses all the properties and functions concerning transformations in CSS:
Properties:





Functions:

