[轉載]CSS教程:實例講解定位Position

http://www.missyuan.com/thread-395406-1-1.html

 

1. position:static

全部元素的默認定位都是:position:static,這意味着元素沒有被定位,並且在文檔中出如今它應該在的位置。css

通常來講,不用指定 position:static,除非想要覆蓋以前設置的定位。html

#div-1 {
 position:static;
}

static

2. position:relative

若是設定 position:relative,就可使用 top,bottom,left 和 right 來相對於元素在文檔中應該出現的位置來移動這個元素。【意思是元素實際上依然佔據文檔中的原有位置,只是視覺上相對於它在文檔中的原有位置移動了】工具

#div-1 {
 position:relative;
 top:20px;
 left:-40px;
}

relative

3. position:absolute

當指定 position:absolute 時,元素就脫離了文檔【即在文檔中已經不佔據位置了】,能夠準確的按照設置的 top,bottom,left 和 right 來定位了。佈局

#div-1a {
 position:absolute;
 top:0;
 right:0;
 width:200px;
}

absolute

4. position:relative + position:absolute

若是咱們給 div-1 設置 relative 定位,那麼 div-1 內的全部元素都會相對 div-1 定位。若是給 div-1a 設置 absolute 定位,就能夠把 div-1a 移動到 div-1 的右上方。ui

#div-1 {
 position:relative;
}
#div-1a {
 position:absolute;
 top:0;
 right:0;
 width:200px;
}

relative-and-absolute

5. 兩欄絕對定位

如今就可使用相對定位和絕對定位來作一個兩欄佈局了。spa

#div-1 {
 position:relative;
}
#div-1a {
 position:absolute;
 top:0;
 right:0;
 width:200px;
}
#div-1b {
 position:absolute;
 top:0;
 left:0;
 width:200px;
}

two-column-absolute

6. 兩欄絕對定位定高

一種方案是給元素設定固定高度。但這種方案對大多數設計來講不太適合,由於通常咱們不知道元素中會有多少文本,或者將要使用的精確的字號。設計

#div-1 {
 position:relative;
 height:250px;
}
#div-1a {
 position:absolute;
 top:0;
 right:0;
 width:200px;
}
#div-1b {
 position:absolute;
 top:0;
 left:0;
 width:200px;
}

two-column-absolute-height

7. 浮動

對於可變高度的列來講,絕對定位就不起做用了,如下是另一個方案。code

咱們能夠浮動一個元素,使它移動到左邊/右邊,而且是文本環繞着它。這主要用於圖像,但這裏咱們把它用於一個複雜的佈局任務(由於這是咱們惟一的工具)。orm

#div-1a {
 float:left;
 width:200px;
}

float

8. 浮動列

若是咱們把一個元素向左浮動,而且把第二個元素也向左浮動,they will push up against each other。htm

#div-1a {
 float:left;
 width:150px;
}
#div-1b {
 float:left;
 width:150px;
}

float-columns

9. 清除浮動列

在浮動元素以後,咱們能夠清除浮動來使其餘元素正肯定位。

#div-1a {
 float:left;
 width:190px;
}
#div-1b {
 float:left;
 width:190px;
}
#div-1c {
 clear:both;
}

float-columns-with-clear

雖然我一直用浮動佈局,可是掌握好 position 也是必須的,其實也沒那麼難的。。。

 

 

複製代碼
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>無標題文檔</title>
 <STYLE TYPE="text/css">
 body {
    font-family: tahoma, helvetica, arial, sans-serif;
    font-size: 12px;
    padding: 0;

    text-align:center;
    background-color:#000000;

}
 #Divbody {
  width:1000px;
  height:900px;
  margin:auto; 
}
 #div-before {
  width:100%;
  height:20px;
  background-color:#FF0000;
}
 #div-after {
  width:100%;
  height:20px;
  background-color:#FD0000;
}
#div-1 {
  width:100%;
  height:500px;
  background-color:#999999;
   position:relative;

}
#div-1a {
  width:200px;
  background-color:#3300FF;
  float:left;

}
#div-1b {
  width:300px;
  height:250px;
  background-color:#66FF00;

}
#div-1c {
  width:100px;
  height:250px;
  background-color:#00FFFF;
  margin:auto;

}
 </STYLE>
</head>

<body>
<div id=Divbody>
    <div id=div-before>#div-before</div>
    <div id=div-1>
        <div id=div-1a>Based on a true story, the Chinese ballet which made its premiere in 1964Based on a true story, the Chinese ballet which made its premiere in 1964Based on a true story, the Chinese ballet which made its premiere in 1964 depicts the liberation of a peasant girl on Hainan Island. The classic ballet will be performed by the Chinese Central Ballet Troupe in Guangzhou in June.
        </div>
        <div id=div-1b>Based on a true story, the Chinese ballet which made its premiere in 1964 depicts the liberation of a peasant girl on Hainan Island. The classic ballet will be performed by the Chinese Central Ballet Troupe in Gu.
        </div>
        <div id=div-1c>Based on a true story, the Chinese ballet which made its premiere in 1964 depicts the liberation of a peas.d.        </div>    </div>    <div id=div-after>#div-after</div></div></body></html>
相關文章
相關標籤/搜索