簡述CSS中的背景屬性background

像我之前提到的那樣,文檔樹中的每個(gè)元素只是一個(gè)矩形盒子。這些盒子都有一個(gè)背景層,背景層可以是完全透明或者其它顏色,也可以是一張圖片。此背景層由8個(gè)CSS屬性(加上1個(gè)簡寫的屬性)控制。
background-color
background-color屬性設(shè)置元素的背景顏色。它的值可以是任意合法的顏色值或者是transparent關(guān)鍵字。
.left { background-color: #ffdb3a; } .middle { background-color: #67b3dd; } .right { background-color: transparent; }
背景顏色繪制在由[background-clip](#backgroundclip)
屬性指定的盒模型的區(qū)域內(nèi)。如果還設(shè)置了任何背景圖像,則在它們后面繪制顏色層。與可以有多個(gè)的圖像層不同,對于一個(gè)元素,我們只能有一個(gè)顏色層。
background-image
background-image屬性定義元素的一個(gè)或多個(gè)背景圖像。它的值通常是用url()符號定義的圖像的url。也可以使用none作為它的值,但這樣會生成一個(gè)空的背景層
.left { background-image: url('ire.png'); } .right { background-image: none; }
我們也可以指定多張背景圖片并通過逗號分隔。后面的圖片都會繪制在Z軸方向上前一個(gè)圖片的后面。
.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
關(guān)鍵字,除了repeat-x和repeat-y,其他值可以為x軸和y軸定義一次,也可以單獨(dú)定義每個(gè)維。
.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屬性定義背景圖片的大小,它的值可以是關(guān)鍵字,長度或者百分比。
可用于此屬性的關(guān)鍵字為“contains”和“cover”。contain將等比縮放圖像到最大的大小。另一方面,cover將把圖像縮放到盡可能小的尺寸,其中整個(gè)背景區(qū)域仍然被覆蓋。
.left { background-size: contain; background-image: url('ire.png'); background-repeat: no-repeat; } .right { background-size: cover; /* Other styles same as .left */ }
對于長度和百分比,我們可以同時(shí)指定背景圖片的寬高,百分比值是根據(jù)元素的大小計(jì)算的。
.left { background-size: 50px; /* Other styles same as .left */ } .right { background-size: 50% 80%; /* Other styles same as .left */ }
background-attachment
background-attachment屬性控制控制背景圖像相對于視口和元素的滾動方式 。它有三個(gè)潛在的值。
fixed意味著背景圖片固定在視口并且不會移動,即使用戶正沿著視口滾動。local意味著背景圖片固定在它在元素中的位置。如果這個(gè)元素可以滾動并且背景圖片定位在頂部,那么當(dāng)用戶向下滾動這個(gè)元素,背景圖片將會從視圖中滾出去。最后scroll意味著背景圖片是固定的且不會隨著元素內(nèi)容的滾動而滾動。
.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
這個(gè)屬性結(jié)合background-origin屬性定義背景圖片的起始位置應(yīng)在何處。它的值可以是關(guān)鍵字,長度或者百分比,我們可以指定沿x軸和y軸的位置。
可用于此屬性的關(guān)鍵字為top, right, bottom, left, 和center,我們可以任意組合這些關(guān)鍵字,如果只明確指定了一個(gè)關(guān)鍵字,那么另外一個(gè)默認(rèn)就是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軸的位置。百分比值是按元素的大小計(jì)算的。
.left { background-position: 20px 70px; /* Others same as .top-left */ } .right { background-position: 50%; /* Others same as .top-left */ }
background-origin
background-origin屬性指定背景圖片應(yīng)根據(jù)盒模型的哪個(gè)區(qū)域進(jìn)行定位。
當(dāng)值為border-box時(shí),背景圖片的位置根據(jù)邊框區(qū)域定位,為padding-box時(shí)其位置根據(jù)邊距區(qū)域定位,為content-box時(shí)其位置根據(jù)內(nèi)容區(qū)域定位。
.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屬性確定背景繪制區(qū)域,這是背景可以被繪制的區(qū)域。和background-origin屬性一樣,它也 基于盒子模型的區(qū)域。
.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屬性是其他背景相關(guān)屬性的簡寫。子屬性的順序無關(guān)緊要,因?yàn)槊總€(gè)屬性的數(shù)據(jù)類型不同。然而對于background-origin 和 background-clip,如果只指定了一個(gè)盒模型區(qū)域,那么這兩個(gè)屬性都會應(yīng)用這個(gè)值。如果指定了兩個(gè),那么第一個(gè)值將用于background-origin屬性。
總結(jié)
以上所述是小編給大家介紹的CSS中的背景屬性background,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時(shí)回復(fù)大家的!
相關(guān)文章
- 本文通過實(shí)例代碼給大家介紹了CSS定義字體、顏色、背景等屬性的相關(guān)知識,非常不錯(cuò),具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2018-08-01
css3實(shí)現(xiàn)一個(gè)div設(shè)置多張背景圖片及background-image屬性實(shí)例演示
這篇文章主要介紹了css3實(shí)現(xiàn)一個(gè)div設(shè)置多張背景圖片及background-image屬性,同時(shí)對于css3背景漸變也做了詳細(xì)的解釋,水平漸變,左上角漸變等等方式,需要的朋友可以參考下2017-08-10css 背景固定樣式background-attachment屬性基礎(chǔ)
這篇文章主要為大家介紹了在CSS中,使用背景附件屬性background-attachment可以設(shè)置背景圖像是隨對象滾動還是固定不動,需要的朋友可以參考下2017-03-08CSS的background屬性及CSS3的背景圖片設(shè)置總結(jié)
這篇文章主要介紹了CSS的background屬性及CSS3的背景圖片設(shè)置總結(jié),背景圖片的顯示區(qū)域和定位是非常值得注意的地方,需要的朋友可以參考下2016-06-13- CSS3新增屬性:background-clip ,background-origin , background-size,本文給大家分享CSS3新增的背景屬性,感興趣的朋友跟隨小編一起看看吧2019-12-25