CSS position(定位)屬性

關於CSS position,來自MDN的描述:css

CSS position屬性用於指定一個元素在文檔中的定位方式。top、right、bottom、left 屬性則決定了該元素的最終位置。html

而後來看看什麼是文檔流(normal flow),下面是 www.w3.org 的描述:瀏覽器

Normal flowide

Boxes in the normal flow belong to a formatting context, which may be block or inline, but not both simultaneously. Block-level boxes participate in a block formatting context. Inline-level boxes participate in an inline formatting context.佈局

我的補充(此處參考FungLeo的博客文章,原文點此):ui

  1. normal flow直譯爲常規流、正常流,國內不知何緣由大多譯爲文檔流;
  2. 窗體自上而下分紅一行一行,並在每行中按從左至右的順序排放元素;
  3. 每一個非浮動塊級元素都獨佔一行, 浮動元素則按規定浮在行的一端,若當前行容不下,則另起新行再浮動;
  4. 內聯元素也不會獨佔一行,幾乎全部元素(包括塊級,內聯和列表元素)都可生成子行,用於擺放子元素;
  5. 有三種狀況將使得元素脫離normal flow而存在,分別是 float,absolute ,fixed,可是在IE6中浮動元素也存在於normal flow中。

1、position: static

MDN的描述:spa

該關鍵字指定元素使用正常的佈局行爲,即元素在文檔常規流中當前的佈局位置。此時 top、right、bottom、left 屬性無效。.net

我的補充:static是position的默認值。ssr

 1 <!DOCTYPE html>
 2 <html lang="en">
 3 <head>
 4     <meta charset="UTF-8">
 5     <title>CSS-position-static</title>
 6     <link rel="stylesheet" href="https://cdn.bootcss.com/normalize/8.0.0/normalize.css">
 7     <style>
 8         .container{
 9             background-color: #868686;
10             width: 100%;
11             height: 300px;
12         }
13         .content{
14             background-color: yellow;
15             width: 100px;
16             height: 100px;
17             position: static;
18             left: 10px;/* 這個left沒有起做用 */
19         }
20     </style>
21 </head>
22 <body>
23     <div class="container">
24         <div class="content">    
25         </div>
26     </div>
27 </body>
28 </html>

對 content 的 position 設定 static 後,left就失效了,而元素(content)就以正常的 normal flow 形式呈現。code

2、position: relative

MDN的描述:

該關鍵字下,元素先放置在未添加定位時的位置,再在不改變頁面佈局的前提下調整元素位置(所以會在此元素未添加定位時所在位置留下空白)。position:relative 對 table-*-group, table-row, table-column, table-cell, table-caption 元素無效。

我的理解:相對於normal flow中的原位置來定位。

 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>CSS-position-relative</title>  
 8     <link rel="stylesheet" href="https://cdn.bootcss.com/normalize/8.0.0/normalize.css">
 9     <style>
10             .container{
11                 background-color: #868686;
12                 width: 100%;
13                 height: 300px;
14             }
15             .content_0{
16                 background-color: yellow;
17                 width: 100px;
18                 height: 100px;               
19             }
20             .content_1{
21                 background-color: red;
22                 width: 100px;
23                 height: 100px;
24                 position: relative;/* 這裏使用了relative  */            
25             }
26             .content_2{
27                 background-color: black;
28                 width: 100px;
29                 height: 100px;               
30             }
31         </style>
32     </head>
33     <body>
34         <div class="container">
35             <div class="content_0">    
36             </div>
37             <div class="content_1">    
38             </div>
39             <div class="content_2">    
40             </div>
41         </div>   
42 </body>
43 </html>
position: relative

這是沒有設置left、top等屬性時,正常出如今normal flow中的位置。

接着添加left、top:

1 .content_1{
2                 background-color: red;
3                 width: 100px;
4                 height: 100px;
5                 position: relative;/* 這裏使用了relative  */
6                 left: 20px;/* 這裏設置了left和top */
7                 top: 20px;            
8             }

能夠看到,元素(content_1)的位置相對於其原位置(normal flow中的正常位置)進行了移動。

3、position: absolute

MDN的描述

不爲元素預留空間,經過指定元素相對於最近的非 static 定位祖先元素的偏移,來肯定元素位置。絕對定位的元素能夠設置外邊距(margin),且不會與其餘邊距合併。

我的理解:生成絕對定位的元素,其相對於 static 定位之外的第一個父元素進行定位,會脫離normal flow。注意:是除了static外

 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>CSS-position-static</title>
 8     <link rel="stylesheet" href="https://cdn.bootcss.com/normalize/8.0.0/normalize.css">
 9     <style>
10         .container{
11             background-color: #868686;
12             width: 100%;
13             height: 300px;
14             margin-top: 50px;
15         }
16         .content{
17             background-color: red;
18             width: 100px;
19             height: 100px;
20             position: absolute;
21             top: 10px;
22         }
23     </style>
24 </head>
25 <body>
26     <div class="container">
27         <div class="content">    
28         </div>
29     </div>
30 </body>
31 </html>
position: absolute

由於 content 的父元素 container 沒有設置 position,默認爲 static,因此找到的第一個父元素是 body(<body></body>),能夠當作是元素(content)相對於 body 向下移動10px。

4、position: fixed

MDN的描述

不爲元素預留空間,而是經過指定元素相對於屏幕視口(viewport)的位置來指定元素位置。元素的位置在屏幕滾動時不會改變。打印時,元素會出如今的每頁的固定位置。fixed屬性會建立新的層疊上下文。當元素祖先的 transform 屬性非 none 時,容器由視口改成該祖先。

我的理解:fixed相對於window固定,滾動瀏覽器窗口並不會使其移動,會脫離normal flow。

 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>CSS-position-static</title>
 8     <link rel="stylesheet" href="https://cdn.bootcss.com/normalize/8.0.0/normalize.css">
 9     <style>
10         .container{
11             background-color: #868686;
12             width: 100%;
13             height: 1000px;
14         }
15         .content{
16             background-color: yellow;
17             width: 100px;
18             height: 100px;
19             position: fixed;/* 這裏使用了fixed */
20         }
21     </style>
22 </head>
23 <body>
24     <div class="container">
25         <div class="content">    
26         </div>
27     </div>
28 </body>
29 </html>
position: fixed

這裏就不上圖了,看一下代碼或者本身動手碼一下就能理解。

5、position: sticky

MDN的描述

盒位置根據正常流計算(這稱爲正常流動中的位置),而後相對於該元素在流中的 flow root(BFC)和 containing block(最近的塊級祖先元素)定位。在全部狀況下(即使被定位元素爲 table),該元素定位均不對後續元素形成影響。當元素 B 被粘性定位時,後續元素的位置仍按照 B 未定位時的位置來肯定。position: sticky對 table元素的效果與 position: relative 相同。

由於各大瀏覽器對於sticky的兼容問題,並且JS也能夠實現這個功能,在這裏就不進行深刻了,瞭解一下就好。

6、position: inherit

w3school.com的 描述

規定應該從父元素繼承 position 屬性的值。

inherit 繼承父元素,這個用得很少,因此也不繼續深刻了。

相關文章
相關標籤/搜索