Workarounds for margin:0 Auto invalidation in CSS

Source: Internet
Author: User
Workarounds for margin:0 Auto invalidation in CSS

<div class= "Div1" style= "width:100%; height:60px; Float:left; Background: #CCC; " ><div class= "Div2" style= "Width:auto; height:100%; margin:0 Auto; Background: #F00; " >111111111</div></div>

Div1 width is screen width, div2 width varies with content size and centered with Div1, but div2 width I set to auto width or div1 100% width, why this? Study the whole night did not understand, hope to have a master can answer?

To Div2 outside a set of Div3, set div3 for the horizontal center display, that is, text-align:center;div2 set as an inline element, that is, display:inline;

Div defaults to block-level elements, and block-level elements are all on their own, with a width default of 100%.

Running this code under IE8 is not centered except for the body setting Text-align:center are there any other workarounds?

<!doctype html>

Pro-Test IE8 Center No problem

If you want text centered, set the. Tcenter{text-align:center}

margin:0 Auto; is to let the div center, not to let the text center.

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.