Understanding of margin in CSS

Source: Internet
Author: User

Understanding of margin in CSS


Set the border sequence in margin to upper right bottom left

{margin:top right buttom right}

It is equivalent to

{  margin-top: 20px;  margin-right: 30px;  margin-bottom: 30px;  margin-left: 20px;  }
The omitted writing rules are as follows:

If three values are specified for the outer margin, the 4th values (that is, the left margin) are copied from the 2nd values (the right margin. If two values are given, 4th values are copied from 2nd values, and 3rd values (bottom margin) are copied from 1st values (top margin. In the last case, if only one value is specified, the other three margins are copied from this value (top margin.


Set margin to a negative value.

(Here is a copy of the image on the Internet)


The effect of moving a margin value to a negative value.

The following is an example of DIV Center

This section describes how to set the Div center effect: first, set the Div to "center" in absolute layout or fixed mode. The center here only indicates that the height and width are set to 50%; calculate the width and height of the DIV by 1/2. Finally, set margin-top and margin-left to the negative values of the second step;

Step 1:

.loginModal {    display: none;    background: #FDF3C1;    color: #000000;    padding: 20px;    border: 20px solid #8F6031;    font-size: 1.2em;    float: left;    position: fixed;    height: 175px;    width: 300px;    top: 50%;    left: 50%;    z-index: 200;}
Step 2:

 var modalMarginTop = ($('#' + modalID).height() + 80) / 2;        var modalMarginLeft = ($('#' + modalID).width() + 80) / 2;        /* apply the margins to the modal window */        $('#' + modalID).css({            'margin-top' : -modalMarginTop,            'margin-left' : -modalMarginLeft        });

Conclusion: 1. Set it to a negative margin element and how the system shows the feature margin of a negative number. Because the element itself does not change, the parent equivalent to the element moves in the opposite direction to achieve the effect (personal opinion ).


Related Article

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.