The understanding of margin

Source: Internet
Author: User

1. Box model

In the design of the Web page, we use the box model, the content is as follows:

The entire Web page is a large box of small boxes, small boxes and smaller boxes to achieve. However, in the design of Web pages are always confused about the use of margin and padding, here specifically to explore.

2.margin 1.margin-top

Only the block element has margin-top, the inline element is not margin-top. Margin-top is calculated as a reference point with the lower bounds of the sibling element or the parent element with the top border.

    • Parent element has no top border
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metahttp-equiv= "X-ua-compatible"content= "Ie=edge"><title>Driving School Registration</title><Metaname= "Viewport"content= "Width=device-width, initial-scale=1"><!--[If Lt IE 9]><script src= "js/html5shiv.min.js" ></script><script src= "Js/respond.min.js" > </script><! [EndIf] -<styletype= "Text/css">Body{Min-width:1024px;margin:0px Auto;font-family:' Microsoft Ya Black ';    }. Fdiv{width:200px;Height:200px;background:#ccc;margin:0;padding:0;    }. Cdiv{width:100px;Height:100px;background:#FFF7DC;Margin-top:20px;Border:1px solid Black;    }a{Border:1px solid Black;    }</style></Head><Body><Divclass= "Fdiv">    <Divclass= "Cdiv">    </Div><Div></Body></HTML>

Since the beige part of the DIV can not find a reference point, we found that the results are very wonderful, the body on the upper boundary as a reference point.

    • parent element has upper bounds
. fdiv{        width:200px; height:200px; background: #ccc;        margin:0;        padding:0;        border-top:1px solid black;  }

We found that once the parent element has the upper bounds set, the margin-top is displayed as normal.

    • Take the brother element as a reference
<!DOCTYPE HTML><HTML><Head><MetaCharSet= "Utf-8"><Metahttp-equiv= "X-ua-compatible"content= "Ie=edge"><title>Driving School Registration</title><Metaname= "Viewport"content= "Width=device-width, initial-scale=1"><!--[If Lt IE 9]><script src= "js/html5shiv.min.js" ></script><script src= "Js/respond.min.js" > </script><! [EndIf] -<styletype= "Text/css">Body{Min-width:1024px;margin:0px Auto;font-family:' Microsoft Ya Black ';    }. Fdiv{width:200px;Height:200px;background:#ccc;margin:0;padding:0;Border:1px solid Black;    }. Cdiv{width:100px;Height:100px;background:#FFF7DC;Margin-top:20px;Border:1px solid Black;    }a{Border:1px solid Black;    }</style></Head><Body><Divclass= "Fdiv">    <a>Ffasdfasdfasfdafdasdf</a>    <Divclass= "Cdiv">    </Div><Div></Body></HTML>

The margin-top of the beige section Div is the reference line in the lower bounds of the a tag to calculate the downward offset (20px).

Understanding of margin

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.