2011-02-07 12 views
0

當IE8是「正常」標準兼容模式時,下面的html和css做了它應該正確的居中紅色div。但在兼容模式下,它不會居中。這裏的任何人都能夠解釋爲什麼並提出替代方案?div沒有以兼容模式爲中心

<html> 
    <head><title>test</title></head> 
<body> 
    <div 
     style="position: absolute; 
     margin-left: auto; 
     margin-right: auto; 
     left: 0; 
     right: 0; 
     top: 0; 
     width: 900px; 
     background-color: red" 
    > 
     test 
    </div> 
</body> 
</html> 
+1

你指定的doctype?它有所作爲 – Hawxby 2011-02-07 13:15:32

+3

Hawxby說了什麼,還有左邊的組合:0;正確:0;和寬度:900px;沒有多大意義。 – reisio 2011-02-07 13:17:30

回答

1

,使其工作沒有DOCTYPE只是做這樣:

style="position: absolute; 
     margin-left: -450px; 
     left: 50%; 
     top: 0; 
     width: 900px; 
     background-color: red" 
1
<!doctype html> 
<html> 
    <head> 
     <title></title> 
     <style> 
div { 
    position: absolute; 
    top: 50%; 
    left: 50%; 
    width: 300px; 
    height: 300px; 
    margin: -150px 0 0 -150px; 
    background: navy; 
} 
     </style> 
    </head> 
    <body> 
     <div></div> 
    </body> 
</html>