position屬性有4個(gè)值:
默認(rèn)是static,無(wú)特殊定位,對(duì)象遵循HTML定位規(guī)則(一般使用margin、padding)
fixed未支持。對(duì)象定位遵從絕對(duì)(absolute)方式。但是要遵守一些規(guī)范
absolute絕對(duì)定位(相對(duì)于最近的父元素,假如父元素都是默認(rèn)的static,那么將相對(duì)body進(jìn)行定位,如果父元素為relative定位,那么就相對(duì)該父元素進(jìn)行定位)
relative相對(duì)定位(相對(duì)于自己,在原來(lái)位置上移動(dòng)。與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: 默認(rèn)值。無(wú)特殊定位,對(duì)象遵循HTML定位規(guī)則 </div>
<div id=”sub” class=”absolute”>absolute: 將對(duì)象從文檔流中拖出,使用left,right,top,bottom 等屬性相對(duì)于其最接近的一個(gè)最有定位設(shè)置的父對(duì)象進(jìn)行絕對(duì)定位。如果不存在這樣的父對(duì)象,則依據(jù)body對(duì)象。而其層疊通過(guò)z-index屬性定義 </div>
<div class=”fixed”>fixed:未支持。對(duì)象定位遵從絕對(duì)(absolute)方式。但是要遵守一些規(guī)范 </div>
<div class=”relative”>relative:對(duì)象不可層疊,但將依據(jù) left,right,top,bottom 等屬性在正常文檔流中偏移位置 </div>
</div>
</body>
</html>
新聞熱點(diǎn)
疑難解答
圖片精選