2013-01-04 111 views
3

我寫了下面的代碼。它在Mozilla和Chrome中顯示得非常好。但在Internet Explorer中,它不是中心對齊,而是顯示在左側。這裏是代碼:HTML Div中心在Internet Explorer中對齊

<html> 
<head> 
<title>ProgramEngine Website Registration</title> 
<style type="text/css"> 
div#login 
{ 
background-color:#f1f1f1; 
width: 510px; 
height: 1024px; 
border: 1px solid grey; 
margin-left: auto; 
margin-right: auto; 
} 
</style> 
</head> 
<body> 
<div id="login"></div> 
</body> 
</html> 

所以AnyBody建議我什麼是問題和如何克服它。

+0

你指的是哪個版本的Internet Explorer?這對我來說在IE7 +中效果很好,而且我看不到任何明顯的問題,您的代碼可能會導致 – Sean

+0

您正在測試哪個版本的IE?它似乎在IE7 +中工作得很好。 – BenM

+0

其Internet Explorer 8 – user1936711

回答

4

你可以使用jquery獲得更好的解決方案。

See this Link

$(document).ready(function(e){  
     var screen = $('body').width(); 
     screen -= 51; //51 is width of div 
     screen /= 2; 
     $('#login').offset({top:0,left:screen}); 
    });​ 

,如果您需要在偏移值使用top。

在這裏,瀏覽器不能產生問題。

3

你使用正確的DOCTYPE嗎?如果沒有DOCTYPE,IE會自動進入怪異渲染模式 - 這可能是您的問題。

+0

什麼DOCTYPE IE(所有版本)都支持? – KarSho

+1

使用例如HTML 5 Doctype <!DOCTYPE html>。 – honzahommer