Today I wrote a div, using margin:0 auto; to define its attributes and center it, but it turned out to be invalid.
At first, I thought it was a code conflict in the css. I checked it several times and found that there was no problem. Then I tested it with Firefox and Google Chrome and found that it was centered. . .
It seems to be a browser compatibility issue. I suddenly remembered that I am using win7 and ie has been upgraded to ie8. It seems that the problem is the compatibility of ie8. Baidu. . . .
The solution is copied below:
To achieve centering under IE6, 7, and Firefox, margin: auto is generally used to achieve it, but under IE8, this is not enough. Two solutions are provided:
Method 1 (recommended):
First, the document type must be set at the top of the page:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transition al.dtd">
and add:
in the head<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/> 即可!
Method 2:
Body settings {text-align:center;width:100%;}
Include all divs in
#wrap { text-align:left; width:***; margin:0 auto; }
and add in the head:
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
That’s it!
Method three:
Use
The above is the detailed content of css:margin:0 auto can't be centered in ie8. For more information, please follow other related articles on the PHP Chinese website!