background-position 用來設定背景圖像的起始位置。它決定由 background-image 指定的原圖要放在哪裡;若背景需要重複,也從這個點開始鋪排。
小提示:如果需要讓此屬性在 Firefox 與 Opera 中正常運作,建議把 background-attachment 設為 "fixed"。
object.style.backgroundPosition="center"body
{
background-image:url('https:///keleyi/phtml/duanwu/duanwu/img/bg.png');
background-repeat:no-repeat;
background-attachment:fixed;
background-position:center;
}
所有瀏覽器都支援 background-position。需要注意的是,任何版本的 Internet Explorer(包含 IE8)都不支援屬性值 "inherit"。
top left top center top right
center left center center center right
bottom left bottom center bottom right
如果只設定一個關鍵字,第二個值會是 "center"。預設值為 0% 0%。
第一個值是水平位置,第二個值是垂直位置。左上角是 0% 0%,右下角是 100% 100%。若只給一個值,另一個值為 50%。
同樣是「水平、垂直」的順序,左上角為 0 0,單位可以是像素(0px 0px)或任何合法 CSS 長度單位。若只給一個值,另一個值為 50%。
此外,% 與 position 關鍵字可以混用,例如 background-position: right 20px; 這種寫法在需要「貼右但留出內距」時很好用。