In CSS3 3D Transform, a cuboid is a 3D box with six faces, each defined by a set of coordinates. This is the basic building block for creating 3D effects.
To create a cuboid, you can use the transform property with the translate3d() function to position the box in 3D space. This allows you to move the box along the x, y, and z axes.
Perspective is a crucial aspect of 3D transforms, as it helps create a sense of depth and distance between objects. By using the perspective property, you can set the distance between the viewer and the 3D scene.
By setting a perspective of 1000px, you can create a scene where objects appear to be a certain distance away from the viewer.
CSS Methods
CSS Methods are the building blocks of 3D transformations in CSS. They allow us to manipulate elements in 3D space.
There are three main methods: translate3d(), rotate3d(), and scale3d(). These methods can be used to create complex 3D effects.
The translate3d() method is used to move elements in 3D space. It takes three values: the x, y, and z coordinates.
The rotate3d() method is used to rotate elements in 3D space. It takes four values: the x, y, z coordinates and the angle of rotation.
The scale3d() method is used to scale elements in 3D space. It takes three values: the x, y, and z scale factors.
To use these methods effectively, we need to define the perspective of the 3D space. The perspective property takes a length value, which indicates the distance from the viewer to the 3D plane containing the transformed element.
Here are the 3D transform methods in a concise list:
- translate3d()
- rotate3d()
- scale3d()
Transform Methods
Transform methods are the building blocks of CSS3 3D transforms. They allow you to manipulate elements in 3D space using various properties.
There are several transform methods, including translate3d(), rotate3d(), and scale3d(). These methods can be used to move, rotate, and scale elements in 3D space.
To visualize the effect of 3D transform methods, you need to use the perspective property, which defines the virtual distance between the viewer and 3D transformed elements.
Here are some key transform methods:
- translate3d(): moves an element along the x, y, or z axis in 3D space.
- rotate3d(): rotates an element around the x, y, or z axis in 3D space.
- scale3d(): scales an element along the x, y, and z axes in 3D space.
These transform methods can be used to create a wide range of 3D effects, from simple rotations to complex transformations.
Translate, Scale, and Rotate Properties
The translate, scale, and rotate properties are essential for creating 3D effects in CSS.
To move an element in 3D space, you can use the translate3d() method, which takes three values: x, y, and z, representing the distance the element moves along each axis. For example, translate3d(60px, 0, 0) moves an element 60 pixels to the right horizontally.
You can also use the translateX(), translateY(), and translateZ() methods to move an element along each axis individually. For example, translateX(60px) moves an element 60 pixels to the right horizontally.
Scaling an element in 3D space is done using the scale3d() method, which takes three values: x, y, and z, representing the scaling factors for each axis. For example, scale3d(1, 2, 5) scales an element's width to 1, height to 2, and depth to 5 times its original values.
Alternatively, you can use the scaleX(), scaleY(), and scaleZ() methods to scale an element along each axis individually. For example, scaleX(2) doubles an element's width, while scaleY(1.5) increases its height to 1.5 times its original value.
Rotating an element in 3D space is achieved using the rotate3d() method, which takes four values: x, y, z, and angle, representing the axis of rotation and the degree of rotation. For example, rotate3d(1, 0, 0, 30deg) rotates an element 30 degrees around the x-axis.
You can also use the rotateX(), rotateY(), and rotateZ() methods to rotate an element along each axis individually. For example, rotateX(30deg) rotates an element 30 degrees around the x-axis.
Here's a summary of the translate, scale, and rotate properties:
Hierarchies
Transforming elements can be a powerful tool, but it's essential to understand how they interact with each other in a hierarchy. By default, transformed elements create a flattened representation of their content.
Transformed elements don't create a 3D rendering context by default. This means that their descendants are rendered into the plane of the element, rather than sharing a common 3D space.
However, you can override this behavior by specifying a value of preserve-3d for the transform-style property. This allows descendants of the transformed element to share the same 3D rendering context.
Elements that are 3D-transformed and share a common 3D space will "pop out" into their own planes. This can create some really interesting visual effects, especially when combined with other transform properties.
The transform-style property can be used to create hierarchies of transformed objects that share a common 3D space. This can be especially useful for creating complex, interactive designs.
By preserving the 3D context, you can create a sense of depth and dimensionality in your designs. This can make them feel more engaging and immersive for the viewer.
Transform Properties
Transform Properties are a crucial part of CSS3 3D Transforms, allowing you to manipulate the positioning and orientation of elements in 3D space.
An element is considered a 3D transform element if its computed value for the transform property includes one of the 3D transform functions, such as translate3d(0, 0, 0) or rotate3d(1, 1, 1, 0).
A 4x4 matrix is used to represent the 3D transform, which is different from a 2D matrix, and includes values for scaling, rotation, and translation in 3D space.
The identity transform function includes examples like translate3d(0, 0, 0), translateZ(0), scaleZ(1), and rotate3d(1, 1, 1, 0), which essentially do nothing but preserve the original position and orientation of the element.
A special case is the perspective function, where perspective(none) makes the value of m34 infinitesimally small, effectively making the transform function equal to the identity matrix.
Terminology
An element with a 3D transform function is one whose computed value for the transform property includes one of these functions.
A 3D transform function is a 4x4 matrix that doesn't meet the requirements of a 2D matrix.
Examples of identity transform functions include translate3d(0, 0, 0), translateZ(0), scaleZ(1), rotate3d(1, 1, 1, 0), rotateX(0), rotateY(0), rotateZ(0), and matrix3d(1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1, 0, 0, 0, 0, 1).
Perspective is a special case, and when set to none, it makes the value of m34 infinitesimally small, effectively making the transform function equal to the identity matrix.
Everything's a Cuboid
For most things, we can use a cuboid. We can create more complex shapes, but they usually take a little more consideration.
Curves are particularly hard, but there are some tricks for handling them. Applying transform-style: preserve-3d is crucial, and it's a good idea to apply it everywhere.
Trying to debug a missing transform-style while hopping between browsers can be painful. It's likely we'll deal with nested cuboids when things get more complex.
Imagine the whole scene built from cuboids when creating 3D creations with more than a few faces. For a real example, consider a 3D book made from four cuboids: one for each cover, one for the spine, and one for the pages.
Perspective and Backface
Perspective and Backface is a powerful duo in CSS3 3D transforms. The perspective() function makes elements appear more realistic by scaling them based on their distance from the viewer, with lower values creating a more extreme effect.
The perspective property applies the same transform as the perspective() function, but only to the children of the element, not the element itself. This is useful for creating a shared perspective among multiple elements.
The perspective-origin property sets the X and Y position where the viewer appears to be looking at the children of the element. It's like setting the origin of a 3D scene, and can be used to create a more immersive experience.
The backface-visibility property determines whether or not the "back" side of a transformed element is visible when facing the viewer. With an identity transform, the front side of an element faces the viewer, but applying a rotation of 180 degrees would cause the back side to face the viewer.
Here's a quick recap of the backface-visibility property values:
Perspective
Perspective is a powerful tool for creating a sense of depth in our designs. It's like looking at a 3D model from a certain angle, and the lower the perspective value, the more extreme the 3D effect will appear.
The perspective() function takes a single value, which is a length unit greater than 0, and it's like a distance between you and the object you're viewing. Any value below 200px will make the transformation appear very exaggerated, and any value of 1000px or more will seem to have no effect at all.
To apply perspective, you can use the perspective property, which applies the same transform as the perspective() function, but only to the positioned or transformed children of the element, not to the transform on the element itself. The perspective property establishes a stacking context and a containing block, just like the transform property does.
The perspective property can be used to add a feeling of depth to a scene by making elements higher on the Z axis appear larger, and those further away appear smaller. The scaling is proportional to d/(d − Z), where d is the value of perspective, which is the distance from the drawing plane to the assumed position of the viewer's eye.
You can influence the rendering of 3D-transformed children by using the perspective and perspective-origin properties. The perspective-origin property establishes the origin for the perspective property, effectively setting the X and Y position at which the viewer appears to be looking at the children of the element.
Here are some common values for perspective-origin:
The perspective matrix is computed by starting with the identity matrix, translating by the computed X and Y values of perspective-origin, multiplying by the matrix that would be obtained from the perspective() transform function, and then translating by the negated computed X and Y values of perspective-origin.
Backface Visibility
Backface visibility is a property that determines whether the reverse side of a transformed element is visible when facing the viewer. With an identity transform, the front side of an element faces the viewer.
The default behavior is for the element to be shown in reverse when its front is angled away from you, but you can use backface-visibility with a value of hidden to prevent this from happening.
You can use backface-visibility to create a playing card effect, where the front and back elements switch places during an animation. This can be useful when creating a 3D stage backdrop.
Elements that are not transformed or only 2D-transformed do not have backface visibility, and their reverse sides are not considered.
The visibility of an element with backface-visibility: hidden is determined by its accumulated 3D transformation matrix. If the component of the matrix in row 3, column 3 is negative, then the element should be hidden.
Here are the possible values for the backface-visibility property:
The backface-visibility property is useful when creating 3D effects, such as a box made out of 6 elements, where you want to see only the inside faces of the box.
Setting a Scene
Setting a scene is crucial for creating a 3D project, and it starts with creating a plane where your creation will sit. This plane makes it easier to rotate and move the whole creation.
To make our lives easier, we can set a scene by creating a plane and rotating it on the X and Y axis first. Then we lay it flat with rotateX(90deg).
Setting position: absolute on all cuboids can also be helpful in organizing our scene. This way, we can easily add new cuboids to the scene without worrying about their position.
Rotating the scene on the X and Y axis first can help us visualize our creation better. This is especially useful when working with complex scenes that need to be broken down into different sections.
Origin and Visibility
The transform-origin property allows you to change the point around which transformations are applied, giving you more control over the effect.
You can set the transform-origin to any point in the element, using pairs of lengths, percentages, or positional keywords like top, right, or center.
In practice, this means you can create different effects by applying the same transformation to elements with different transform-origin values, like the example with the elements on the left and right.
The backface-visibility property, on the other hand, controls what happens when an element is transformed so its reverse side is visible, which can happen when using 3D transformations.
If you set backface-visibility to hidden, the element will be invisible, even if it has a background color, and this effect is live, meaning it will work even during animations.
The 'Origin
The 'Origin' is a fundamental concept in CSS transformations, and it's essential to understand how it works to create visually appealing effects. The transform-origin property establishes the origin of transformation for an element.
By default, the origin is set to the center of the element, but you can change it to any point using a pair of lengths, percentages, or positional keywords like top, right, or center. For example, setting the transform-origin to right top will move the transformation point to the top-right corner of the element.
You can see the difference in action when applying the same transformation to two elements with different transform-origin values. The element on the left has a transform-origin value of center center, while the one on the right has a value of right top.
Here's a breakdown of the acceptable values for the transform-origin property:
The transform-origin property is applied by first translating the element by the negated value of the property, then applying the element's transform, and finally translating by the property value. This effectively moves the desired transformation origin of the element to (0,0,0) in the local coordinate system, then applies the element's transform, and then moves the element back to its original position.
Centering
Centering is intentional in cuboid design, making animation easier, especially when rotating around the Z-axis. This design choice was discovered while creating "CSS is Cake".
Having an offset center would affect the demo, as seen in the breakdown of the cake slices. It's a subtle but important detail that can impact the overall user experience.
The containing element of our cuboids is used as the center of the cuboid, which simplifies animation and interaction.
Sources
- https://www.programiz.com/css/3d-transform
- https://www.smashingmagazine.com/2012/01/adventures-in-the-third-dimension-css-3-d-transforms/
- https://www.w3.org/TR/2009/WD-css3-3d-transforms-20090320/
- https://css-tricks.com/css-in-3d-learning-to-think-in-cubes-instead-of-boxes/
- https://www.w3.org/TR/css-transforms-2/
Featured Images: pexels.com