CSS定位
定位
html
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>定位</title> <link href="style1.css" type="text/css" rel="stylesheet"> </head> <body> <div id="position1"></div> <div id="position2"></div> <script> for(var i=0;i<100;i++){ document.write(i+"<br/>") } </script> </body> </html>
style1.css(默认static)
static(不声明position则默认static,left,top等偏移无效)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#position1{ width:100px; height:100px; background-color:blue; position:static; left:20px; top:20px; } #position2{ width:100px; height:100px; background-color:green; position:static; left:10px; top:10px; }
relative
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#position1{ width:100px; height:100px; background-color:blue; position:relative; left:20px; top:150px; } #position2{ width:100px; height:100px; background-color:green; position:relative; left:30px; top:30px; }
absolute(会影响其它元素位置变动)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#position1{ width:100px; height:100px; background-color:blue; #position:relative; #left:20px; #top:150px; } #position2{ width:100px; height:100px; background-color:green; position:absolute; left:30px; top:30px; }
fixed(不随页面上下拉动而动)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#position1{ width:100px; height:100px; background-color:blue; #position:relative; #left:20px; #top:150px; } #position2{ width:100px; height:100px; background-color:green; position:fixed; left:30px; top:30px; }
z-index(重叠后,哪个显示在前)
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#position1{ width:100px; height:100px; background-color:blue; position:relative; left:20px; top:20px; } #position2{ width:100px; height:100px; background-color:green; position:relative; left:10px; top:10px; }
默认是后面的元素在前
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#position1{ width:100px; height:100px; background-color:blue; position:relative; left:20px; top:20px; z-index:2; } #position2{ width:100px; height:100px; background-color:green; position:relative; left:10px; top:10px; z-index:1; }
z-index值大的在前
谢谢