background

設定或擷取物件的五個不同背景屬性。

語法

{ background: sBackground }

可能的值

sBackground

以任何順序指定或接收下列最多五個空格分隔值的字串:

attachment

background-attachment 屬性可用的任何附件值範圍。

color

background-color 屬性可用的任何色彩值範圍。

image

background-image 屬性可用的任何影像值範圍。

position

background-position 屬性可用的任何位置值範圍。

repeat

background-repeat 屬性可用的任何重複值範圍。

此屬性的預設值為 transparent none repeat scroll 0% 0% ,而且不是繼承屬性。

備註

background 屬性是將個別背景屬性結合成單一屬性的省略屬性。

background 省略屬性未設定的個別背景屬性將會設成其預設值。例如, image 的預設值為 none。設定 background: white 相當於設定 background: white none repeat scroll 0% 0% 。因此,除了將背景色彩設定為白色之外,設定 background: white 還會清除先前設定的任何 imagerepeatattachmentposition 值。

background 屬性會在物件的內容與邊框間距中呈現,不過框線則使用 border 屬性設定。

雖然物件不會繼承 background 屬性,但是如果未指定背景,則物件父項的背景影像或色彩會出現在物件後方。

範例

下列範例使用 background 屬性來設定背景值。此範例使用內嵌事件處理常式來修改影像的 background-colorbackground-position 屬性。這些屬性是使用 background 屬性在內嵌樣式表中指定:

<style>
.style1{ background: beige url(sphere.jpg) no-repeat top center; }
.style2{ background: ivory url(sphere.jpeg) no-repeat bottom right; }
</style>
</head> 
<body>
<span onmouseover="this.className='style1'"
    onmouseout="this.className='style2'">
. . .  
</span>

標準資訊

您可以在 World Wide Web Consortium (W3C) Document Object Model (DOM) Level 1 (全球資訊網協會 (W3C) 文件物件模組 (DOM) 層級 1) Ee341495.xtlink_newWindow(zh-tw,Expression.40).pngCascading Style Sheets (CSS), Level 1 (CSS1) (階層式樣式表 (CSS) 層級 1 (CSS1)) Ee341495.xtlink_newWindow(zh-tw,Expression.40).png 找到此屬性的定義。

適用範圍

a, address, b, big, blockquote, body, button, caption, center, cite, code, col, colgroup, custom, dd, defaults, dfn, dir, div, dl, dt, em, fieldset, form, hn, html, i, img, input type=button, input type=checkbox, input type=file, input type=image, input type=password, input type=radio, input type=reset, input type=submit, input type=text, isindex, kbd, label, legend, li, listing, menu, nobr, ol, p, plaintext, pre, s, samp, small, span, strike, strong, sub, sup, table, tbody, td, textarea, tfoot, th, thead, tr, tt, t, ul, var, xmp

另請參閱

概念

background-color
background-image
background-repeat
background-attachment
background-position
色彩