8

IE7 の位置絶対プロパティで問題が発生しました。私のdivは右に10px移動します。以下は私のコードです。IE8 と 9 は正常に動作します。 id menuは、Im が参照している 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>
4

2 に答える 2

18

に追加position:relativeしてから#centerに追加left:0px#menuます。

絶対配置要素は、最も近い位置にある親を基準にして配置されます。あなたが見つけたような奇妙な結果を防ぐために、配置したいアイテムに左/右と上/下の座標を与えるのが最善です.

于 2011-02-24T06:25:11.507 に答える
7

上と左の両方の位置を指定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/ambiguous/vRJMd/

デフォルトleftautoであり、多かれ少なかれ、ブラウザが適切だと判断したことは何でも実行できることを意味します。また、絶対配置された要素は、そうではない位置を持つ最も近い祖先に対して配置されますstatic(おそらく<body>あなたの場合です)。

于 2011-02-24T06:31:58.513 に答える