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


CSS background-origin用法及代碼示例


background-origin是CSS中定義的屬性,可幫助調整網頁的背景圖像。此屬性用於在背景中設置圖像的原點。默認情況下,此屬性將背景圖像原點設置為屏幕/網頁的左上角。

用法:

background-origin:padding-box|border-box|content-box|initial|
inherit;

屬性值:
initial:這采用將背景原點設置為左上角的填充邊的初始值/默認值。


  • 用法:
    background-origin:initial;
  • Example:
    <!DOCTYPE html> 
    <html> 
      
    <head> 
        <title>background-origin property</title> 
        <style> 
            .gfg { 
                padding:40px; 
                width:400px; 
                background-image:  
    url('https://media.geeksforgeeks.org/wp-content/cdn-uploads/gfg_200X200.png'); 
                background-repeat:no-repeat; 
                background-origin:intial; 
                border:1px double; 
                text-align:justify; 
            } 
        </style> 
    </head> 
      
    <body> 
        <div class="gfg"> 
            <p> 
              Prepare for the Recruitment drive of product 
              based companies like Microsoft, Amazon, Adobe 
              etc with a free online placement preparation 
              course. The course focuses on various MCQ's  
              & Coding question likely to be asked in the  
              interviews & make your upcoming placement  
              season efficient and successful.  
            </p> 
        </div> 
    </body> 
      
    </html>
  • Output:
    initial prop

padding-box:此屬性用於將背景圖像的原點設置為左上角的填充邊。

  • 用法:
    background-origin:padding-box;
  • 例:
    <!DOCTYPE html> 
    <html> 
      
    <head> 
        <title>background-origin property</title> 
        <style> 
            .gfg { 
                padding:40px; 
                width:400px; 
                background-image:  
    url('https://media.geeksforgeeks.org/wp-content/cdn-uploads/gfg_200X200.png'); 
                background-repeat:no-repeat; 
                background-origin:padding-box; 
                border:1px double; 
                text-align:justify; 
            } 
        </style> 
    </head> 
      
    <body> 
        <div class="gfg"> 
            <p> 
              Prepare for the Recruitment drive of 
              product based companies like Microsoft, 
              Amazon, Adobe etc with a free online  
              placement preparation course. The course 
              focuses on various MCQ's & Coding question 
              likely to be asked in the interviews & 
              make your upcoming placement season  
              efficient and successful.  
            </p> 
        </div> 
    </body> 
      
    </html>      
  • 輸出:
    padding box

border-box:此屬性用於將圖像設置為網頁正文的邊界,即絕對左上角。

  • 用法:
    background-origin:border-box;
  • 例:
    <!DOCTYPE html> 
    <html> 
      
    <head> 
        <title>background-origin property</title> 
        <style> 
            .gfg { 
                padding:40px; 
                width:400px; 
                background-image:  
    url('https://media.geeksforgeeks.org/wp-content/cdn-uploads/gfg_200X200.png'); 
                background-repeat:no-repeat; 
                background-origin:border-box; 
                border:1px double; 
                text-align:justify; 
            } 
        </style> 
    </head> 
      
    <body> 
        <div class="gfg"> 
            <p> 
              Prepare for the Recruitment drive of  
              product based companies like Microsoft, 
              Amazon, Adobe etc with a free online  
              placement preparation course. The course 
              focuses on various MCQ's & Coding question  
              likely to be asked in the interviews & make 
              your upcoming placement season efficient  
              and successful.  
            </p> 
        </div> 
    </body> 
      
    </html>    
  • 輸出:
    border box

content-box:此屬性用於根據使用該屬性的部門/主體的內容來設置背景的原點。

  • 用法:
    background-origin:content-box;
  • 例:
    <!DOCTYPE html> 
    <html> 
      
    <head> 
        <title>background-origin property</title> 
        <style> 
            .gfg { 
                padding:40px; 
                width:400px; 
                background-image:  
    url('https://media.geeksforgeeks.org/wp-content/cdn-uploads/gfg_200X200.png'); 
                background-repeat:no-repeat; 
                background-origin:content-box; 
                border:1px double; 
                text-align:justify; 
            } 
        </style> 
    </head> 
      
    <body> 
        <div class="gfg"> 
            <p> 
              Prepare for the Recruitment drive of 
              product based companies like Microsoft, 
              Amazon, Adobe etc with a free online 
              placement preparation course. The course 
              focuses on various MCQ's & Coding question 
              likely to be asked in the interviews & make 
              your upcoming placement season efficient  
              and successful. 
            </p> 
        </div> 
    </body> 
      
    </html>
  • 輸出:
    content box

繼承:用於從已使用的代碼繼承屬性(父屬性),否則默認情況下用作padding-box。

  • 用法:
    background-origin:inherit;
  • 例:
    <!DOCTYPE html> 
    <html> 
      
    <head> 
        <title>background-origin property</title> 
        <style> 
            .gfg { 
                padding:40px; 
                width:400px; 
                background-image:
    url('https://media.geeksforgeeks.org/wp-content/cdn-uploads/gfg_200X200.png'); 
                background-repeat:no-repeat; 
                background-origin:inherit; 
                border:1px double; 
                text-align:justify; 
            } 
        </style> 
    </head> 
      
    <body> 
        <div class="gfg"> 
            <p> 
              Prepare for the Recruitment drive of  
              product based companies like Microsoft, 
              Amazon, Adobe etc with a free online  
              placement preparation course. The course 
              focuses on various MCQ's & Coding question 
              likely to be asked in the interviews & make  
              your upcoming placement season efficient and 
              successful.  
            </p> 
        </div> 
    </body> 
      
    </html>      
  • 輸出:
    initial prop

支持的瀏覽器:CSS | Java支持的瀏覽器。下麵列出了background-origin屬性:

  • 穀歌瀏覽器
  • IE瀏覽器
  • Firefox
  • Opera
  • Safari


相關用法


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