
CSS Transitions: Syntax, Timing, and Interaction States
27/07/2023
CSS Animations with @keyframes and animation
29/07/2023
CSS transforms rotate, skew, scale, or move an element. They change how the element is rendered without moving surrounding boxes in normal flow.
Transforms apply as soon as the style changes. Combine them with transitions to animate between states such as :hover, :focus, or :active, or between class changes.
1. CSS transformation properties
Use transform to apply the transformation and transform-origin to change its reference point.
1.1. The transform-origin property
transform-origin sets the point around which a rotation or scale is applied. For ordinary HTML boxes, the default is the center.
Common ways to set the origin:
- Keywords: Use
top,left,right,bottom, orcenter. Combine them as intop leftorbottom right. Ejemplo:
transform-origin: top left; /* Top-left corner */ transform-origin: bottom right; /* Bottom-right corner */ transform-origin: center; /* Center of the element (default) */
- Percentages: The start is
0%and the end is100%. For an ordinary box,50% 50%is its center. Ejemplo:
transform-origin: 25% 75%; /* 25% from the left, 75% from the top */
- Lengths: Use offsets such as
20px 30px, or font-relative lengths such as1em. Ejemplo:
transform-origin: 20px 30px; /* 20px from the left, 30px from the top */
1.2. The transform property
Use transform to apply one or more transform functions to an element.
transform: transform-function(value);
Separate functions with spaces. Their order matters, so compare a translation followed by a rotation with the same functions in reverse order.
transform: transform-function(value) another-transform(value);
2. Transform functions
These functions cover the most common two-dimensional transformations:
- Scale:
scale(x)scales both axes, andscale(x, y)sets separate factors. An absolute factor greater than 1 enlarges the element along that axis. A factor between −1 and 1 reduces it, and a negative factor also reflects it.
transform: scale(0.5); /* Scale the element by half */
- Translate:
translate(x)moves an element horizontally, andtranslate(x, y)moves it along both axes. Values can be lengths or percentages of the transform reference box.
transform: translate(10px); /* Move the element 10px to the right */
- Rotate:
rotate(angle)rotates the element through the specified angle, for example45deg.
transform: rotate(45deg); /* Rotate the element 45 degrees */
- Skew:
skew(x)orskew(x, y)shears the element by the given angles.
transform: skew(45deg); /* Skew the element 45 degrees along the x-axis */
- Matrix:
matrix(a, b, c, d, tx, ty)expresses a two-dimensional transform. The first four numbers describe the linear transformation and the last two set translation.
transform: matrix(0.5, 0.1, 0.5, 1, 10, -2);
Use transition on the transform property to animate changes over time.
.example{
transition: transform 1s linear;
}
.example:hover{
transform: scale(.5);
}
3. Transform exercise
Create a 180px square with text and a background color. On hover, try these effects one at a time:
- Scale the box.
- Rotate the box.
- Move the box with
translate(). - Skew the box with
skew().
4. Centering an element with translate()
Use absolute positioning and translate() to center an image or another box horizontally and vertically inside a container.
- Container:
position: relativeestablishes the containing block for the positioned child. - Child: Set
position: absolute,left: 50%, andtop: 50%to position the child’s top-left corner at the center of the containing block. Then usetransform: translate(-50%, -50%)to move the child back by half of its own width and height.
This technique centers the entire box, including a box with text inside it. Use text-align to align text inside that box, or Flexbox or Grid when the centered item should stay in the normal layout.