CSS的三種定位方式介紹(轉載)

來源:互聯網
上載者:User

標籤:

在CSS中一共有N種定位方式,其中,static ,relative,absolute三種方式是最基本最常用的三種定位方式。他們的基

本介紹如下。

static預設定位方式
relative相對定位,相對於原來的位置,但是原來的位置仍然保留
absolute定位,相對於最近的非標準劉定位,原來的位置消失,被後邊的位置所頂替

 

下面先示範相對定位的案例

[html] view plain copyprint?<!DOCTYPE html><html><head><title>relative.html</title><meta http-equiv="keywords" content="keyword1,keyword2,keyword3"><meta http-equiv="description" content="this is my page"><meta http-equiv="content-type" content="text/html; charset=UTF-8"><link rel="stylesheet" href="../css/relative.css" type="text/css"></link></head><body><div class="style1">內容一</div><div id="special" class="style1">內容二</div><div class="style1">內容三</div><div class="style1">內容四</div></body></html>

 CSS代碼如下

[css] view plain copy print?.style1{      width: 300px;      height: 100px;      background-color: gray;      border: 1px solid red;      float: left;      margin-left: 10px;  }    #special{      position: relative;/*這裡使用了相對定位,left意思是在原來的位置向左移動多少*/      left: 40px;/*左側座標變大,向右移動*/      top: 200px;  }  

 

其中的left是值擴大left的長度,也就是向右移動,當然了,是相對於這個模組的原來的位置。他的後面的元素不會頂

替他的位置,

然後是絕對位置。其中,HTML代碼不變,只改變了CSS代碼

[css] view plain copy print?.style1{      width: 300px;      height: 100px;      background-color: gray;      border: 1px solid red;      float: left;      margin-left: 10px;  }    #special{      position: absolute;/*這裡使用了相對定位,left意思是在原來的位置向左移動多少*/      left: 40px;/*左側座標變大,向右移動*/      top: 200px;  }  

絕對位置這裡就是相對於body這個元素的絕對位置,當然了,當他的最近處有一個非標準流的東西,他就會跟那個非標準流為標準進行配對。

如下

本文採摘自網路本人迷迷糊糊的懂點,有何不正確的,還望大神指點!

 

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.