先來一個簡單的背景設置:windows
1 #show-box { 2 width: 800px; 3 height: 500px; 4 background: #000; 5 background-image: url(image url); 6 } 7 </style>
這裏只是簡單的設置了顏色和背景貼圖。url
下面讓咱們來看一下官方的background的屬性:spa
語法格式:3d
background: color position size repeat origin clip attachment image;code
注意:若是同時設置了「position」和「size」兩個屬性,應該用左斜槓「/」,而不是用空格把兩個參數值隔開:「position/size」。blog
1 background: url("img.jpg") center center/100% 100% no-repeat;
屬性表(圖片可能會顯示得過小,請右鍵「在新標籤中打開」以查看原圖):圖片
可選值:默認是透明,其餘值能夠經過查看「CSS顏色值表」來設置。ip
1 <style> 2 #show-box { 3 width: 180px; 4 height: 180px; 5 border: 20px dashed #000; 6 background-color: #000000; 7 background-color: blue; 8 background-color: rgb(255, 255, 255); 9 background-color: rgba(255, 255, 255, 0.8); 10 } 11 </style>
可選值:兩個參數,水平位置和垂直位置。若是隻有一個值,第二個值爲「center」。it
默認值是元素的左上頂角。能夠使用位置關鍵字(top,right,bottom,left,center)。百分比(以元素大小爲基值)。像素值。io
1 <style> 2 #show-box { 3 width: 180px; 4 height: 180px; 5 border: 20px dashed #000; 6 background-position: center; 7 background-position: center top; 8 background-position: 0 100px; 9 background-position: 10% 20%; 10 } 11 </style>
可選值:兩個數值,若是隻有一個值,第二個值爲auto。
默認是圖片自身大小。能夠使用像素值,百分百(以元素大小爲基值)。
cover:等比例縮放圖片,覆蓋這個元素。相似與windows中桌面背景的「填充」。
contain:等比例縮放圖片,適應元素的寬或者高。相似於windows中桌面背景的「適應」。
repeat:徹底平鋪,複製圖片把整個元素填滿。(默認)
repeat-x:水平平鋪,在水平方向複製並平鋪。
repeat-y:垂直平鋪,在垂直方向複製並平鋪。
no-repeat:不平鋪,只使用一張圖片。
可選值:border-box,padding-box,content-box。
可選值:border-box,padding-box,content-box。
對比一下不一樣值的效果圖:
1.origin:border-box;clip:border-box;
1 <style> 2 #show-box { 3 width: 180px; 4 height: 180px; 5 margin: 20px; 6 padding: 20px; 7 border: 20px dashed #000; 8 background: url("img.jpg") no-repeat border-box border-box; 9 } 10 </style>
2.origin:padding-box;clip:border-box;
1 <style> 2 #show-box { 3 width: 180px; 4 height: 180px; 5 margin: 20px; 6 padding: 20px; 7 border: 20px dashed #000; 8 background: url("img.jpg") no-repeat padding-box border-box; 9 } 10 </style>
3.origin:content-box;clip:border-box;
1 <style> 2 #show-box { 3 width: 180px; 4 height: 180px; 5 margin: 20px; 6 padding: 20px; 7 border: 20px dashed #000; 8 background: url("img.jpg") no-repeat content-box border-box; 9 } 10 </style>
4.origin:border-box;clip:content-box;
1 <style> 2 #show-box { 3 width: 180px; 4 height: 180px; 5 margin: 20px; 6 padding: 20px; 7 border: 20px dashed #000; 8 background: url("img.jpg") no-repeat border-box content-box; 9 } 10 </style>
能夠看出,origin設置的是位置,clip則會根據區域裁剪出背景圖片。
默認值是scroll:背景圖片隨頁面的其他部分滾動。fixed:背景圖像是固定的。
導入圖片:background-image: url(image url);
多背景的寫法:使用逗號「,」隔開,繼續寫背景屬性。
background: color position size repeat origin clip attachment image, color position size repeat origin clip attachment image;
也能夠具體屬性單獨設置:background-image:url(image url 1),url(image url 2);
1 <style> 2 #show-box { 3 width: 180px; 4 height: 180px; 5 border: 20px dashed #000; 6 background: url("img.jpg1") left top/100% 100% no-repeat, 7 url("img.jpg2") left center/100% 100% no-repeat, 8 url("img.jpg3") left bottom/100% 100% no-repeat, 9 url("img.jpg4") center top/100% 100% no-repeat; 10 } 11 </style>
1 <style> 2 #show-box { 3 width: 180px; 4 height: 180px; 5 border: 20px dashed #000; 6 background-image: url("img.jpg1"), url("img.jpg2"), url("img.jpg3"), url("img.jpg4"); 7 background-position: left top, left center, left bottom, center top; 8 background-size: 100%; 9 background-repeat: no-repeat; 10 } 11 </style>