日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 前端技术 > CSS >内容正文

CSS

CSS position(定位)属性

發(fā)布時間:2023/12/2 CSS 46 豆豆
生活随笔 收集整理的這篇文章主要介紹了 CSS position(定位)属性 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

關(guān)于CSS position,來自MDN的描述:

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

然后來看看什么是文檔流(normal flow),下面是?www.w3.org 的描述:

Normal flow

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的博客文章,原文點此):

  • normal flow直譯為常規(guī)流、正常流,國內(nèi)不知何原因大多譯為文檔流;
  • 窗體自上而下分成一行一行,并在每行中按從左至右的順序排放元素;
  • 每個非浮動塊級元素都獨占一行,?浮動元素則按規(guī)定浮在行的一端,若當(dāng)前行容不下,則另起新行再浮動;
  • 內(nèi)聯(lián)元素也不會獨占一行,幾乎所有元素(包括塊級,內(nèi)聯(lián)和列表元素)均可生成子行,用于擺放子元素;
  • 有三種情況將使得元素脫離normal flow而存在,分別是 float,absolute ,fixed,但是在IE6中浮動元素也存在于normal flow中。
  • 一、position:?static

    MDN的描述:

    該關(guān)鍵字指定元素使用正常的布局行為,即元素在文檔常規(guī)流中當(dāng)前的布局位置。此時 top、right、bottom、left 屬性無效。

    個人補充:static是position的默認值。

    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 設(shè)定 static 后,left就失效了,而元素(content)就以正常的 normal flow 形式呈現(xiàn)。

    二、position: relative

    MDN的描述:

    該關(guān)鍵字下,元素先放置在未添加定位時的位置,再在不改變頁面布局的前提下調(diào)整元素位置(因此會在此元素未添加定位時所在位置留下空白)。position:relative 對 table-*-group, table-row, table-column, table-cell, table-caption 元素?zé)o效。

    個人理解:相對于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

    這是沒有設(shè)置left、top等屬性時,正常出現(xiàn)在normal flow中的位置。

    接著添加left、top:

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

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

    三、position: absolute

    MDN的描述

    不為元素預(yù)留空間,通過指定元素相對于最近的非 static 定位祖先元素的偏移,來確定元素位置。絕對定位的元素可以設(shè)置外邊距(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 沒有設(shè)置 position,默認為 static,所以找到的第一個父元素是 body(<body></body>),可以看成是元素(content)相對于 body 向下移動10px。

    四、position: fixed

    MDN的描述

    不為元素預(yù)留空間,而是通過指定元素相對于屏幕視口(viewport)的位置來指定元素位置。元素的位置在屏幕滾動時不會改變。打印時,元素會出現(xiàn)在的每頁的固定位置。fixed屬性會創(chuàng)建新的層疊上下文。當(dāng)元素祖先的?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

    這里就不上圖了,看一下代碼或者自己動手碼一下就能理解。

    五、position: sticky

    MDN的描述

    盒位置根據(jù)正常流計算(這稱為正常流動中的位置),然后相對于該元素在流中的 flow root(BFC)和 containing block(最近的塊級祖先元素)定位。在所有情況下(即便被定位元素為 table

    時),該元素定位均不對后續(xù)元素造成影響。當(dāng)元素 B 被粘性定位時,后續(xù)元素的位置仍按照?B 未定位時的位置來確定。position: sticky對 table元素的效果與?position: relative 相同。

    因為各大瀏覽器對于sticky的兼容問題,而且JS也可以實現(xiàn)這個功能,在這里就不進行深入了,了解一下就好。

    六、position:?inherit

    w3school.com的 描述

    規(guī)定應(yīng)該從父元素繼承 position 屬性的值。

    inherit 繼承父元素,這個用得不多,所以也不繼續(xù)深入了。

    總結(jié)

    以上是生活随笔為你收集整理的CSS position(定位)属性的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。