Computer >> 컴퓨터 >  >> 프로그래밍 >> CSS

CSS perspective-origin 속성으로 3D 요소 원점 위치 설정하기

CSS perspective-origin 속성이란?

perspective-origin 속성은 3D 변환된 요소를 볼 때 관찰자(시점)가 바라보는 위치, 즉 소실점의 기준 위치를 지정하는 CSS 속성입니다. 이 속성은 perspective 속성과 함께 사용되며, 3D 요소가 화면에 어떤 각도로 보이는지 결정합니다.

기본값은 50% 50%으로, 부모 요소의 중앙을 시점 기준으로 삼습니다. 이 값을 조절하면 3D 효과의 방향과 깊이감이 달라집니다.

주요 사용 가능한 값

  • 키워드 값: left, center, right, top, bottom
  • 백분율(%): 요소 크기 기준으로 지정 (예: 25% 75%)
  • 길이 단위(px 등): 정확한 픽셀 값으로 지정

perspective-origin 예제 코드

아래 코드는 perspective-origin 속성을 활용해 3D 요소의 시점을 왼쪽으로 설정하는 예제입니다. 직접 실행해 결과를 확인해 보세요.

<!DOCTYPE html>
<html>
   <head>
      <style>
         .demo1 {
            position: relative;
            width: 150px;
            height: 150px;
            background-color: yellow;
            perspective: 80px;
            margin: 50px;
            perspective-origin: left;
         }
         .demo2 {
            position: absolute;
            padding: 20px;
            background-color: orange;
            transform-style: preserve-3d;
            transform: rotateX(45deg);
         }
      </style>
   </head>

   <body>
      <h1>Rotation</h1>
      <div class = "demo1">Demo
         <div class = "demo2">Demo</div>
      </div>
   </body>
</html>

코드 설명

위 예제에서 부모 요소인 .demo1에는 perspective: 80px;로 원근감의 깊이를 설정하고, perspective-origin: left;로 시점의 기준 위치를 왼쪽으로 지정했습니다. 자식 요소인 .demo2transform: rotateX(45deg);를 통해 X축 기준으로 45도 회전하며, transform-style: preserve-3d; 덕분에 3D 공간이 유지됩니다.

perspective-origin 값을 left, right, center 등으로 변경하면서 실행해 보면, 시점 위치에 따라 회전된 요소의 원근 효과가 어떻게 달라지는지 쉽게 확인할 수 있습니다.