---
title: "IE6、7、8、9下 DIV  的 position:absolute"
author: "slikel"
date: "2012-11-29T10:36:09Z"
tags: []
categories: ["Web前端"]
slug: "ie6-7-8-9-div-position-absolute"
draft: false
lastmod: "2012-11-29T10:41:01Z"
id: 1738064
url: "https://slikel.iteye.com/blog/1738064"
---

今天看到这篇来自[http://www.light-star.net/itemid3id155atpage.html](http://www.light-star.net/itemid3id155atpage.html)的文章。上面说到了margin-top和position:absolute的冲突问题。标题引起了我的好奇。我们一起来看看吧

先看一个代码：

&lt;div id="layer1" style="margin:20px; border:1px solid #F88; width:400px; "&gt;  
  &lt;div id="layer2" style="position:absolute; background-color:#ccc;"&gt;Absolute&lt;/div&gt;  
  &lt;div id="layer3" style="margin:30px auto; width:200px; height:80px; background-color:#36F;"&gt;Normal&lt;/div&gt;  
&lt;/div&gt;

这个代码在FF和IE8下都没有任何问题的，但是在IE6和IE7下有人如下两个bug：

1，绝对定义(position:absolute)的相邻元素margin-top失效，但如果相邻元素(layer3)去掉width属性，margin-top又会生效。

2，layer1无法靠左，距离左边的距离为layer1的第一个非绝对定义元素(layer3)的margin-left值。

解决方法：

1，添加代码：&lt;!--\[if lte IE 7]&gt;&lt;div&gt;&lt;/div&gt;&lt;!\[endif]--&gt;，这也是网上找到的能够完全解决问题的方法。即代码变为：

&lt;div style="margin:20px; border:1px solid #F88; width:400px; "&gt;  
  &lt;div style="position:absolute; background-color:#ccc;"&gt;Absolute&lt;/div&gt;  
  &lt;!--\[if lte IE 7]&gt;&lt;div&gt;&lt;/div&gt;&lt;!\[endif]--&gt;  
  &lt;div style="margin:30px auto; width:200px; height:80px; background-color:#36F;"&gt;Normal&lt;/div&gt;  
&lt;/div&gt;

2，外围元素加position:relative定义，绝对定义元素加left和top定义。此方法可以解决第二个bug，无法解决第一个bug。也有说法用padding-top替代margin-top的，但是有时可以这样，有时候毕竟不行的。代码为：

&lt;div style="margin:20px; border:1px solid #F88; width:400px; position:relative"&gt;  
  &lt;div style="position:absolute; background-color:#ccc; left:0; top:0;"&gt;Absolute&lt;/div&gt;  
  &lt;div style="margin:30px auto; width:200px; height:80px; background-color:#36F;"&gt;Normal&lt;/div&gt;  
&lt;/div&gt;

3，这也是我这个文章所要说的最完美的解决方法，就是给绝对定义元素添加“float:left; display:inline;”定义。即代码变为：

&lt;div style="margin:20px; border:1px solid #F88; width:400px;"&gt;  
  &lt;div style="position:absolute; background-color:#ccc; float:left; display:inline;"&gt;Absolute&lt;/div&gt;  
  &lt;div style="margin:30px auto; width:200px; height:80px; background-color:#36F;"&gt;Normal&lt;/div&gt;  
&lt;/div&gt;

 

好了，到我发言了，首先第3个解决方案我认为也有不太正确的地方，所以既然用个容器来包裹绝对定位的容器，那为啥不要父元素上加个 position:relative;呢？这样更符合标准啊，不是吗？

另外position:absolute和float:left一起使用我还是第一次看到，原谅我的肤浅吧。感觉用float:left 还不如用left:0更好呢。至于margin-top失效我还真的不太明白原因，所以我有了padding-top来代替。下面贴出我重新改过的代码，希望各位大侠路过可以看看，有不对的也顺便提一提纠正一下：

&lt;div style=" position:relative; margin:20px; border:1px solid #F88; width:400px; padding-top:30px"&gt;  
&lt;div style="position:absolute; background-color:#ccc; left:0; top:0;"&gt;Absolute&lt;/div&gt;  
&lt;div style="margin:0 auto 30px auto; width:200px; height:80px; background-color:#36F;"&gt;Normal&lt;/div&gt;  
&lt;/div&gt;

 

 

（源自[http://blog.sina.com.cn/s/blog\_77dbebbb0100u7hy.html](http://blog.sina.com.cn/s/blog_77dbebbb0100u7hy.html)）

 

 

      我解决的办法是在父DIV上添加position: relative，在子DIV（包含属性position:absolute）与之兄弟DIV之间添加一个空白的DIV。