# How to transform child elements preserve the 3D transformations?

Being a web developer, it's important to have a solid understanding of 3D transformations and how to apply them to child elements. Transforming child elements can be a bit tricky, as we'll need to make sure that the transformations of the parent element are preserved. CSS 3D transforms create depth and visually interesting elements on the web page using perspective.

In this article, we will learn how to transform child elements and preserve 3D transformations. We will learn different methods to transform the child elements in HTML.

## Different Methods to Transform Child Elements Preserve the 3D Transformations

To transform the child elements preserving their 3D transformations consists of various different methods are used for the same. Let’s see some of the commonly used methods used in transforming child elements.

### Method 1: Using Transform-style Property

The first method used to transform the child elements to preserve the 3d element is using the transform-style property. This property specifies how child elements are transformed in 3D space when the parent element has a 3D transform applied to it. It has two possible values: preserve-3d and flat. Below is the syntax and example −

### Syntax

Below is the syntax for using the transform-style property to transform any child element preserving the 3d transformations.

.parent {
transform: rotateY(45deg);
transform-style: preserve-3d;
}
.child-wrapper {
transform: rotateY(-45deg);
transform-style: preserve-3d;
}


In the syntax above, we have defined a parent element with a rotation of 45 degrees around the Y-axis. Here, the child element is also rotated with a -45 degrees rotation around the Y axis, and the transform-styles property is set to preserve-3d so that the children of an element are positioned in 3D space or flattened with respect to the plane of the element.

### Example

In the given example, we have defined a parent element that is rotated by 45 degrees, and the child-wrapper element is rotated by -45 degrees to counteract the parent element's rotation. The child element inherits the combined rotation of both parent and child-wrapper elements, resulting in no rotation.

<html>
<style>
.parent-element {
width: 200px;
height: 200px;
background-color: green;
transform: rotateY(45deg);
transform-style: preserve-3d;
}
.child-wrapper-container {
transform: rotateY(-45deg);
transform-style: preserve-3d;
}
.child-element {
width: 50px;
height: 50px;
background-color: blue;
}
</style>
<body>
<h2>Transforming the Child Elements Using Transform-style Property</h2>
<div class="parent-element">
<div class="child-wrapper-container">
<div class="child-element"></div>
</div>
</div>
</body>
</html>


### Method 2: Using Absolute Position

The second method to transform the child elements to preserve the 3d element is using the absolute position. With this method, we can easily position child elements within a parent element without affecting the parent's transformations.

Below is the syntax and example −

### Syntax

Below is the syntax for using the absolute position to transform any child element preserving the 3d transformations.

.parent {
transform: rotateY(45deg);
position: relative;
}
.child {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%) rotateY(-45deg);
}


In the syntax above, we have defined a parent element with a rotation of 45 degrees around the Y-axis. We have also defined a child element that is positioned absolutely within the parent element, and its position is adjusted to be centered using the left and top properties. Here, the child element's own transformation is then applied, that is the rotation of -45 degrees around the Y axis.

### Example

In the given example, we have defined a parent element with a rotation of 45 degrees around the Y-axis, and the child element is positioned absolutely within the parent element having its position adjusted to be centered using the left and top properties.

Here, the child element's own transformation is then applied, that is a rotation of -45 degrees around the Y axis.

<html>
<style>
.parent-element {
width: 150px;
height: 150px;
background-color: red;
transform: rotateY(45deg);
position: relative;
}
.child-element {
width: 50px;
height: 50px;
background-color: green;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%) rotateY(-45deg);
}
</style>
<body>
<h2>Transforming the Child Elements Using Absolute Position</h2>
<div class="parent-element">
<div class="child-element"></div>
</div>
</body>
</html>


### Method 3: Using Backface-visibility

The third method to transform the child elements to preserve the 3d element is using the backface-visibility property. With this property, we can control whether or not the back face of an element has set to be visible when the element is rotated in 3D space. If this property is set to hidden, we can prevent any transformations from affecting the back face of an element.

### Syntax

Below is the syntax for using the backface-visibility property to transform any child element preserving the 3d transformations.

.parent {
transform: rotateY(45deg);
}
.child {
transform: rotateY(-45deg);
backface-visibility: hidden;
}


In the syntax above, we have defined a parent element with a rotation of 45 degrees around the Y-axis. Here, the child element is also rotated with a -45 degrees rotation around the Y axis, and the backface-visibility property is set to hidden to prevent any transformations from affecting the back face of the element.

### Example

In the given example, we have defined a parent element with a rotation of 45 degrees around the Y-axis, and the child element is then rotated at -45 degrees around the Y-axis, but the backface-visibility property is set to hide that prevents any transformations from affecting the back face of the element.

<html>
<style>
.parent-element {
width: 500px;
height: 100px;
background-color: red;
transform: rotateY(45deg);
}
.child-element {
width: 405px;
height: 95px;
background-color: blue;
transform: rotateY(-45deg);
backface-visibility: hidden;
}
</style>
<body>
<h2>Transforming the Child Elements Using backface-visibility Property</h2>
<div class="parent-element">
<div class="child-element"></div>
</div>
</body>
</html>


## Conclusion

Ttransforming child elements while preserving 3D transformations is an essential skill for web developers. Applying 3D transformations to child elements can be complicated, and ensuring that the transformations of the parent element are preserved is crucial. In this article, we explored three different methods of transforming child elements while preserving 3D transformations, including using the transform-style property, absolute position, and backface-visibility property. Each method has its advantages and disadvantages, and developers should choose the method that best fits their project's needs. With a good understanding of these techniques, web developers can create visually stunning and engaging 3D elements on their web pages.

Updated on: 04-May-2023

41 Views