談談CSS中background的8個屬性

來源:互聯網
上載者:User
像我之前提到的那樣,文檔樹中的每個元素只是一個矩形盒子。這些盒子都有一個背景層,背景層可以是完全透明或者其它顏色,也可以是一張圖片。此背景層由8個CSS屬性(加上1個簡寫的屬性)控制。

background-color

background-color屬性設定元素的背景顏色。它的值可以是任意合法的顏色值或者是transparent關鍵字。

.left { background-color: #ffdb3a; }.middle { background-color: #67b3dd; }.right { background-color: transparent; }

背景顏色繪製在由[background-clip](#backgroundclip)屬性指定的盒模型的地區內。如果還設定了任何背景映像,則在它們後面繪製顏色層。與可以有多個的映像層不同,對於一個元素,我們只能有一個顏色層。

background-image

background-image屬性定義元素的一個或多個背景映像。它的值通常是用url()符號定義的映像的url。也可以使用none作為它的值,但這樣會產生一個空的背景層

.left { background-image: url('ire.png'); }.right { background-image: none; }

我們也可以指定多張背景圖片並通過逗號分隔。後面的圖片都會繪製在Z軸方向上前一個圖片的後面。

.middle {   background-image: url('khaled.png'), url('ire.png');  /* Other styles */  background-repeat: no-repeat;   background-size: 100px;}

background-repeat

background-repeat屬性控制背景圖片在被[background-size](#backgroundsize)屬性改變了大小及被[background-position](#backgroundposition )屬性定位後如何平鋪。

該屬性的值可以是 repeat-x, repeat-y, repeat, space, round, no-repeat關鍵字,除了repeat-x和repeat-y,其他值可以為x軸和y軸定義一次,也可以單獨定義每個維。

.top-outer-left { background-repeat: repeat-x; }.top-inner-left { background-repeat: repeat-y; }.top-inner-right { background-repeat: repeat; }.top-outer-right { background-repeat: space; }.bottom-outer-left { background-repeat: round; }.bottom-inner-left { background-repeat: no-repeat; }.bottom-inner-right { background-repeat: space repeat; }.bottom-outer-right { background-repeat: round space; }

background-size

background-size屬性定義背景圖片的大小,它的值可以是關鍵字,長度或者百分比。

可用於此屬性的關鍵字為“contains”和“cover”。contain將等比縮放映像到最大的大小。另一方面,cover將把映像縮放到儘可能小的尺寸,其中整個背景地區仍然被覆蓋。

.left {   background-size: contain;  background-image: url('ire.png');   background-repeat: no-repeat;}.right { background-size: cover; /* Other styles same as .left */ }

對於長度和百分比,我們可以同時指定背景圖片的寬高,百分比值是根據元素的大小計算的。

.left { background-size: 50px; /* Other styles same as .left */ }.right { background-size: 50% 80%; /* Other styles same as .left */ }

background-attachment

background-attachment屬性控制控制背景映像相對於視口和元素的滾動方式 。它有三個潛在的值。

fixed意味著背景圖片固定在視口並且不會移動,即使使用者正沿著視口滾動。local意味著背景圖片固定在它在元素中的位置。如果這個元素可以滾動並且背景圖片定位在頂部,那麼當使用者向下滾動這個元素,背景圖片將會從視圖中滾出去。最後scroll意味著背景圖片是固定的且不會隨著元素內容的滾動而滾動。

.left {   background-attachment: fixed;  background-size: 50%;  background-image: url('ire.png');   background-repeat: no-repeat;  overflow: scroll;}.middle { background-attachment: local; /* Other styles same as .left */ }.right { background-attachment: scroll; /* Other styles same as .left */ }

background-position

這個屬性結合background-origin屬性定義背景圖片的起始位置應在何處。它的值可以是關鍵字,長度或者百分比,我們可以指定沿x軸和y軸的位置。

可用於此屬性的關鍵字為top, right, bottom, left, 和center,我們可以任意組合這些關鍵字,如果只明確指定了一個關鍵字,那麼另外一個預設就是center。

.top-left {   background-position: top;  background-size: 50%;  background-image: url('ire.png');   background-repeat: no-repeat;}.top-middle { background-position: right;  /* Other styles same as .top-left */ }.top-right { background-position: bottom;  /* Other styles same as .top-left */ }.bottom-left { background-position: left;  /* Other styles same as .top-left */ }.bottom-right { background-position: center;  /* Other styles same as .top-left */ }

對於長度和百分比,我們也可以指定沿x軸和y軸的位置。百分比值是按元素的大小計算的。

.left { background-position: 20px 70px; /* Others same as .top-left */ }.right { background-position: 50%; /* Others same as .top-left */ }

background-origin

background-origin屬性指定背景圖片應根據盒模型的哪個地區進行定位。

當值為border-box時,背景圖片的位置根據邊框地區定位,為padding-box時其位置根據邊距地區定位,為content-box時其位置根據內容地區定位。

.left {   background-origin: border-box;  background-size: 50%;  background-image: url('ire.png');   background-repeat: no-repeat;  background-position: top left;   border: 10px dotted black;   padding: 20px;}.middle { background-origin: padding-box;  /* Other styles same as .left*/ }.right { background-origin: content-box;  /* Other styles same as .left*/ }

background-clip

background-clip屬性確定背景繪製地區,這是背景可以被繪製的地區。和background-origin屬性一樣,它也 基於盒子模型的地區。

.left{   background-clip: border-box;  background-size: 50%;  background-color: #ffdb3a;   background-repeat: no-repeat;  background-position: top left;   border: 10px dotted black;   padding: 20px;}.middle { background-clip: padding-box;  /* Other styles same as .left*/ }.right { background-clip: content-box;  /* Other styles same as .left*/ }

background

最後,background屬性是其他背景相關屬性的簡寫。子屬性的順序無關緊要,因為每個屬性的資料類型不同。然而對於background-origin 和 background-clip,如果只指定了一個盒模型地區,那麼這兩個屬性都會應用這個值。如果指定了兩個,那麼第一個值將用於background-origin屬性。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.