Graphics that use a two-dimensional representation of geometric data.
CSS transforms open up a world of possibilities when it comes to manipulating HTML elements. In this unit, we will focus on 2D transforms, which allow us to move, rotate, scale, and skew elements in a two-dimensional space.
There are four primary methods for 2D transformations in CSS: translate, rotate, scale, and skew.
The translate
method moves an element from its current position in the 2D space. It takes two parameters: translate(x, y)
, where x
moves the element horizontally and y
vertically. The values can be in percentages, ems, rems, pixels, or viewport units.
div { transform: translate(50px, 100px); }
In the above example, the div
element will move 50 pixels to the right and 100 pixels down from its original position.
The rotate
method rotates an element clockwise or counterclockwise around a given point, defined by the transform-origin
property. The rotation angle is specified in degrees (deg
), gradients (grad
), radians (rad
), or turns (turn
).
div { transform: rotate(45deg); }
In this example, the div
element will rotate 45 degrees clockwise.
The scale
method changes the size of an element. It takes two parameters: scale(x, y)
, where x
scales the element horizontally and y
vertically. A value of 1 is the element's original size, while values less than 1 shrink the element and values greater than 1 enlarge it.
div { transform: scale(1.5, 1.5); }
In this example, the div
element will increase to 1.5 times its original size in both width and height.
The skew
method tilts an element along the X and Y axes. It takes two parameters: skew(x-angle, y-angle)
, where x-angle
skews the element along the X-axis and y-angle
along the Y-axis. The skew angles are specified in degrees (deg
), gradients (grad
), radians (rad
), or turns (turn
).
div { transform: skew(20deg, 10deg); }
In this example, the div
element will skew 20 degrees along the X-axis and 10 degrees along the Y-axis.
You can combine multiple transform methods by listing them within the transform
property.
div { transform: translate(50px, 100px) rotate(45deg) scale(1.5, 1.5) skew(20deg, 10deg); }
In this example, the div
element will first move, then rotate, scale, and finally skew.
By mastering these 2D transform methods, you can create intricate designs and interactive elements that enhance the user experience on your website.