search
HomeWeb Front-endCSS TutorialA brief discussion on the similarities and differences between the css perspective attribute and the perspective() function

What is the difference between the css perspective attribute and the perspective() function? This article will briefly compare the perspective attribute and the perspective() function to understand the similarities and differences between them.

A brief discussion on the similarities and differences between the css perspective attribute and the perspective() function

perspective

Perspective propertyperspective Used to activate three-dimensional space on an element so that its children can be positioned in this space.

It allows you to add a sense of depth to your scene by making elements higher on the z-axis (closer to the viewer) appear larger, while elements farther away appear smaller.

A brief discussion on the similarities and differences between the css perspective attribute and the perspective() function

#The blue circle in this image represents an element in three-dimensional space. The letter d represents the perspective value, which is the assumed distance between the observer's eyes and the screen. The letter Z represents the position of the element on the z-axis. The farther an element is on the z-axis, the smaller it appears relative to the viewer, and the closer it is, the larger it appears. This is the effect of perspective in three-dimensional space.

Without specifying perspective, elements transformed using the 3D transformation function will appear flat and 2D.

Perspective propertiesperspective Used in conjunction with CSS transforms. It requires a length value or the keyword none. Use this property with any value other than none to establish a stacking context. It also creates a containing block (somewhat similar to position:relative), just like the transform property does.

The following image shows the results of transforming (rotating) an element in three-dimensional space with and without specifying perspective.

A brief discussion on the similarities and differences between the css perspective attribute and the perspective() function

#Use the rotateY() function to rotate the image 50 degrees on the y-axis. The image on the left shows the result of applying rotation while specifying perspective, and the image on the right shows the same transformation without perspective. In the image on the right, the transformation appears to be planar and two-dimensional because no three-dimensional space is triggered.

By default, the vanishing point of 3D space is at the center. (It can be changed using the perspective-origin property perspective-origin.)

Official syntax

  • Statement :
perspective: none | <length>

Values

none

No perspective is applied, so three-dimensional space is not triggered .

Specifies the assumed distance between the observer and the z=0 plane. It is used to calculate the perspective matrix that applies the perspective transformation to the element and its contents. If set to zero, no perspective is applied. Negative values ​​are not allowed. See the entry for a list of possible values.

Example

The following sets the perspective of an element, and the child elements of the element are transformed in the three-dimensional space triggered by the perspective attribute:

.container {
    perspective: 1800px;
}

.container .child {
    float: left;
    margin: 50px;
    transform: translateZ(-50px) rotateY(45deg);
}

Online Demo

The perspective effect of elements can best be demonstrated on 3D shapes (such as cubes).

In this demonstration, there are three identical cubes, all with the same transformation. They each have a different set of perspectives. Try changing the perspective value to see how the depth of the scene changes.

The higher the perspective value, the weaker the effect, the lower the perspective value, the stronger the effect.

Because Nuggets does not support the introduction of iframe preview, the preview effect can be seen in codepen css perspective demo

A brief discussion on the similarities and differences between the css perspective attribute and the perspective() function

perspective vs perspective()

When you have an element that you want to transform in 3D space, you can activate that space using the perspective property of its parent elementperspective , or use the perspective function perspective() that converts the element itself. So what is the difference between these two methods?

Perspective propertiesperspective and perspective functionsperspective() are both used to make elements that are farther away appear larger by making elements that are higher on the z-axis (closer to the viewer) appear larger. Smaller to give the element depth. The lower the value, the closer the z-pane is to the viewer, and the more impressive the effect; the higher the value, the farther the element is from the screen, and the more subtle the effect.

When you apply perspective to an element using the perspective function perspective() (see the Transform attribute entry transform for details on how this works), you Three-dimensional space is activated only on this element. The perspective() notation is convenient when applying three-dimensional transformations to individual elements. But when you have multiple transformed elements in a container, if each element has a perspective set using the perspective function perspective(), the elements will not line up as expected. This is because each of them has its own three-dimensional space and therefore its own vanishing point. To avoid this and allow elements to align, they should share the same space. By using the perspective property perspective on the parent container, a three-dimensional space is created that is shared by all of its child elements, which are transformed using a 3D transform.

The image below shows the results of triggering a three-dimensional space on the container, in which the container's children are rotated (left), and using the perspective function perspective() on each item The result of triggering a three-dimensional space (right):

A brief discussion on the similarities and differences between the css perspective attribute and the perspective() function

Reference translation

The main reference translation of this article is from:

  • https://tympanus.net/codrops/css_reference/perspective/

But because the original text is a bit too wordy, the chapters and table of contents have been adjusted.

Original address: https://juejin.cn/post/6978259550881677349

For more programming-related knowledge, please visit: Programming Video! !

The above is the detailed content of A brief discussion on the similarities and differences between the css perspective attribute and the perspective() function. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:掘金社区. If there is any infringement, please contact admin@php.cn delete
利用CSS怎么创建渐变色边框?5种方法分享利用CSS怎么创建渐变色边框?5种方法分享Oct 13, 2021 am 10:19 AM

利用CSS怎么创建渐变色边框?下面本篇文章给大家分享CSS实现渐变色边框的5种方法,希望对大家有所帮助!

css ul标签怎么去掉圆点css ul标签怎么去掉圆点Apr 25, 2022 pm 05:55 PM

在css中,可用list-style-type属性来去掉ul的圆点标记,语法为“ul{list-style-type:none}”;list-style-type属性可设置列表项标记的类型,当值为“none”可不定义标记,也可去除已有标记。

css与xml的区别是什么css与xml的区别是什么Apr 24, 2022 am 11:21 AM

区别是:css是层叠样式表单,是将样式信息与网页内容分离的一种标记语言,主要用来设计网页的样式,还可以对网页各元素进行格式化;xml是可扩展标记语言,是一种数据存储语言,用于使用简单的标记描述数据,将文档分成许多部件并对这些部件加以标识。

css3怎么实现鼠标隐藏效果css3怎么实现鼠标隐藏效果Apr 27, 2022 pm 05:20 PM

在css中,可以利用cursor属性实现鼠标隐藏效果,该属性用于定义鼠标指针放在一个元素边界范围内时所用的光标形状,当属性值设置为none时,就可以实现鼠标隐藏效果,语法为“元素{cursor:none}”。

css怎么实现英文小写转为大写css怎么实现英文小写转为大写Apr 25, 2022 pm 06:35 PM

转换方法:1、给英文元素添加“text-transform: uppercase;”样式,可将所有的英文字母都变成大写;2、给英文元素添加“text-transform:capitalize;”样式,可将英文文本中每个单词的首字母变为大写。

rtl在css是什么意思rtl在css是什么意思Apr 24, 2022 am 11:07 AM

在css中,rtl是“right-to-left”的缩写,是从右往左的意思,指的是内联内容从右往左依次排布,是direction属性的一个属性值;该属性规定了文本的方向和书写方向,语法为“元素{direction:rtl}”。

css怎么设置i不是斜体css怎么设置i不是斜体Apr 20, 2022 am 10:36 AM

在css中,可以利用“font-style”属性设置i元素不是斜体样式,该属性用于指定文本的字体样式,当属性值设置为“normal”时,会显示元素的标准字体样式,语法为“i元素{font-style:normal}”。

怎么设置rotate在css3的旋转中心点怎么设置rotate在css3的旋转中心点Apr 24, 2022 am 10:50 AM

在css3中,可以用“transform-origin”属性设置rotate的旋转中心点,该属性可更改转换元素的位置,第一个参数设置x轴的旋转位置,第二个参数设置y轴旋转位置,语法为“transform-origin:x轴位置 y轴位置”。

See all articles

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)
2 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Repo: How To Revive Teammates
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island Adventure: How To Get Giant Seeds
3 weeks agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.