w3resource

CSS Properties: How to transformed child elements preserve the 3D transformations

Go to Exercise page

Solution:

HTML Code :

<!DOCTYPE html><!-- Declares the document type and version of HTML -->
<html><!-- Begins the HTML document -->
<head><!-- Contains metadata and links to external resources -->
<title>How to transformed child elements preserve the 3D transformations</title><!-- Sets the title of the document -->
<style type="text/css"> /* Starts CSS styling */
#div1 {
    position: relative; /* Sets the positioning context for div1 */
    height: 250px; /* Sets the height of div1 to 250 pixels */
    width: 230px; /* Sets the width of div1 to 230 pixels */
    margin: 100px; /* Sets the margin around div1 */
    padding: 10px; /* Sets the padding inside div1 */
    border: 1px solid #CC0099; /* Sets the border style and color of div1 */
}

#div2 {
    padding: 50px; /* Sets the padding inside div2 */
    position: absolute; /* Positions div2 absolutely within its nearest positioned ancestor */
    border: 1px solid #6633FF; /* Sets the border style and color of div2 */
    background-color: #0000CC; /* Sets the background color of div2 */
    -webkit-transform: rotateY(60deg); /* Applies a Y-axis rotation to div2 for WebKit-based browsers */
    -webkit-transform-style: preserve-3d; /* Preserves the 3D transformations of child elements in WebKit-based browsers */
    transform: rotateY(60deg); /* Applies a Y-axis rotation to div2 for other browsers */
    transform-style: preserve-3d; /* Preserves the 3D transformations of child elements in other browsers */
}

#div3 {
    padding: 50px; /* Sets the padding inside div3 */
    position: absolute; /* Positions div3 absolutely within its nearest positioned ancestor */
    border: 1px solid #FF0000; /* Sets the border style and color of div3 */
    background-color: #33CCFF; /* Sets the background color of div3 */
    -webkit-transform: rotateY(-60deg); /* Applies a Y-axis rotation to div3 for WebKit-based browsers */
    transform: rotateY(-60deg); /* Applies a Y-axis rotation to div3 for other browsers */
}
</style><!-- Ends CSS styling -->
</head><!-- Ends the head section -->
<body><!-- Begins the body of the document -->
<h1><strong>w3resource Tutorial</strong></h1><!-- Displays a level 1 heading with bold text "w3resource Tutorial" -->
<div id="div1"><!-- Starts a div with id "div1" -->
<div id="div2"><strong>w3resource</strong><!-- Starts a div with id "div2" inside div1 -->
	<div id="div3">Web Development Tutorial</div><!-- Starts a div with id "div3" inside div2 -->
</div><!-- Ends div2 -->
</div><!-- Ends div1 -->
</body><!-- Ends the body section -->
</html><!-- Ends the HTML document -->

Explanation:

  • This HTML document demonstrates how child elements preserve 3D transformations applied to their parent elements.
  • div1 serves as the parent container for div2.
  • div2 is rotated by 60 degrees around the Y-axis using CSS3 transformations, and its 3D transformations are preserved.
  • Inside div2, div3 is also rotated by -60 degrees around the Y-axis, demonstrating that child elements maintain the 3D transformations of their parent elements.

Live Demo:

See the Pen transform-style-answer by w3resource (@w3resource) on CodePen.


See the solution in the browser

Supported browser

Firefox logo Chrome logo Opera logo Safari logo Internet Explorer logo
YesYesYesYesYes

Go to Exercise page

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://w3resource.com/html-css-exercise/css-properties/transform-style-answer.php