Style background 屬性
示例
設定文件的背景樣式
document.body.style.background = "#f3f3f3 url('img_tree.png') no-repeat right top";
自己動手試一試 »
更多“自己嘗試”的例子見下文。
描述
background 屬性以簡寫形式設定或返回最多八個獨立的背景屬性。
使用此屬性,您可以設定/返回以下一個或多個(按任意順序):
DOM 屬性 | CSS 屬性 |
---|---|
backgroundAttachment | background-attachment |
backgroundClip | background-clip |
backgroundColor | background-color |
backgroundImage | background-image |
backgroundOrigin | background-origin |
backgroundPosition | background-position |
backgroundRepeat | background-repeat |
backgroundSize | background-size |
上述屬性也可以透過單獨的樣式屬性設定。強烈建議非高階作者使用單獨的屬性,以獲得更好的可控性。
語法
返回 background 屬性
object.style.background
設定 background 屬性
object.style.background = "color image repeat attachment position size origin clip|initial|inherit"
屬性值
值 | 描述 |
---|---|
附件 | 設定背景影像是固定還是滾動 |
clip | 設定背景影像的繪製區域 |
color | 設定元素的背景顏色 |
圖片 | 設定元素的背景影像 |
origin | 設定背景定位區域 |
position | 設定背景影像的起始位置 |
重複 | 設定背景影像的重複方式 |
大小 | 設定背景影像的大小 |
initial | 將此屬性設定為其預設值。閱讀關於 initial |
inherit | 從其父元素繼承此屬性。閱讀關於 inherit |
技術詳情
預設值 | transparent none repeat scroll 0% 0% auto padding-box border-box |
---|---|
返回值 | 一個字串,表示元素的背景 |
CSS 版本 | CSS1 |
瀏覽器支援
background
是 CSS1 (1996) 的特性。
所有瀏覽器都完全支援。
Chrome | Edge | Firefox | Safari | Opera | IE |
是 | 是 | 是 | 是 | 是 | 是 |
注意
CSS3 (1999) 中新增了 3 個屬性
- background-clip
- background-origin
- background-size
更多示例
示例
更改 DIV 元素的背景
document.getElementById("myDIV").style.background = "url('smiley.gif') blue repeat-x center";
自己動手試一試 »