Margin-top and Margin-left are not only different in direction

Source: Internet
Author: User

First look at the following code:

 
  
  1. <body style= "margin:0px;padding:0px;" >
  2. <div style= "Background-color: #FF0000;" >
  3. <div style= "Margin:10px;background-color: #00FF00;" >cftea</div>
  4. </div>
  5. </body>

This code shows the same results in each browser, and the following illustration shows a screenshot from Chrome:

This means that the margin-top is relative to the body, and the margin-left is relative to the outer div. Here said that relative to the body may not be accurate, such as the outermost set a table, it is relative to the table TD.

Now look at another piece of code:

 
  
  1. <body style= "margin:0px;padding:0px;" >
  2. <div style= "Width:100px;background-color: #FF0000;" >
  3. <div style= "Margin:10px;background-color: #00FF00;" >cftea</div>
  4. </div>
  5. </body>

For the outer div increased width, in Firefox, the effect is unchanged, in IE became such:

This shows that after specifying the width of the outer div, in IE, the inner div margin-top is relative to the outer div.

Finally, look at another piece of code:

 
  
  1. <body style= "margin:0px;padding:0px;" >
  2. <div style= "Position:absolute;background-color: #FF0000;" >
  3. <div style= "Margin:10px;background-color: #00FF00;" >cftea</div>
  4. </div>
  5. </body>

Change the width:100% to Position:absolute, at this time, the performance results are the same in each browser, the inner div margin-top, margin-left are relative outer Div, and the outer div width is no longer 100%, but tightening to paste Near the inner layer div's margin edge.



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.