共用方式為


background

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

語法

{ background: sBackground }

可能的值

sBackground

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

attachment

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

color

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

image

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

位置

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>

標準資訊

此屬性是在全球資訊網協會 (W3C) 文件物件模組 (DOM) 層級 1 (英文) 與階層式樣式表 (CSS) 層級 1 (CSS1) (英文) 中定義。

適用於

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

色彩