DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS特效代碼
HTML5 元素定位:relative 與 absolute 深入解析
編輯:CSS特效代碼     

元素定位是 CSS 布局的核心知識之一。本篇重點說明相對定位與絕對定位的特性與應用。

1. 相對定位 position:relative

a. 不影響元素本身的特性;
b. 元素不脫離文檔流;
c. 若沒有設定定位偏移量,對元素本身沒有任何影響;
d. 透過 top/bottom/left/right 控制定位元素的位置。

<div class="box1">div1</div> <div class="box2">div2</div> <div class="box3">div3</div> .box1{width:100px;height:100px; background:red;} .box2{width:100px;height:100px;background:blue; position:relative;left:100px;top:100px;} .box3{width:100px;height:100px;background:green;} 

2. 絕對定位 position:absolute

a. 使元素完全脫離文檔流;
b. 使行內元素支援寬高;
c. 由內容撐開寬度。

<div class="box1">123</div> <span class="box2">span1</span> <span class="box3">span2</span> .box1{height: 100px;position: absolute;top: 0;left: 0; background: red;} .box2{width: 100px; height: 100px; position: absolute; top: 0; left: 110px; background: red;} .box3{width: 100px; height: 100px; position: absolute; top: 0; left: 220px; background: red;} 

d. 若有父級則相對於父級發生偏移,若無父級則相對於整個文檔發生偏移;
f. 相對定位一般是配合絕對定位一起使用。

<div class="box1"><!-- 定位父級 --> <div class="box2"><!-- 結構父級 --> <div class="box3"></div><!-- 絕對定位元素 --> </div> </div> .box1{width:300px;height:300px; background:red; position:relative;} .box2{width:200px;height:200px;background:blue;} .box3{width:100px;height:100px;background:green; position:absolute;right:0;bottom:0;} 

4. 定位層級

規則:1. 預設情況下,文檔流中靠後的元素層級高於靠前的元素;
2. z-index:[num] 用來控制定位層級,數值越大越靠上。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved