Css_Backgroud-position(背景圖片)定位問題詳解


background-position的說明:
    設置或檢索對象的背景圖像位置。必須先指定 background-image 屬性。該屬性定位不受對象的補丁屬性( padding )設置影響。
    默認值為: 0% 0% 。此時背景圖片將被定位於對象不包括補丁( padding )的內容區域的左上角。
    如果只指定了一個值,該值將用於橫坐標。縱坐標將默認為 50% 。如果指定了兩個值,第二個值將用於縱坐標。
    如果設置值為 right center,因為 right 作為橫坐標值將會覆蓋 center 值,所以背景圖片將被居右定位。
對應的腳本特性為 backgroundPosition 。

語法:
    background-position : length || length
    background-position : position || position

取值:
    length : 百分數 | 由浮點數字和單位標識符組成的長度值。
    position : top | center | bottom | left | center | right

background-position -- 定義背景圖片的位置

取值:
[ <percentage> | <length> | left | center | right ] [ <percentage> | <length> | top | center | bottom ] ] 
 * 水平
 left: 左
 center: 中
 right: 右
 * 垂直
 top: 上
 center: 中
 bottom: 下
* 垂直與水平的組合
 x-% y-%
 x-pos y-pos

 初始值: 0% 0%
 繼承性: 否
 適用於: 所有元素
 background:背景.position:位置.

重點講解的地方:

 Example Source Code [www.52css.com]
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml" lang="UTF-8">
    <head>
    <title>CSS背景圖片的定位background-position的問題 - 
PIGZZ.COM</title>
    <style type="text/css">
    <!--
    *{
    margin:0;
    padding:0;
    }
    body {
    text-align:center;
    background:#000;
    }
    #container{
    width:1000px;
    margin:0 auto;
    background:#fff url(images/1.gif) no-repeat left top;
    height:500px;
    }
    -->
    </style>
    </head>
    <body>
    <div id="container"> </div>
    </body>
    </html>
    

 

一.background-position:left top;(見圖1).
背景圖片的左上角和容器(container)的左上角對齊,超出的部分隱藏。
等同於 background-position:0,0;
也等同於background-position:0%,0%;

二.background-position:right bottom;(見圖2)。
背景圖片的右下角和容器(container)的右下角對齊,超出的部分隱藏。
等同於background-positon:100%,100%;
也等同於background-positon:容器(container)的寬度-背景圖片的寬度,容器(container)的高度-背景圖片的高度

三.background-position:500px 15px;(見圖3)。
背景圖片從容器(container)左上角的地方向右移500px,向下移15px,超出的部分隱藏。

四.background-position:-500px -15px;(見圖4)。
背景圖片從容器(container)左上角的地方向左移500px,向上移15px,超出的部分隱藏。

五.background-position:50% 50%;(見圖5)。
等同於left:{容器(container)的寬度—背景圖片的寬度}*left百分比,超出的部分隱藏。
等同於right:{容器(container)的高度—背景圖片的高度}*right百分比,超出的部分隱藏。
例如:background-position:50% 50%;就是background-position:(1000-2000)*50%px,(500-30)*50%px;即background-position:-500px,235px;也就是背景圖片從容器(container)的左上角向左移500px,向下移235px;

六.background-position:-50% -50%;(見圖6)。
(這種情況背景圖片應該用bg2.jpg才能看出效果,bg.jpg的高度太小效果不明顯)
等同於left:-{{容器(container)的寬度—背景圖片的寬度}*left百分比(百分比都取正值)},超出的部分隱藏。
等同於right:-{{容器(container)的高度—背景圖片的高度}*right百分比(百分比都取正值)},超出的部分隱藏。
例如:background-position:-50% -50%;就是background-position:-{(1000-500)*50%}px,-{(500-360)*50%}px;即background- position:-250px,-70px;也就是背景圖片從容器(container)的左上角向左移250px,向上移70px;


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM