CSS的background背景屬性有哪些

小編給大家分享一下CSS的background背景屬性有哪些,相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)長(zhǎng)期為千余家客戶提供的網(wǎng)站建設(shè)服務(wù),團(tuán)隊(duì)從業(yè)經(jīng)驗(yàn)10年,關(guān)注不同地域、不同群體,并針對(duì)不同對(duì)象提供差異化的產(chǎn)品和服務(wù);打造開放共贏平臺(tái),與合作伙伴共同營(yíng)造健康的互聯(lián)網(wǎng)生態(tài)環(huán)境。為鐵西企業(yè)提供專業(yè)的成都網(wǎng)站設(shè)計(jì)、做網(wǎng)站,鐵西網(wǎng)站改版等技術(shù)服務(wù)。擁有10年豐富建站經(jīng)驗(yàn)和眾多成功案例,為您定制開發(fā)。

文檔樹中的每個(gè)元素只是一個(gè)矩形盒子,這些盒子都有一個(gè)背景層,背景層可以是完全透明或者其它顏色,也可以是一張圖片。此背景層由8個(gè)CSS屬性(加上1個(gè)簡(jiǎn)寫的屬性)控制。

background-color

background-color屬性設(shè)置元素的背景顏色。它的值可以是任意合法的顏色值或者是transparent關(guān)鍵字。

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

相關(guān)教程:CSS視頻教程

背景顏色繪制在由[background-clip](#backgroundclip)屬性指定的盒模型的區(qū)域內(nèi)。如果還設(shè)置了任何背景圖像,則在它們后面繪制顏色層。與可以有多個(gè)的圖像層不同,對(duì)于一個(gè)元素,我們只能有一個(gè)顏色層。

background-image

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

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

CSS的background背景屬性有哪些

我們也可以指定多張背景圖片并通過逗號(hào)分隔。后面的圖片都會(huì)繪制在Z軸方向上前一個(gè)圖片的后面。

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

CSS的background背景屬性有哪些

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; }

CSS的background背景屬性有哪些

background-size

background-size屬性定義背景圖片的大小,它的值可以是關(guān)鍵字,長(zhǎng)度或者百分比。

可用于此屬性的關(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 */ }

CSS的background背景屬性有哪些

對(duì)于長(zhǎng)度和百分比,我們可以同時(shí)指定背景圖片的寬高,百分比值是根據(jù)元素的大小計(jì)算的。

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

CSS的background背景屬性有哪些

background-attachment

background-attachment屬性控制控制背景圖像相對(duì)于視口和元素的滾動(dòng)方式 。它有三個(gè)潛在的值。

fixed意味著背景圖片固定在視口并且不會(huì)移動(dòng),即使用戶正沿著視口滾動(dòng)。local意味著背景圖片固定在它在元素中的位置。如果這個(gè)元素可以滾動(dòng)并且背景圖片定位在頂部,那么當(dāng)用戶向下滾動(dòng)這個(gè)元素,背景圖片將會(huì)從視圖中滾出去。最后scroll意味著背景圖片是固定的且不會(huì)隨著元素內(nèi)容的滾動(dòng)而滾動(dòng)。

.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)鍵字,長(zhǎng)度或者百分比,我們可以指定沿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 */ }

CSS的background背景屬性有哪些

對(duì)于長(zhǎng)度和百分比,我們也可以指定沿x軸和y軸的位置。百分比值是按元素的大小計(jì)算的。

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

CSS的background背景屬性有哪些

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*/ }

CSS的background背景屬性有哪些

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*/ }

CSS的background背景屬性有哪些

background

最后,background屬性是其他背景相關(guān)屬性的簡(jiǎn)寫。子屬性的順序無(wú)關(guān)緊要,因?yàn)槊總€(gè)屬性的數(shù)據(jù)類型不同。然而對(duì)于background-origin 和 background-clip,如果只指定了一個(gè)盒模型區(qū)域,那么這兩個(gè)屬性都會(huì)應(yīng)用這個(gè)值。如果指定了兩個(gè),那么第一個(gè)值將用于background-origin屬性。

以上是“CSS的background背景屬性有哪些”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

網(wǎng)站名稱:CSS的background背景屬性有哪些
文章起源:http://www.muchs.cn/article46/ishshg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供App開發(fā)、網(wǎng)站策劃App設(shè)計(jì)、外貿(mào)網(wǎng)站建設(shè)、品牌網(wǎng)站設(shè)計(jì)、靜態(tài)網(wǎng)站

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)

外貿(mào)網(wǎng)站制作