2D & 3D Transforms

 
new course
CSS grid landing page tutorial (36min)

Lesson info

Lesson info

In web design, transforms allow you to manipulate an element's appearance and position without changing the actual dimensions of the element, or affecting the other elements around it. There are four kinds of transforms:

  1. Move
  2. ‍Scale
  3. ‍Rotate
  4. ‍Skew

In this video, we'll show you to add each of these transform types to your web designs, whether they're 2D or 3D. We'll also briefly cover how to use transforms to smooth out transitions between states like hover, pressed, and focused.

Clone this projectDownload project assetsDownload lesson assets

Transcript

Just like setting relative position on an element, Transforms give us the power to manipulate without interrupting the default document flow. That (what we just said) is a technical and likely obnoxious way of saying: Transforms only affect an element and its children without pushing other stuff around. We can move an element, affect its scale or size, rotate the element, skew its proportions, and control specific settings regarding how each of these Transform values affects styling.

Instead of going through this content on the None state — which we know isn't so much a state as it's just styling the normal element — instead of going through all of this on None, we're going to add our Transforms on hover.

Why? Good question.

Let's start with Move.

With our element selected — and this is a link block, but this works on almost any kind of element like buttons or text or Adamantium — we're going to head on over to our Hover state. And as we make changes to this state, not only are we affecting this element; we're affecting the other two link blocks as well. They've all been styled using the same class.

So in Hover, let's move things around using a Transform. And here we can adjust the X value to move left and right. We can adjust the Y value to move up and down. We can use combinations of X and Y to move things diagonally. And we can change our Z value. Whoops, that's embarrassing. Except it isn't.

The Z value is essentially pushing or pulling an object away from or towards what we think of as the screen. It's our depth value. So we have to set our Children Perspective on one of the parent elements. We can just select our section? And set our Children Perspective to — let's say — 1000 pixels.

Back to our Link Block, back into where we were controlling our Transform? We can move our Z value accordingly. And now that depth — that perspective — is more apparent.

If we hover over these Link Blocks, we can see our Transform effect in action. But it's a bit abrupt. Let's go in (we're going to style None) and we'll go and add a Transition. What kind of Transition? A Transition affecting Transforms. And we'll set the duration to 500 milliseconds (half a second).

Hover now, and we can see that move in action. Right on our Link Blocks.

That's Move. Let's do Scale.

And from our Move controls, we can switch over to scale. Keep in mind that doing this loses all our Move values. Each Transform type (like move, scale, rotate...) must be made separately. We can add a separate one for each. So if we go back to Move? Tweak some values? That's cool. We can close out and add a separate Transform, and now we can choose Scale.

Notice how the changes we made on the Move Transform still exist — because this is separate. We can adjust our X and Y values (these are linked by default). Or we can unlink and adjust x and y values independently.

If we go to test this out? Take a look at hover? We can see the Transform properties we just set.

And what about Z? This is actually kind of cool. It's really powerful. And right now it does nothing. We cover this more in 3D Perspective, but here we'll say Z scaling is an option.

Let's delete our existing Transforms so we can clean house. And we'll move on to Rotate.

Add a Transform? Rotate. We can rotate about the x-axis. We can rotate about the y-axis. We can rotate about the z-axis. If we hover over these Link Blocks, we can again preview what this Transform is looking like on hover.

And the reason this looks 3D — which it is — is because we added that Child Perspective to our section. If we go back into the section and remove that Child Perspective? We get a bit of flattening. Not always the intended look.

That's Rotate. Let's do Skew.

And Skew is one of those Transforms that's great to assess visually. We'll delete our Rotate Transform, then add a new one for Skew. Adjusting the x value here controls shearing along the x-axis, and adjusting the y-value controls shearing along the y-axis.

That's Skew. And finally, let's look at some settings.

Inside Settings, we can control our Transform origin. This is like setting our anchor point. If we add a Transform and go over to scale? We can see that everything is scaling from the center. That's because the origin is in the center.

If we click out, go back to Settings, and adjust the Transform origin to the bottom-right? We'll notice that our anchor point (that Transform origin) is scaling from the bottom-right. If we go to Rotate? That's the Transform origin for rotations as well.

Self Perspective can be used if we only want to affect our element and its children, but the 3D effect is relative to itself. It's not taking into account what we'd expect the perspective to be inside, say, our section.

And backface? This is how we can set whether or not the backside of an object is visible.

So. Transforms. A powerful tool to affect movement, scaling, rotation, skewing, and skewering. Of course additional settings to control these values.