【圖解CSS#Position】

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

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

先看一個圖片:


child沒設(shè)置position的樣式

代碼如下:

<body>
  <div class="parent1">
    <div class="child1-1">child1-1</div>
    <div class="child1-2">child1-2</div>
    <div class="child1-3">child1-3</div>
    <div class="child1-4">child1-4</div>
  </div>
</body>
<style type="text/css">
    .parent1{
      background-color: chocolate;
      width: 300px;
      height: 300px;
      margin: 25px 50px 75px;
    }
    .child1-1{
      background-color: cornflowerblue;
      width: 60px;
      height: 60px;
    }
    .child1-2{
      background-color:crimson;
      width: 60px;
      height: 60px;
    }
    .child1-3{
      background-color:darkcyan;
      width: 60px;
      height: 60px;
    }
    .child1-4{
      background-color: darkgreen;
      width: 60px;
      height: 60px;
    }
</style>

這里展示的就是一個正?!拔臋n流”,

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.

個人理解:顧名思義就是像寫文檔一樣,一行一行的排列,如圖,每個child 都是一個60*60 的小方塊,即使在他們右邊有多余的位置,但是它們還是占一行。position就是每個元素的定位屬性,而默認就是 position = static,即上圖中parent 和 child 都是展示的文檔流樣式。

而position屬性一共有以下取值:

  1. static
    默認值。沒有定位,元素出現(xiàn)在正常的流中(忽略 top, bottom, left, right 或者 z-index 聲明)

  2. relative
    生成相對定位的元素,相對于其正常位置進行定位。 如我們將上面的代碼改成

    // ...
    .child1-2{
        background-color:crimson;
        width: 60px;
        height: 60px;
        position: relative;
        left: 20px;
        top: 30px;
    }
    
    relative

    可以看到 child1-2 在原來的位置上,左邊多出了 20px ,距離頂部多出了 30px

  3. absolute
    生成絕對定位的元素,相對于 static 定位以外的第一個父元素進行定位。
    我們將上例的child1-2恢復(fù),然后操作child1-3

    .child1-3{
          background-color:darkcyan;
          width: 60px;
          height: 60px;
          position: absolute;
          left: 20px;
          top: 30px;
    }
    
    absolute

    可以看到,child1-3 的位置已經(jīng)和 parent 無關(guān)了(因為 parent 的 position = "static"),很多時候,都會將 parent 設(shè)置成relative 來和 absolute child 配合使用。
    我們再改一下 child1-3 的代碼

    .child1-3{
        background-color:darkcyan;
        width: 60px;
        height: 60px;
        position: absolute;
        left: 20px;
        bottom: 30px;   // 注意這個
      }
    
    bottom 30px

    bottom 30px 增加瀏覽器的高度

    可以看到,child1-3 其實是一直跟著瀏覽器的高度在變化的。

  4. fixed

    .child1-4{
          background-color: darkgreen;
          width: 60px;
          height: 60px;
          position: fixed;
          bottom: 30px;
          left: 70px;
    }
    
    fixed

    而且,當瀏覽器高度變化時,他們兩個也會跟著變化。
    但是,兩者的區(qū)別就在于,當 parent 設(shè)置成 position = relative 時,

    .parent1{
          background-color: chocolate;
          width: 300px;
          height: 300px;
          margin: 25px 50px 75px;
          position: relative;
    }
    
    parent position = relative

    即此時,child1-3 的 left,right 等以 parent 為基準。

  5. inherit
    規(guī)定應(yīng)該從父元素繼承 position 屬性的值。
    這個就不需要圖解了。。。。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

  • 第一部分 HTML&CSS整理答案 1. 什么是HTML5? 答:HTML5是最新的HTML標準。 注意:講述HT...
    kismetajun閱讀 28,797評論 1 45
  • 1 css3知識解讀 css3解讀鏈接:css3知識解讀 自定義字體利用@font-face{}引入自定義字體;創(chuàng)...
    果木山閱讀 403評論 0 1
  • 本文主要講述頁面布局樣式方面涉及的知識點,更全面的對CSS相應(yīng)的技術(shù)進行歸類、整理、說明,沒有特別詳細的技術(shù)要點說...
    Joel_zh閱讀 1,157評論 0 1
  • 各種純css圖標 CSS3可以實現(xiàn)很多漂亮的圖形,我收集了32種圖形,在下面列出。直接用CSS3畫出這些圖形,要比...
    劍殘閱讀 9,977評論 0 8
  • 推薦指數(shù): 6.0 書籍主旨關(guān)鍵詞:特權(quán)、焦點、注意力、語言聯(lián)想、情景聯(lián)想 觀點: 1.統(tǒng)計學現(xiàn)在叫數(shù)據(jù)分析,社會...
    Jenaral閱讀 5,965評論 0 5

友情鏈接更多精彩內(nèi)容