CSS背景background詳解,background-position詳解

背景(background)是css中一個重要的的部分,也是須要知道的css的基礎知識之一。這篇文章將會涉及css背景(background)的基本用法,包括諸如 background-attachment 等的屬性,也會介紹一些有關背景(background)的經常使用技巧,以及 css3 中的 背景(background)(包含4個新的背景(background)屬性)。css

css2 中的背景(background)html

概述css3

CSS2 中有5個主要的背景(background)屬性,它們是:瀏覽器

* background-color: 指定填充背景的顏色。工具

* background-image: 引用圖片做爲背景。url

* background-position: 指定元素背景圖片的位置。spa

* background-repeat: 決定是否重複背景圖片。.net

* background-attachment: 決定背景圖是否隨頁面滾動。翻譯

這些屬性能夠所有合併爲一個縮寫屬性: background。須要注意的一個要點是背景佔據元素的全部內容區域,包括 padding 和 border,可是不包括元素的 margin。它在 Firefox, Safari ,Opera 以及 IE8 中工做正常,可是 IE6 和 IE7 中,background 沒把 border 計算在內。3d

 

基本屬性

 

背景色(background-color)

background-color 屬性用純色來填充背景。有許多方式指定這個顏色,如下方式都獲得相同的結果。

background-color: blue;
background-color: rgb(0, 0, 255);
background-color: #0000ff;

background-color 也可被設置爲透明(transparent),這會使得其下的元素可見。

 

背景圖(background-image)

background-image 屬性容許指定一個圖片展現在背景中。能夠和 background-color 連用,所以若是圖片不重複地話,圖片覆蓋不到地地方都會被背景色填充。代碼很簡單,只須要記住,路徑是相對於樣式表的,所以如下的代碼中,圖片和樣式表是在同一個目錄中的。

background-image: url(image.jpg);

可是若是圖片在一個名爲 images 的子目錄中,就應該是:

background-image: url(images/image.jpg);

糖伴西紅柿:使用 ../ 表示上一級目錄,好比 background-image: url(../images/image.jpg); 表示圖片位於樣式表的上級目錄中的 images 子目錄中。有點繞,不過這個你們應該都知道了,我就不詳說了。

 

背景平鋪(background-repeat)

設置背景圖片時,默認把圖片在水平和垂直方向平鋪以鋪滿整個元素。這也許是你須要的,可是有時會但願圖片只出現一次,或者只在一個方向平鋪。如下爲可能的設置值和結果:

background-repeat: repeat; /* 默認值,在水平和垂直方向平鋪 */
background-repeat: no-repeat; /* 不平鋪。圖片只展現一次。 */
background-repeat: repeat-x; /* 水平方向平鋪(沿 x 軸) */
background-repeat: repeat-y; /* 垂直方向平鋪(沿 y 軸) */
background-repeat: inherit; /* 繼承父元素的 background-repeat 屬性*/

 

【重點】背景定位(background-position)

background-position 屬性用來控制背景圖片在元素中的位置。技巧是,實際上指定的是圖片左上角相對於元素左上角的位置。
下面的例子中,設置了一個背景圖片而且用 background-position 屬性來控制它的位置,同時也設置了 background-repeat 爲 no-repeat。計量單位是像素。第一個數字表示 x 軸(水平)位置,第二個是 y 軸(垂直) 位置。

/* 例 1: 默認值 */
background-position: 0 0; /* 元素的左上角 */

/* 例 2: 把圖片向右移動 */
background-position: 75px 0;

/* 例 3: 把圖片向左移動 */
background-position: -75px 0;

/* 例 4: 把圖片向下移動 */
background-position: 0 100px;

 

background-position 屬性能夠用其它數值,關鍵詞和百分比來指定,這比較有用,尤爲是在元素尺寸不是用像素設置時。

關鍵詞是不用解釋的。x 軸上:

  • * left
  • * center
  • * right

y 軸上:

  • * top
  • * center
  • * bottom

順序方面和使用像素值時的順序幾乎同樣,首先是 x 軸,其次是 y 軸,像這樣:

background-position: right top;

使用百分數時也相似。須要主要的是,使用百分數時,瀏覽器是以元素的百分比數值來設置圖片的位置的。看例子就好理解了。假設設定以下:

background-position: 100% 50%;

This goes 100% of the way across the image (i.e. the very right-hand edge) and 100% of the way across the element (remember, the starting point is always the top-left corner), and the two line up there. It then goes 50% of the way down the image and 50% of the way down the element to line up there. The result is that the image is aligned to the right of the element and exactly half-way down it.

糖伴西紅柿:這一段沒想到合適的翻譯,保留原文,意譯。 

update: 感謝天涯的指教,這段搞明白了。使用百分數定位時,實際上是將背景圖片的百分比指定的位置和元素的百分比位置對齊。也就是說,百分數定位是改變了背景圖和元素的對齊基點。再也不像使用像素和關鍵詞定位時,使用背景圖和元素的左上角爲對齊基點。例如上例的 background-position: 100% 50%; 就是將背景圖片的 100%(right) 50%(center) 這個點,和元素的 100%(right) 50%(center) 這個點對齊。

這再一次說明了,咱們一直認爲已經掌握的簡單的東西,其實還有咱們有限的認知以外的知識。

注意原點老是左上角,最終的效果是笑臉圖片被定位在元素的最右邊,離元素頂部是元素的一半,效果和 background-position: right center; 同樣。

 

背景附着

background-attachment 屬性決定用戶滾動頁面時圖片的狀態。三個可用屬性爲 scroll(滾動),fixed(固定) 和 inherit(繼承)。inherit 單純地指定元素繼承他的父元素的 background-attachment 屬性。

爲了正確地理解 background-attachment,首先須要明白頁面(page)和視口(view port)是如何協做地。視口(view port)是瀏覽器顯示網頁的部分(就是去掉工具欄的瀏覽器)。視口(view port)的位置固定,不變更。

當向下滾動網頁時,視口(view port)是不動的,而頁面的內容向上滾動。看起來貌似視口(view port)向頁面下方滾動了。若是設置 background-attachment: scroll,就設置了當元素滾動時,元素背景也必需隨着滾動。簡而言之,背景是緊貼元素的。這是 background-attachment 默認值。

用一個例子來更清楚地描述下:

background-image: url(test-image.jpg);

background-position: 0 0;
background-repeat: no-repeat;
background-attachment: scroll;

當向下滾動頁面時,背景向上滾動直至消失。

可是當設置 background-attachment 爲 fixed 時,當頁面向下滾動時,背景要待在它原來的位置(相對於瀏覽器來講)。也就是不隨元素滾動。

用另外一個例子描述下:

background-image: url(test-image.jpg);
background-position: 0 100%;
background-repeat: no-repeat;
background-attachment: fixed;

頁面已經向下滾動了,可是圖像仍然保持可見。

須要重視的一點是背景圖只能出如今它父元素能達到的區域。即便圖片是相對於視口(view port)定位地,若是它的父元素不可見,圖片就會消失。參見下面的例子。此例中,圖片位於視口(view port)的左下方,可是隻有元素內的圖片部分是可見的。

background-image: url(test-image.jpg);
background-position: 0 100%;
background-repeat: no-repeat;
background-attachment: fixed;

由於圖片開始在元素以外,一部分圖片被切除了。

 

 

英文原文:Backgrounds In CSS: Everything You Need To Know

漢化原文:CSS背景全攻略

相關文章
相關標籤/搜索