position屬性有4個值:
默認是static,無特殊定位,對象遵循HTML定位規(guī)則(一般使用margin、padding)
fixed未支持。對象定位遵從絕對(absolute)方式。但是要遵守一些規(guī)范
absolute絕對定位(相對于最近的父元素,假如父元素都是默認的static,那么將相對body進行定位,如果父元素為relative定位,那么就相對該父元素進行定位)
relative相對定位(相對于自己,在原來位置上移動。與static相比,top、bottom、left、rigth生效,而static不生效)
relative不脫離文檔流,而absolute脫離文檔流。
例子:
<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Strict//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd”>
<html xmlns=”http://www.w3.org/1999/xhtml”>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=utf-8″>
<title>position</title>
<style type=”text/css”>
<!–
body{
font-size:12px;
margin:0 auto;
}
div#demo{
position:relative;
border:1px solid #000;
margin:50px;
top:-50px;
line-height:18px;
overflow:hidden;
clear:both;
height:1%;
}
div#sub{
position:absolute;
right:10px;
top:10px;
}
div.relative{
position:relative;
left:400px;
top:-20px;
}
div.static,div.fixed,div.absolute,div.relative{
width:300px;
}
div.static{
background-color:#bbb;
position:static;
}
div.fixed{
background-color:#ffc0cb;
}
div.absolute{
background-color:#b0c4de;
}
div.relative{
background-color:#ffe4e1;
}
–>
</style>
</head>
<body>
<div id=”demo”>
<div class=”static”>static: 默認值。無特殊定位,對象遵循HTML定位規(guī)則 </div>
<div id=”sub” class=”absolute”>absolute: 將對象從文檔流中拖出,使用left,right,top,bottom 等屬性相對于其最接近的一個最有定位設置的父對象進行絕對定位。如果不存在這樣的父對象,則依據(jù)body對象。而其層疊通過z-index屬性定義 </div>
<div class=”fixed”>fixed:未支持。對象定位遵從絕對(absolute)方式。但是要遵守一些規(guī)范 </div>
<div class=”relative”>relative:對象不可層疊,但將依據(jù) left,right,top,bottom 等屬性在正常文檔流中偏移位置 </div>
</div>
</body>
</html>
新聞熱點
疑難解答