Manipulate Elements with CSS transform

Hero image for Manipulate Elements with CSS transform. Image by Jeffery Ho.
Hero image for 'Manipulate Elements with CSS transform.' Image by Jeffery Ho.

CSS transforms are a versatile and powerful tool that allows us to change the shape, size, and position of HTML elements in our web applications. Whilst CSS transitions focus on animating changes over time, transform handles the geometric manipulation of an element.

In this article, I intend to explore what transforms are, how they differ from transitions, and how they can be used to add engagement to the websites we develop.


What are CSS Transforms?

At their core, CSS transforms enable us to modify elements visually without affecting their actual layout within the document flow. Using the transform property we can rotate, scale, skew, or translate elements.

For example, rotating an element by 45 degrees using transform looks like this:

.box {  transform: rotate(45deg);}

Transforms vs. Transitions

Interestingly, this is a point that I see even more senior developers stumble on, so it's worth quickly discussing. Whilst both CSS transform and transition can enhance a website's visual appeal, they serve very different and distinct purposes:

  • transform modifies the appearance or positioning of elements on the page.
  • transition describes animation between different states of an element. For example, when hovering over an element.

These can be combined together to introduce animated transitions, such as an animated hover effect like this:

.button {  transform: scale(1);  transition: transform 0.3s ease;}.button:hover {  transform: scale(1.2);}

This animates the scaling effect when the element is hovered over, combining the strengths of both features. For a detailed look at CSS transitions, check out my previous article on the topic.


Common transform Functions

CSS transforms provide several builtin functions for manipulating elements. Here's some of the most common ones:

1. translate

Moves an element along the X and Y axes without changing its layout:

.box {  transform: translate(50px, 20px);}

Here, the element will be shifted 50 pixels to the right and 20 pixels downwards.

2. scale()

Resizes an element proportionally whilst retaining the original bounding box (i.e., overlapping adjacent elements rather than shifting them in the document flow):

.box {  transform: scale(1.5);}

Here, the element becomes 1.5 times larger in both width and height.

3. rotate()

Rotates an element clockwise by the specified angle:

.box {  transform: rotate(90deg);}

Here, the element rotates 90 degrees clockwise.

4. skew()

Skews an element along the X and/or Y axes, creating a slanted effect: css Copy code

.box {  transform: skew(20deg, 10deg);}

Here, we are skewing the element by 20 degrees along the Xaxis and 10 degrees along the Yaxis.

5. matrix()

The matrix() function combines all transform operations into a single function for advanced use cases. However, it is very rarely used manually just because of its complexity. Instead, I would recommend combining multiple transforms together in a single declaration.


Combining Multiple Transforms

Outside of using the matrix() function, you can combine multiple transform functions together into a single transform property like this:

.box {  transform: rotate(45deg) translate(20px, 20px) scale(1.2);}

In this example, the element is rotated, moved, and scaled, all within a single declaration. It's important to note that the order of these transformations matters: each transform is applied in sequence, in the order they are written.


Practical Examples of Transforms

1. Flipping Elements

transform can be used to create flipping effects, perfect for cards or tiles:

.card {  transform: rotateY(180deg);}

This is exactly the method I used to create the card flipping animation for the Cox & Kings Pairs Game:

2. Dynamic UI Elements

Buttons or icons can use transform for subtle interactive effects or microinteractions:

.icon:hover {  transform: scale(1.1) rotate(10deg);}

Wrapping up

CSS transforms gives us the ability to manipulate elements in creative and dynamic ways, from simple translations to more complex rotations, scaling, and positioning in three dimensions. Whilst CSS transform modifies how an element is rendered, CSS transition can animate those changes, making the two properties powerful companions for creating interactive, visually appealing designs.

Key Takeaways

  • CSS transforms modify elements visually through operations like translate, scale, and rotate.
  • Use transform alongside transition to create smooth animations.
  • Transforms do not affect the document flow, allowing for creative positioning and manipulation.

Understanding CSS transforms expands our toolkit for crafting dynamic, responsive, and engaging web experiences. Pairing them with transitions allows us to create polished, userfriendly designs.


Categories:

  1. CSS
  2. Development
  3. Front‑End Development
  4. Guides