background 簡寫屬性在一個聲明中設定所有的背景屬性。
基本介紹
- 中文名:背景屬性
- 外文名:background
- 屬性定義元素:
概念,詳解實例,
概念
背景 (background) 屬性定義元素的
元素的背景區包括前景之下直到框線邊界的所有空間。因此,內容框與內都是元素背景的一部分,且框線畫在背景上。
css 允許套用純色作為背景,也允許使用創建相當複雜的效果;css 在這方面的能力遠遠在 html 之上。
background 是用於在一個聲明中設定所有背景屬性的一個簡寫屬性。
格式: background: {屬性值}
繼承性: no
可能的值:
background-color
background-image
background-repeat
background-attachment
background-position
新增的值:
background-clip
background-origin
background-size
註:可以在此聲明中聲明1到5個背景屬性
這只是 css 1.0 的屬性 在css 2.0 background已經退出了歷史舞台;
例:
body
{
background: #ff0000 no-repeat fixed center;
}
詳解實例
background-color:{顏色值|transparent(默認值)} 屬性設定元素的背景顏色。
color 顏色值可以是顏色名稱、rgb 值或者。
transparent 默認。背景顏色為透明。
例:
body
{
background-color: #00ff00
}
1.background-image:{url(url)} 把圖像設定為背景。
指向圖像的路徑。
none 默認。無。
例:
body
{
background-image: ;
}
2.background-repeat:{repeat|no-repeat|repeat-x|repeat-y} 設定背景圖像是否及如何重複。
repeat 默認。背景圖像將在垂直方向和水平方向重複。
repeat-x 背景圖像將在水平方向重複。
repeat-y 背景圖像將在垂直方向重複。
no-repeat 將僅顯示一次。
例:
body
{
background-image: ;
background-repeat: no-repeat;
}
3.background-attachment:{fixed|scroll} 背景圖像是否固定或者隨著頁面的其餘部分滾動
scroll 默認。背景圖像會隨著頁面其餘部分的滾動而移動。
fixed 當頁面的其餘部分滾動時,不會移動。
例:
body
{
background-attachment: fixed;
background-image: ;
}
4.background-position:{位置值} 屬性設定背景圖像的起始位置。
top left
top center
top right
center left
center center
center right
bottom left
bottom center
bottom right
x% y%
xpos ypos
使用百分數定位時,其實是將背景圖片的百分比指定的位置和元素的百分比位置對齊。也就是說,百分數定位是改變了背景圖和元素的對齊基點。不再像使用像素和關鍵字定位時,使用背景圖和元素的左上角為對齊基點。例如上例的background-position: 100% 50%; 就是將背景圖片的100%(right) 50%(center)這個點,和元素的100%(right) 50%(center)這個點對齊。
使用數值需要注意的是,當只有一個數值時,這個值將用於橫坐標,縱坐標將默認是50%。
例:
body
{
background-image: ;
background-position: top;
}
用javascript改變背景圖片
例:
function hi()
{
document.body.style.background="no-repeat url(/uploads/image/images/asp_logo1.gif) 50% 50%";
}