perspective-origin

Baseline 已广泛支持

此特性已相当成熟,可在许多设备和浏览器版本上使用。自 2015 年 9 月以来,该特性已在各大浏览器中可用。

perspective-origin CSS 属性决定了观察者的视点位置。它被 perspective 属性用作消失点

试一试

perspective-origin: center;
perspective-origin: top;
perspective-origin: bottom right;
perspective-origin: -170%;
perspective-origin: 500% 200%;
<section class="default-example" id="default-example">
  <div class="transition-all" id="example-element">
    <div class="face front">1</div>
    <div class="face back">2</div>
    <div class="face right">3</div>
    <div class="face left">4</div>
    <div class="face top">5</div>
    <div class="face bottom">6</div>
  </div>
</section>
#default-example {
  background: linear-gradient(skyblue, khaki);
  perspective: 550px;
}

#example-element {
  width: 100px;
  height: 100px;
  transform-style: preserve-3d;
  perspective: 250px;
}

.face {
  display: flex;
  align-items: center;
  justify-content: center;
  width: 100%;
  height: 100%;
  position: absolute;
  backface-visibility: inherit;
  font-size: 60px;
  color: white;
}

.front {
  background: rgb(90 90 90 / 0.7);
  transform: translateZ(50px);
}

.back {
  background: rgb(0 210 0 / 0.7);
  transform: rotateY(180deg) translateZ(50px);
}

.right {
  background: rgb(210 0 0 / 0.7);
  transform: rotateY(90deg) translateZ(50px);
}

.left {
  background: rgb(0 0 210 / 0.7);
  transform: rotateY(-90deg) translateZ(50px);
}

.top {
  background: rgb(210 210 0 / 0.7);
  transform: rotateX(90deg) translateZ(50px);
}

.bottom {
  background: rgb(210 0 210 / 0.7);
  transform: rotateX(-90deg) translateZ(50px);
}

perspective-originperspective 属性是附加到在三维空间中进行转换的子元素的父元素上的,这与放置在被转换元素上的 perspective() 转换函数不同。

语法

css
/* One-value syntax */
perspective-origin: x-position;

/* Two-value syntax */
perspective-origin: x-position y-position;

/* When both x-position and y-position are keywords,
   the following is also valid */
perspective-origin: y-position x-position;

/* Global values */
perspective-origin: inherit;
perspective-origin: initial;
perspective-origin: revert;
perspective-origin: revert-layer;
perspective-origin: unset;

x-位置

表示消失点的横坐标位置。它可以是以下值之一:

  • <length-percentage>,表示绝对长度值或相对于元素宽度百分比的位置。该值可以是负数。
  • left,一个关键字,是长度值 0 的简写。
  • center,一个关键字,是百分比值 50% 的简写。
  • right,一个关键字,是百分比值 100% 的简写。
y-位置

表示消失点的纵坐标位置。它可以是以下值之一:

  • <length-percentage>,表示绝对长度值或相对于元素高度百分比的位置。该值可以是负数。
  • top,一个关键字,是长度值 0 的简写。
  • center,一个关键字,是百分比值 50% 的简写。
  • bottom,一个关键字,是百分比值 100% 的简写。

正式定义

初始值50% 50%
应用于可变换元素
继承性
百分比指边框的尺寸
计算值对于 <length> 则是绝对值,否则为百分比
动画类型长度、百分比或 calc 的简单列表

正式语法

perspective-origin = 
<position>

<position> =
<position-one> |
<position-two> |
<position-four>

<position-one> =
left |
center |
right |
top |
bottom |
x-start |
x-end |
y-start |
y-end |
block-start |
block-end |
inline-start |
inline-end |
<length-percentage>

<position-two> =
[ left | center | right | x-start | x-end ] && [ top | center | bottom | y-start | y-end ] |
[ left | center | right | x-start | x-end | <length-percentage> ] [ top | center | bottom | y-start | y-end | <length-percentage> ] |
[ block-start | center | block-end ] && [ inline-start | center | inline-end ] |
[ start | center | end ]{2}

<position-four> =
[ [ left | right | x-start | x-end ] <length-percentage> ] && [ [ top | bottom | y-start | y-end ] <length-percentage> ] |
[ [ block-start | block-end ] <length-percentage> ] && [ [ inline-start | inline-end ] <length-percentage> ] |
[ [ start | end ] <length-percentage> ]{2}

<length-percentage> =
<length> |
<percentage>

示例

更改透视原点

一个展示如何更改 perspective-origin 的示例在使用 CSS 转换 > 更改透视原点中给出。

规范

规范
CSS 变换模块级别 2
# perspective-origin 属性

浏览器兼容性

另见