css 中相對定位和絕對位置

來源:互聯網
上載者:User

標籤:ima   ice   set   設定   title   class   har   div   .class   

1. css中定位機制有三種: 標準文檔流, 浮動, 絕對位置

2. 絕對位置就屬於第三種定位, 用到position屬性, 下面就是具體設定

相對定位:

  相對於自身原有位置(就是普通流的時候)進行位移(設定top, left...後)

  仍然處於標準文檔流中

  隨即擁有位移屬性和z-index屬性

絕對位置: 

  建立了以包含塊為基準的定位

  完全脫離了標準文檔流

  隨即擁有位移屬性和z-index屬性

 

3.  先上範例程式碼

 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4     <meta charset="UTF-8"> 5     <meta name="viewport" content="width=device-width, initial-scale=1.0"> 6     <meta http-equiv="X-UA-Compatible" content="ie=edge"> 7     <title>Document</title> 8     <style type="text/css"> 9         .class1 {10             height: 100px;11             background-color: blue12         }13         .class2 {14             position:relative; /*去掉後子項目會以html為參照物*/15             height: 50px;16             background-color: green;17         }18         .class3 {19             height: 80px;20             background-color: red;21         }22         .child {23             height: 20;24             position:absolute;25             top: 10px;26             left: 30px;27             background-color:gold;28         }29     </style>30 </head>31 <body>32     <div class="class1"></div>33     <div class="class2">34         <div class="child">35             Jason Zeng36         </div>37     </div>38     <div class="class3"></div>39 </body>40 </html>

 

祖先元素設定了定位

1 .class2 {2     position:relative; /*去掉後子項目會以html為參照物*/3     height: 50px;4     background-color: green;5 }

祖先元素沒有設定定位

1 .class2 {2     height: 50px;3     background-color: green;4 }

4.總結: 絕對位置時位移參考物:

  無已定位的祖先元素(包裹它的所有層都沒有設定position屬性): 以<html>為位移參照物

  有已定位的祖先元素: 以距其最近的已定位的祖先元素為位移參照基準

 

css 中相對定位和絕對位置

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.