CSS background 屬性
定義和用法
background
簡寫屬性在一個聲明中設置所有的背景屬性。
可以設置如下屬性:
- background-color
- background-position
- background-size
- background-repeat
- background-origin
- background-clip
- background-attachment
- background-image
如果不設置其中的某個值,也不會出問題,比如 background:#ff0000 url('smiley.gif'); 也是允許的。
通常建議使用這個屬性,而不是分別使用單個屬性,因為這個屬性在較老的瀏覽器中能夠得到更好的支持,而且需要鍵入的字母也更少。
另請參閱:
CSS 教程:CSS 背景、CSS 背景(高級)
HTML DOM 參考手冊:background 屬性
CSS 語法
background: bg-color bg-image position/bg-size bg-repeat bg-origin bg-clip bg-attachment initial|inherit;
屬性值
值 | 描述 | CSS |
---|---|---|
background-color | 規定要使用的背景顏色。 | 1 |
background-position | 規定背景圖像的位置。 | 1 |
background-size | 規定背景圖片的尺寸。 | 3 |
background-repeat | 規定如何重復背景圖像。 | 1 |
background-origin | 規定背景圖片的定位區域。 | 3 |
background-clip | 規定背景的繪制區域。 | 3 |
background-attachment | 規定背景圖像是否固定或者隨著頁面的其余部分滾動。 | 1 |
background-image | 規定要使用的背景圖像。 | 1 |
inherit | 規定應該從父元素繼承 background 屬性的設置。 | 1 |
技術細節
默認值: | not specified |
---|---|
繼承性: | no |
版本: | CSS1 + CSS3 |
JavaScript 語法: | object.style.background="white url(paper.gif) repeat-y" |
瀏覽器支持
表格中的數字注明了完全支持該屬性的首個瀏覽器版本。
Chrome | Edge | Firefox | Safari | Opera |
---|---|---|---|---|
Chrome | Edge | Firefox | Safari | Opera |
1.0 | 4.0 | 1.0 | 1.0 | 3.5 |