CSS-Background詳解

background主要包含5個屬性:

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

background-image: 引用圖片作爲背景。

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

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

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

基本屬性

背景色(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-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 屬性用來控制背景圖片在元素中的位置。技巧是,實際上指定的是圖片左上角相對於元素左上角的位置。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章