Home Web Front-end CSS Tutorial Interpretation of CSS 3D view properties: transform and perspective

Interpretation of CSS 3D view properties: transform and perspective

Oct 24, 2023 am 08:11 AM
transform css d perspective

CSS 3D 视图属性解读:transform 和 perspective

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:

  1. Rotation:
    transform: rotateX(45deg); // Rotate 45 degrees around the X axis
    transform: rotateY( 45deg); // Rotate 45 degrees around the Y axis
    transform: rotateZ(45deg); // Rotate 45 degrees around the Z axis
  2. Scale:
    transform: scale(2); // In Magnify twice on the X and Y axes
    transform: scaleX(2); // Magnify twice on the X axis
    transform: scaleY(2); // Magnify twice on the Y axis
  3. Move:
    transform: translateX(100px); // Move 100 pixels on the X axis
    transform: translateY(100px); // Move 100 pixels on the Y axis
    transform: translateZ(100px ); // Move 100 pixels on the Z axis
  4. Tilt:
    transform: skewX(45deg); // Tilt 45 degrees along the X axis
    transform: skewY(45deg); // Along The Y-axis is tilted 45 degrees

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>
Copy after login
.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);
}
Copy after login

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;
}
Copy after login

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:

  • CSS Transform (https://developer.mozilla.org/zh-CN/docs/Web/CSS/transform)
  • CSS Perspective (https://developer.mozilla.org/zh-CN/docs/Web/CSS/perspective)

(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!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Making Your First Custom Svelte Transition Making Your First Custom Svelte Transition Mar 15, 2025 am 11:08 AM

The Svelte transition API provides a way to animate components when they enter or leave the document, including custom Svelte transitions.

Working With GraphQL Caching Working With GraphQL Caching Mar 19, 2025 am 09:36 AM

If you’ve recently started working with GraphQL, or reviewed its pros and cons, you’ve no doubt heard things like “GraphQL doesn’t support caching” or

Show, Don't Tell Show, Don't Tell Mar 16, 2025 am 11:49 AM

How much time do you spend designing the content presentation for your websites? When you write a new blog post or create a new page, are you thinking about

Building an Ethereum app using Redwood.js and Fauna Building an Ethereum app using Redwood.js and Fauna Mar 28, 2025 am 09:18 AM

With the recent climb of Bitcoin’s price over 20k $USD, and to it recently breaking 30k, I thought it’s worth taking a deep dive back into creating Ethereum

Creating Your Own Bragdoc With Eleventy Creating Your Own Bragdoc With Eleventy Mar 18, 2025 am 11:23 AM

No matter what stage you’re at as a developer, the tasks we complete—whether big or small—make a huge impact in our personal and professional growth.

What the Heck Are npm Commands? What the Heck Are npm Commands? Mar 15, 2025 am 11:36 AM

npm commands run various tasks for you, either as a one-off or a continuously running process for things like starting a server or compiling code.

A bit on ci/cd A bit on ci/cd Apr 02, 2025 pm 06:21 PM

I&#039;d say "website" fits better than "mobile app" but I like this framing from Max Lynch:

Let's use (X, X, X, X) for talking about specificity Let's use (X, X, X, X) for talking about specificity Mar 24, 2025 am 10:37 AM

I was just chatting with Eric Meyer the other day and I remembered an Eric Meyer story from my formative years. I wrote a blog post about CSS specificity, and

See all articles