为什么这个div / img不会在IE8中居中?

时间:2009-05-03 04:42:08

标签: html css internet-explorer-8

我有一个非常简单的控制页面,我以div,锚和图像为中心。出于某种原因,它不会以IE8(任一模式)为中心,我希望有人可以告诉我原因。我没有机会在其他IE浏览器中尝试它。我已经在Chrome和FF3中尝试过这种方法,它可以正常工作。

<html>
<head>
<title>Welcome</title>
<style>
    #pageContainer {width:300px;margin:0 auto;text-align:center;}
    #toLogo{border:none; }
</style>
</head>
<body>
    <div id="pageContainer">
    <a href="http://portal.thesit.com" id="toSite"><img src="LOGO_DNNsmall.png" id="toLogo"></a>
    </div>
</body>
</html>

我说这很简单。 :)

谢谢,

布雷特

6 个答案:

答案 0 :(得分:70)

你真的希望你的页面在怪癖模式下工作吗?一旦我将doctype添加到强制标准模式,你的HTML就可以了:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">

<head>
<title>Welcome</title>
<style>    
    #pageContainer {width:300px;margin:0 auto;text-align:center;}    
    #toLogo{border:none; }
</style>
</head>

<body>

<div id="pageContainer">
    <a href="http://portal.thesit.com" id="toSite">
    <img src="http://stackoverflow.com/content/img/so/logo.png" id="toLogo"></a> </div>

</body>
</html>

答案 1 :(得分:3)

div旁边的auto边距让浏览器决定它的位置。没有什么可以告诉浏览器div应该在主体中心,或左或右对齐。所以这取决于浏览器。如果向主体添加指令,则问题将得到解决。

<html>
  <head>
    <title>Welcome</title>
    <style>
      body { text-align: center;}
      #pageContainer {width:300px; margin:0px auto;
            text-align:center; border:thin 1px solid;}
      #toLogo{border:none; }
    </style>
  </head>
  <body>
    <div id="pageContainer">
      <a href="http://portal.thesit.com" id="toSite">
        <img src="LOGO_DNNsmall.png" id="toLogo">
      </a>
    </div>
  </body>
</html>

我在div中添加了1px边框,以便您可以更清楚地看到发生的事情。

你将它留给了浏览器,因为它处于怪癖模式。要删除怪异模式,请将doctype定义添加到顶部,如下所示:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
  <head>
    <title>Welcome</title>
    <style>
      #pageContainer {width:300px; margin:0px auto;
            text-align:center; border:thin 1px solid;}
      #toLogo{border:none; }
    </style>
  </head>
  <body>
    <div id="pageContainer">
      <a href="http://portal.thesit.com" id="toSite">
        <img src="LOGO_DNNsmall.png" id="toLogo">
      </a>
    </div>
  </body>
</html>

现在,您将能够在页面上看到300 px div中心。

答案 2 :(得分:2)

text-align:center添加到正文中。当与div上的margin:0 auto结合使用时,应该这样做。

通过将整个页面内容包装在全宽容器中,您可以在不使用身体text-align:center的情况下居中。然后在此设置text-align:center

<html>
<head>
<title>Welcome</title>
<style>
    #container {text-align:center;border:1px solid blue}
    #pageContainer {width:300px; margin:0 auto; border:1px solid red}
    #toLogo{border:none; }
</style>
</head>
<body>
    <div id="container">
        <div id="pageContainer">
        <a href="http://portal.thesit.com" id="toSite"><img src="LOGO_DNNsmall.png" id="toLogo"></a>
        </div>
    </div>
</body>
</html>

(我添加了container div)。它并没有真正改变任何东西......只是一个额外的div。你仍然需要所有相同的css属性。

答案 3 :(得分:0)

您可能希望将其更改为以下内容:

<html>
<head>
<title>Welcome</title>
<style>
    body { text-align: center; }
    #pageContainer {width:300px;margin:0 auto;}
    #toLogo{border:none; }
</style>
</head>
<body>
    <div id="pageContainer">
    <a href="http://portal.thesit.com" id="toSite"><img src="LOGO_DNNsmall.png" id="toLogo"></a>
    </div>
</body>
</html>

text-align:center;移至正文。如果您想在div #pageContainer中放置其他对齐的左侧内容,则该类需要text-align:left;。这是我现在在很多网站中使用的解决方案,似乎适用于所有浏览器(这是Dreamweaver在其初始模板中使用的)。

答案 4 :(得分:0)

对于BLUEPRINT用户 这开了我的坚果,直到我找到这个帖子:problem with ie8 and blueprint 长话短说,在你的html代码改变

 <!--[if IE]>
 <link rel="stylesheet" href="../css/blueprint/ie.css" type="text/css" media="screen, projection" /> 
<![endif]--> 

<!--[if lt IE 8]>
<link rel="stylesheet" href="../css/blueprint/ie.css" type="text/css" media="screen, projection" />
<![endif]-->

此致 亚历

答案 5 :(得分:-1)

这适用于IE6,7,8,FF 3.6.3:

    #container
    {
        width:100%;
    }

    #centered
    {
        width:350px;
        margin:0 auto;
    }

    <div id="container">
        <div id="centered">content</div>
    </div>