Interpretation of CSS 3D view properties: transform and perspective, specific code examples are required
Introduction:
In modern web design, 3D effects have become a very important Popular elements. Through the transform and perspective properties of CSS, we can easily add 3D visual effects to web pages to make them more vivid and attractive. This article will explain these two properties and provide specific code examples.
1. Transform attribute:
The transform attribute is a very powerful attribute in CSS. It can realize various transformation operations such as rotation, scaling, movement and tilt of elements. In the 3D view, we can use the transform attribute to transform elements in 3D space.
The following are some commonly used transform functions:
By combining different transform functions, we can achieve complex 3D transformation effects. For example, we can use rotation, scaling, and movement to create a 3D cube:
<div class="cube"> <div class="face front">Front</div> <div class="face back">Back</div> <div class="face left">Left</div> <div class="face right">Right</div> <div class="face top">Top</div> <div class="face bottom">Bottom</div> </div>
.cube { width: 200px; height: 200px; position: relative; transform-style: preserve-3d; } .face { position: absolute; width: 200px; height: 200px; background-color: rgba(0, 0, 0, 0.5); color: white; display: flex; justify-content: center; align-items: center; font-size: 20px; } .front { transform: translateZ(100px); } .back { transform: translateZ(-100px) rotateY(180deg); } .left { transform: translateX(-100px) rotateY(-90deg); } .right { transform: translateX(100px) rotateY(90deg); } .top { transform: translateY(-100px) rotateX(90deg); } .bottom { transform: translateY(100px) rotateX(-90deg); }
This code will create a 200x200 pixel cube with a translucent black background color on each side. The transformation of each face in 3D space can be achieved through the transform attribute. For example, by giving one of the faces translateZ(100px), you can move it forward 100 pixels relative to the viewpoint, thus forming the front face of the cube.
2. Perspective attribute:
The perspective attribute is used to set the perspective effect of the element. By setting the position and observation distance of the perspective point, it can affect the performance of the 3D transformation effect. Perspective also needs to be used with the transform attribute.
You can set the perspective attribute to make elements produce far and near effects in 3D space. For example:
.cube { width: 200px; height: 200px; position: relative; transform: rotateX(45deg) rotateY(45deg); transform-style: preserve-3d; perspective: 1000px; }
In this example, by setting perspective: 1000px, we give the element a far and near effect of 1000 pixels. You can adjust the distance of an element in 3D space by changing the perspective value.
Summary:
By using the transform and perspective properties of CSS, we can easily add 3D effects to web pages. The transform attribute can implement transformation operations such as rotation, scaling, movement, and tilt of elements in 3D space. The perspective attribute can control the perspective effect of elements in 3D space. By combining the two, we are able to create a rich variety of 3D effects.
Reference:
(Note: The above example code is only for demonstration effect, actual application needs to be based on specific needs Make adjustments)
The above is the detailed content of Interpretation of CSS 3D view properties: transform and perspective. For more information, please follow other related articles on the PHP Chinese website!