An Introduction to CSS Transforms, CSS Summit, 2012

Tiffany B. Brown, Opera Software

An Introduction to CSS Transforms

Tiffany B. Brown (@webinista)
Opera Software Developer Relations Team
CSS Summit 2012

First,
the caveats…

Not all browsers support all features (yet).

Prefixes still required
-o-, -moz-, -ms-, -webkit-

Warning: Math Ahead

Coordinate System

A 2 D coordinate plane with X and Y axes and a zero-zero origin.

3D Coordinate System

A 3 D coordinate plane with X, Y and Z axes and a zero-zero-one origin.

Transforms Create a Local Coordinate System

A local coordinate system

Transforms Create a Local Coordinate System

transform:none versus transform: translate(100px,100px);

 
 

The Transform Origin

A local coordinate system with a transform origin.

transform-origin property

Specifies the x and y position of the origin, relative to the transform object.

  • Keyword positions: left, right, bottom, top, center
  • Length values
  • Percentage values (default = 50% 50%)

transform-origin: 50px 70px

A local coordinate system with a transform origin.

transform-origin: x y

0 0

50% 50%

100% 100%

200% 200%

perspective-origin property

Sets a horizon point at the given coordinates on the parent element. Changes the perceived vantage point at which the user appears to be looking at child elements.

  • Keyword positions: left, right, bottom, top, center
  • Length values
  • Percentage values (default = 50% 50%)

Perspective Demo

transform property

Which transform or transforms to apply.

  • transform: function();
  • transform: function() function();

What is a transform?

A mathematical function that alters an object while preserving the algebraic or geometric structure. Often expressed as a matrix.

Source

This is a matrix

image/svg+xml

What the browser does: Matrix multiplication

image/svg+xml =

Coordinate vector × Transform matrix = Transformed coordinates

Transform matrices as vectors

Function Vector
scale3d [sx 0 0 0 0 sy 0 0 0 0 sz 0 0 0 0 1]
translate [1 0 0 1 tx ty]
rotate [cos(a) sin(a) -sin(a) cos(a) 0 0]
skew (x-axis) [1 0 tan(a) 1 0 0]
skew (y-axis) [1 tan(a) 0 1 0 0]

Partial list. Sources: SVG and CSS Transforms specifications.

Vectors become parameters for the matrix functions

  • transform: matrix3d(sx,0,0,0,0,sy,0,0,0,0,sz,0,0,0,0,1);
  • transform: matrix(1,0,0,1,tx,ty);
  • transform: matrix(1,0,tan(a),1,0,0);
  • transform: matrix(1,tan(a),0,1,0,0);
  • transform: matrix(cos(a),-sin(a),tan(a),0,1,0,0);

Matrix Demo

Matrix Multiplication: Compound transforms

image/svg+xml

Fear matrix math?
Use transform functions

2D transform functions

  • matrix(a,b,c,d,e,f)
  • rotate(ang)
  • translate(tx[,ty])
  • translateX(tx)
  • translateY(ty)
  • scale(sx[,sy])
  • scaleX(sx)
  • scaleY(sy)
  • skewX(ang)
  • skewY(ang)

2D Transforms in action

3D transform functions

  • matrix3d(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p)
  • translate3d(tx, ty, tz)
  • translateZ(tz)
  • scale3d(n,n,n)
  • scaleZ(n)
  • rotate3d(n, n, n, ang)
  • rotateX(ang)
  • rotateY(ang)
  • rotateZ(ang)
  • perspective(len)

3D Transforms in action

Other Transform Properties

Other Transform Properties

  • backface-visibility
  • transform-style
  • transform-origin
  • perspective
    (not to be confused with the perspective() function)

backface-visibility

hidden or visible

1
2
3
4
5
6

transform-style

flat or preserve-3d

1
2
3

perspective

Applied to parent element. Smaller = closer; larger = farther

px

Transforms Demo

Other Things To Know

Transforms create a new stacking context

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Duis accumsan faucibus tortor id porta. Sed imperdiet, tortor vitae viverra vestibulum, diam lorem fermentum mauris, quis bibendum erat dolor non ante. Quisque quis augue ut purus feugiat semper sit amet sit amet mauris. Integer commodo ligula id velit blandit eget dictum sem fringilla. Nullam dolor dui, laoreet eget hendrerit a, fermentum id libero.

Morbi posuere condimentum leo, ut venenatis enim semper aliquet. Nunc rutrum varius iaculis. Quisque adipiscing mollis est. Nunc est nisl, fermentum non scelerisque id, vulputate id urna. Mauris eget quam vel eros molestie iaculis. Sed vulputate blandit dui ut mollis. Curabitur consectetur placerat odio in porta. Proin tempor, libero in ultrices venenatis, lacus turpis euismod leo, a placerat nisi nisl vitae odio.

Etiam eget neque diam. Donec eget ligula vel turpis aliquet dignissim sit amet sed magna. Aenean a metus congue nunc tempus elementum sit amet sit amet mi. In hac habitasse platea dictumst. Cras molestie tempor erat, ultricies placerat neque fringilla in. Fusce tincidunt elementum massa a hendrerit.

Nullam mollis nisl et est molestie quis lacinia leo imperdiet. Vestibulum faucibus varius velit, in pretium sem consequat et. Mauris pharetra dui quis tortor ultricies et convallis purus tincidunt. Curabitur ullamcorper vehicula ultrices. Vestibulum elit leo, fermentum rutrum convallis quis, gravida imperdiet risus.

Transforms create a containing block

Questions?

Resources