ie7中绝对位置问题,div向右移动10px
我在 IE7 上的绝对位置属性上遇到了问题。我的 div 向右移动 10px。下面是我的代码。 IE8和9运行良好。 id menu 是我指的 div。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body{margin: 0 0 0 0; padding: 0 0 0 0;}
#holder{width: 400px; height: 500px; margin: 0 auto;}
#left{float: left; width: 50px; height: 500px; background-color: red;}
#center{float: left; width: 300px; height: 500px; background-color: green;}
#right{float: left; width: 50px; height: 500px; background-color: red;}
#header{width: 300px; height: 70px; background-color: yellow;}
#gal-holder{width: 280px; height: 70px; margin: 0 auto;}
#gallery{width: 280px; height: 410px; background-color: orange;}
#button{width: 400px; height: 45px; background-color: red; margin: 0 auto;}
#menu{width: 300px; height: 45px; background-color: pink; position: absolute; z-index: 1000; top: 100px;}
#content{width: 380px; height: 200px; margin: 0 auto; background-color: blue; padding: 10px; color: #fff;}
#clear{height: 10px;}
</style>
</head>
<body>
<div id="holder">
<div id="left"></div>
<div id="center">
<div id="header"></div>
<div id="menu"></div>
<div id="gal-holder">
<div id="clear"></div>
<div id="gallery"></div>
<div id="clear"></div>
</div>
</div>
<div id="right"></div>
</div>
<div id="button"></div>
<div id="content">Sample text</div>
</body>
</html>
ive got a problem on my position absolute property on IE7. My div moves 10px to the right. Below is my code. IE8 and 9 works fine.
id menu is the div Im referring.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
body{margin: 0 0 0 0; padding: 0 0 0 0;}
#holder{width: 400px; height: 500px; margin: 0 auto;}
#left{float: left; width: 50px; height: 500px; background-color: red;}
#center{float: left; width: 300px; height: 500px; background-color: green;}
#right{float: left; width: 50px; height: 500px; background-color: red;}
#header{width: 300px; height: 70px; background-color: yellow;}
#gal-holder{width: 280px; height: 70px; margin: 0 auto;}
#gallery{width: 280px; height: 410px; background-color: orange;}
#button{width: 400px; height: 45px; background-color: red; margin: 0 auto;}
#menu{width: 300px; height: 45px; background-color: pink; position: absolute; z-index: 1000; top: 100px;}
#content{width: 380px; height: 200px; margin: 0 auto; background-color: blue; padding: 10px; color: #fff;}
#clear{height: 10px;}
</style>
</head>
<body>
<div id="holder">
<div id="left"></div>
<div id="center">
<div id="header"></div>
<div id="menu"></div>
<div id="gal-holder">
<div id="clear"></div>
<div id="gallery"></div>
<div id="clear"></div>
</div>
</div>
<div id="right"></div>
</div>
<div id="button"></div>
<div id="content">Sample text</div>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将
position:relative
添加到#center
,然后将left:0px
添加到#menu
。绝对定位的元素相对于其最近定位的父元素进行定位。最好为您想要放置的项目提供左/右和上/下坐标,以防止像您找到的那样出现奇怪的结果。
Add
position:relative
to#center
and thenleft:0px
to#menu
.Absolutely positioned elements are positioned relative to their closest positioned parent. It's best to give the items you want to position a left/right and top/bottom coordinate to prevent weird results like the one you found.
您应该指定顶部和左侧位置,并将
position:relative
添加到直接父级:以及一个实例: http://jsfiddle.net/ambiguously/vRJMd/
默认的
left
是auto
,这或多或少意味着浏览器可以做任何它认为是的事情明智的。此外,绝对定位的元素相对于最近的祖先进行定位,其位置不是static
(在您的情况下可能是)。
You should specify both the top and left positions and add
position:relative
to the immediate parent:And a live example: http://jsfiddle.net/ambiguous/vRJMd/
The default
left
isauto
and that more or less means that the browser can do whatever it thinks is sensible. Also, absolutely positioned elements are positioned with respect to the nearest ancestor with a position that is notstatic
(which is probably<body>
in your case).