天天看點

【轉】html背景設定

背景顔色屬性(background-color)

這個屬性為HTML元素設定背景顔色,相當于HTML中bgcolor屬性。

body {background-color:#99FF00;}

上面的代碼表示Body這個HTML元素的背景顔色是翠綠色的。

示範示例

背景圖檔屬性(background-image)

這個屬性為HTML元素設定背景圖檔,相當于HTML中background屬性。

<body style="background-image:url(../images/css_tutorials/background.jpg)">

上面的代碼為Body這個HTML元素設定了一個背景圖檔。

背景重複屬性(background-repeat)

這個屬性和background-image屬性連在一起使用,決定背景圖檔是否重複。如果隻設定background-image屬性,沒設定background-repeat屬性,在預設狀态下,圖檔既橫向重複,又豎向重複。

repeat :  預設值。背景圖像在縱向和橫向上平鋪

no-repeat :  背景圖像不平鋪

repeat-x :  背景圖像僅在橫向上平鋪

repeat-y :  背景圖像僅在縱向上平鋪

body {background-image:url(../images/css_tutorials/background.jpg); background-repeat:repeat-y}

上面的代碼表示圖檔豎向重複。

背景附着屬性(background-attachment)

這個屬性和background-image屬性連在一起使用,決定圖檔是跟随内容滾動,還是固定不動。這個屬性有兩個值,一個是scroll,一個是fixed。預設值是scroll。

body {background-image:url(../images/css_tutorials/background.jpg); background-repeat:no-repeat; background-attachment:fixed}

上面的代碼表示圖檔固定不動,不随内容滾動而動。

背景位置屬性(background-position)

這個屬性和background-image屬性連在一起使用,決定了背景圖檔的最初位置。

body {background-image:url(../images/css_tutorials/background.jpg);background-repeat:no-repeat; background-position:20px 60px}

上面的代碼表示背景圖檔的初始位置距離網頁最左面20px,距離網頁最上面60px。

背景屬性(background)

這個屬性是設定背景相關屬性的一種快捷的綜合寫法, 包括background-color, background-image, background-repeat, backgroundattachment, background-position。

body {background:#99FF00 url(../images/css_tutorials/background.jpg) no-repeat fixed 40px 100px}

上面的代碼表示,網頁的背景顔色是翠綠色,背景圖檔是background.jpg圖檔,背景圖檔不重複顯示,背景圖檔不随内容滾動而動,背景圖檔距離網頁最左面40px,距離網頁最上面100px。