日期:2014-05-17  浏览次数:20741 次

css 绝对定位 和相对定位问题
绝对定位的div 为什么显示在 相对定位的td 下方代码如下:<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "//www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="//www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
<style type="text/css">
</style>
</head>
<body>
<table width="395" border="1">
  <tr>
  <td>&nbsp;</td>
  <td style="position:relative;" height="20">sadfsadfsadfsdaf<div style=" position:absolute; width:500px; background:#006600; top:15px; left:-20px;"></div></td>
  <td style="position:relative;" height="20">要求在此基础上<div style="position:absolute; width:500px; top:30px; left:-100px; background:#000000;"></div></td>
  </tr>
  <tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  </tr>
  <tr>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  <td>&nbsp;</td>
  </tr>
</table>
</body>
</html>


------解决方案--------------------
<td style="position:relative;" height="20"> sadfsadfsadfsdaf <div style=" position:absolute; width:500px; background:#006600; top:15px; left:-20px;"> </div> </td>
你的td写了position,所以其中的div是相对于父节点td的absolute,
所以你可以去掉td的position就可以看到效果
------解决方案--------------------
不明白你的效果,你把两个div,top:0px; left:0px;就知道他是如何定位的了