left和offsetLeft

來源:互聯網
上載者:User

標籤:

left:

1.當該對象的定位position為absolute時left是相對於擁有定位屬性(position的值為預設值"static"時除外)的父級對象的左邊距。

例1:當父級a的position為relative、absolute或者fixed時,b都根據a進行定位。

 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1"> 6 <title></title> 7 <style> 8     .a{width: 200px;height: 200px;border: 1px solid red;margin-top: 100px;margin-left:100px;position:relative;} 9     .b{width:100px;height: 100px;position:absolute;background-color:pink;top:0;left:0;}10 </style>11 </head>12 <body>13     <div class="a">14         <div class="b"></div>15     </div>16 </body>17 </html>

例2:當a沒有設定位(position)或者position為static時,b根據瀏覽器的左上方定位。

.a{width: 200px;height: 200px;border: 1px solid red;margin-top: 100px;margin-left:100px;position:static;}
.b{width:100px;height: 100px;position:absolute;background-color:pink;top:0;left:0;}

2.當該對象的position為relative時,根據自己最初的位置進行定位,不受父級的定位影響。

例:

.a{width: 200px;height: 200px;border: 1px solid red;margin-top: 100px;margin-left:100px;position:absolute;}.b{width:100px;height:100px;position:relative;background-color:pink;top:50px;left:50px;}

3.當該對象的position為fixed時,根據視窗的視圖進行定位,不受父級的定位影響。

例:

.a{width: 200px;height: 200px;border: 1px solid red;margin-top: 100px;margin-left:100px;position:absolute;}.b{width:100px;height:100px;position:fixed;background-color:pink;top:50px;left:50px;}

offsetLeft :

相對於父級的左邊距。

  

        

 

left和offsetLeft

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.