ie7中绝对位置问题,div向右移动10px

发布于 2024-10-19 06:04:11 字数 1833 浏览 4 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

扛起拖把扫天下 2024-10-26 06:04:11

position:relative 添加到 #center,然后将 left:0px 添加到 #menu

绝对定位的元素相对于其最近定位的父元素进行定位。最好为您想要放置的项目提供左/右和上/下坐标,以防止像您找到的那样出现奇怪的结果。

Add position:relative to #center and then left: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.

酒几许 2024-10-26 06:04:11

您应该指定顶部和左侧位置,并将 position:relative 添加到直接父级:

#center {
    float: left;
    width: 300px;
    height: 500px;
    background-color: green;
    position:relative;
}
#menu { 
    width: 300px;
    height: 45px;
    background-color: pink;
    position: absolute;
    z-index: 1000;
    top: 100px;
    left: 0;
}

以及一个实例: http://jsfiddle.net/ambiguously/vRJMd/

默认的 leftauto ,这或多或少意味着浏览器可以做任何它认为是的事情明智的。此外,绝对定位的元素相对于最近的祖先进行定位,其位置不是static(在您的情况下可能是)。

You should specify both the top and left positions and add position:relative to the immediate parent:

#center {
    float: left;
    width: 300px;
    height: 500px;
    background-color: green;
    position:relative;
}
#menu { 
    width: 300px;
    height: 45px;
    background-color: pink;
    position: absolute;
    z-index: 1000;
    top: 100px;
    left: 0;
}

And a live example: http://jsfiddle.net/ambiguous/vRJMd/

The default left is auto 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 not static (which is probably <body> in your case).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文