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 屬性

瀏覽器相容性

另見