當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


CSS perspective屬性用法及代碼示例


透視圖是2D平麵上3D對象的表示,以給出彼此之間的外觀深度和距離。 CSS透視圖屬性用於為3D對象提供透視圖。定義視角屬性時,子元素獲得視角,而不是父元素。因此,該值定義了子代與Z平麵的距離。

用法:

perspective:length | none | initial | inherit;

屬性值:

  • length:length設置子元素從查看器到Z平麵的距離。因此,值越小,形成的圖像越出色。

    用法:



    perspective:length;

    例:

    <!DOCTYPE html> 
    <html> 
      
    <head> 
        <title> 
            CSS | perspective Property 
        </title> 
        <style> 
            .class1 { 
                margin-top:20px; 
                margin-left:40px; 
                height:200px; 
                width:200px; 
                color:#0F9D58; 
                border:1px solid; 
                
                <!--length value of perspective property--> 
                perspective:200px; 
            } 
              
            .class2 { 
                padding:60px; 
                border:2px solid; 
                background:#0F9D58; 
                color:white; 
                transform:rotateX(45deg) rotateY(45deg); 
            } 
        </style> 
    </head> 
      
    <body> 
        <div class="class1">GeeksforGeeks 
            <div class="class2">GeeksforGeeks</div> 
        </div> 
    </body> 
      
    </html>

    輸出:

  • none:默認值是none。它指示未設置透視圖。 none等於0px。
    用法:
    perspective: none;

    例:

    <!DOCTYPE html> 
    <html> 
      
    <head> 
        <title> 
            CSS | perspective Property 
        </title> 
        <style> 
            .class1 { 
                margin-top:20px; 
                margin-left:40px; 
                height:200px; 
                width:200px; 
                color:#0F9D58; 
                border:1px solid; 
                
                <!--none value of perspective property--> 
                perspective:none; 
            } 
              
            .class2 { 
                padding:60px; 
                border:2px solid; 
                background:#0F9D58; 
                color:white; 
                transform:rotateX(45deg) rotateY(45deg); 
            } 
        </style> 
    </head> 
      
    <body> 
        <div class="class1">GeeksforGeeks 
            <div class="class2">GeeksforGeeks</div> 
        </div> 
    </body> 
      
    </html>

    輸出:

  • initial:初始設置默認值,即未設置任何透視圖。
    用法:
    perspective: initial ;

    例:

    <!DOCTYPE html> 
    <html> 
      
    <head> 
        <title> 
            CSS | perspective Property 
        </title> 
        <style> 
            .class1 { 
                margin-top:20px; 
                margin-left:40px; 
                height:200px; 
                width:200px; 
                color:#0F9D58; 
                border:1px solid; 
                
                <!--initial value of perspective property--> 
                perspective:initial; 
            } 
              
            .class2 { 
                padding:60px; 
                border:2px solid; 
                background:#0F9D58; 
                color:white; 
                transform:rotateX(45deg) rotateY(45deg); 
            } 
        </style> 
    </head> 
      
    <body> 
        <div class="class1">GeeksforGeeks 
            <div class="class2">GeeksforGeeks</div> 
        </div> 
    </body> 
      
    </html>

    輸出:

  • inherit:繼承從父元素接收屬性。與根元素一起使用時,將使用初始屬性。
    用法:
    perspective: inherit;

    例:

    <!DOCTYPE html> 
    <html> 
      
    <head> 
        <title> 
            CSS | perspective Property 
        </title> 
        <style> 
            .class0 { 
                perspective:150px; 
            } 
              
            .class1 { 
                margin-top:20px; 
                margin-left:40px; 
                height:200px; 
                width:200px; 
                color:#0F9D58; 
                border:1px solid; 
                
                <!--inherit value of perspective property--> 
                perspective:inherit; 
            } 
              
            .class2 { 
                padding:60px; 
                border:2px solid; 
                background:#0F9D58; 
                color:white; 
                transform:rotateX(45deg) rotateY(45deg); 
            } 
        </style> 
    </head> 
      
    <body> 
        <div class="class0"> 
            <div class="class1">GeeksforGeeks 
                <div class="class2">GeeksforGeeks</div> 
            </div> 
        </div> 
    </body> 
      
    </html>

    輸出:

支持的瀏覽器:透視圖屬性支持的瀏覽器如下所示:

  • 穀歌瀏覽器36.0
  • Internet Explorer 10.0
  • Firefox 16.0
  • 歐寶23.0
  • Safari 9.0




相關用法


注:本文由純淨天空篩選整理自gribeshdhakal大神的英文原創作品 CSS | perspective Property。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。