Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Internet Explorer CSS - Center Div

I recently made a website but I'm having a really big problem with Internet Explorer. The CSS seems to do strange things in IE. Rather than copying hundreds of lines of CSS here is a link to a page of the site: http://www.appwheal.com/app/ios/angry-birds-space All the content should be displayed in the middle of the screen. Instead with IE it is stuck to the left.

I'm using

margin-left: auto;

margin-right: auto;

This is supported by IE, right.

Please help and thanks in advance.

like image 428
John Wheal Avatar asked Dec 09 '22 00:12

John Wheal


2 Answers

You need to declare a DOCTYPE, or Internet Explorer defaults to Quirks mode (IE5 compatibility). Go into Internet Explorer, hit F12 to bring up Developer tools, and notice that it shows "Quirks" mode under Document Mode. Quirks doesn't support any of the known div centering methods around, and declaring the DOCTYPE is the easiest (and recommended) way to fix it.

To set your page for XHTML 1.0 Transitional (which is the most common), use

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

To declare the page is HTML5 compatible, use

<!DOCTYPE html>

The DOCTYPE line needs to be the first line in the html file, appearing BEFORE the opening <html> tag.

like image 88
saluce Avatar answered Dec 29 '22 09:12

saluce


You must also set

body {
    text-align: center;
}

#yourDiv {
    margin: 0 auto;
    text-align: left;
}
like image 23
Kristian Avatar answered Dec 29 '22 08:12

Kristian