css:margin:0 auto can't be centered in ie8

黄舟
Release: 2017-07-21 13:43:54
Original
1995 people have browsed it

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">
Copy after login

and add:

in the head
<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/> 即可!
Copy after login


Method 2:

Body settings {text-align:center;width:100%;}
Include all divs in

, wrap is set as follows:

 #wrap    
 { 
 text-align:left; 
 width:***;  
 margin:0 
 auto; 
  }
Copy after login

and add in the head:

<meta http-equiv="X-UA-Compatible" content="IE=EmulateIE7"/>
Copy after login

That’s it!

Method three:

Use

to replace the wrap layer in the second one.

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!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template