# CSS Margins

Margins specify the space and position of the element on the web-page. There are basically 4 properties that can be used to specify the margins for the elements:

Run

• margin-top
• margin-bottom
• margin-right
• margin-left

Run

The value for the margin properties can be of following 4 types:

• auto: Auto-Calculated Margins
• length: Specific length in px, cm, pt etc.
• percentage: Specific percentage of the containing element
• inherit: The margin here is inherited from the parent element
• We will now know more about auto and inherit values.

## auto value

The auto Value provides the margin so that the element is horizontally centered in its containing element. Here’s an example for the same:

Run

Here the property being used for margin is the shorthand property for margin.
Although, the auto Value is only for the horizontal margins, for the rest of the values, the order of margin properties is
margin-top>margin-right>margin-bottom>margin-left
A sample code for the same is:

Run

## inherit value

As specified earlier, the margin value is inherited from the parent element. For example, if a paragraph or <p> element has its parent element as a <div> then the margin can be inherited as:

Run

Thus, the <p> element will have a right margin of 100px whenever it is being used within the <div>element.

## Margin collapse

The margin property has another interesting feature of collapsing into a single margin for the top and bottom values. The following example will help to understand this better:

Here, the bottom margin for h1 is 40 and the top margin for h2 is 30. Logically, the total margin between h1 and h2 should be 40+30=70px but due to margin collapse, it will be 40px as that is the greater value.
{"ft_lang_label":"__label__en","ft_lang_prob":0.65801585,"math_prob":0.7679973,"size":2392,"snap":"2021-21-2021-25","text_gpt3_token_len":695,"char_repetition_ratio":0.16666667,"word_repetition_ratio":0.046728972,"special_character_ratio":0.30183947,"punctuation_ratio":0.13806707,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.95828426,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-06-17T19:43:01Z\",\"WARC-Record-ID\":\"<urn:uuid:0e4de5ac-47d7-4d0c-b83c-455ef8975f96>\",\"Content-Length\":\"72632\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:cc5e57e8-daa3-4b84-8f17-60de7c780dfc>\",\"WARC-Concurrent-To\":\"<urn:uuid:40f78d18-d624-462d-b7cd-2ca993bc7ef5>\",\"WARC-IP-Address\":\"104.21.29.193\",\"WARC-Target-URI\":\"https://www.ebhor.com/css-margins/\",\"WARC-Payload-Digest\":\"sha1:CHAEGESTZ6GJGCSDNAKFUNXM5HNCVRAQ\",\"WARC-Block-Digest\":\"sha1:7RGQKBQUNYOVTDPTU75LREKJUTRKNRN7\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-25/CC-MAIN-2021-25_segments_1623487633444.37_warc_CC-MAIN-20210617192319-20210617222319-00009.warc.gz\"}"}