Using margin:0 Auto page in IE cannot be centered

Source: Internet
Author: User

Today, Xiao Tao accidentally found a problem, that is, some pages in IE browser when the margin:0 Auto page can not be centered, other browsers are normal, search for the discovery of two solutions:

1. Change DOCTYPE to XHTML 1.0 tranditional (Invalid under HTML 4.01)
2, add div outside the block, and apply text-align:center to this div


Add a 3C invocation declaration

HTML code

The code is as follows Copy Code


<! DOCTYPE HTML PUBLIC "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd ">

I don't want to add

There is also a saying that to add a property to the body

HTML code

The code is as follows Copy Code
Text-align:center;

I don't want to add it, it's a little steep.

Since this attribute can be added to the body, why not a div with this attribute on the outer bread?
Test, through

The code is as follows Copy Code


<div style= "Text-align:center;" >
<div style= "margin:0 auto;" > Center </div>
</div>

A friend on the internet said

You can try using absolute positioning to half of the parent element, position:absolute;left:50%; Then use the left negative margin of half the width of the centered element, for example, the center element width is 760px; margin-left:-360px

Summary:

The difference between IE6 and Firefox, IE's text-align:center can make it contain the block elements and inline elements and text content centered.
Under Firefox, text-align:center only contains inline elements and text content that is centered.

Margin:0 Auto Enables a block element to be centered, but does not center inline elements, which are the same under Firefox and IE6. For IE6 Previous versions of browsers need to match the text-align:center of their parent elements in order to center, you also use for IE under the margin : 0 Auto Page can not center the problem headache?

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.