What are the rules of margin collapse in CSS ?
Last Updated :
11 May, 2023
In this article we will learn the rules of margin collapse in CSS, Margin collapsing is a behavior of CSS where the vertical margins of block-level elements are combined into one i.e. to the margin of the element with the largest value. Sometimes when we assigned margins to elements it does not work in the way we think it to and that creates confusion. The following rules will help you understand margin collapsing:
Rule 1: Only vertical margins of block-level elements are collapsed: The first rule is that only the vertical margins of elements will collapse and not the horizontal margins. The CSS rules that govern margin collapsing say that horizontal margins can never satisfy the required conditions.
Example 1: Here is the implementation of the above-explained rule.
HTML
< html >
< head >
< style >
p {
font-size: 24px;
margin-top: 34px;
margin-bottom: 34px;
background-color: rgb(79, 236, 119);
}
</ style >
</ head >
< body >
< div class = "container" >
< p >
This paragraph's bottom
margin is collapsed.
</ p >
< p >
This paragraph is 34px
below the above text.
</ p >
</ div >
</ body >
</ html >
|
Output: In this example, instead of sitting 68px apart the 34px margins of the first and second <p> tag merge, occupying the same space. As the bottom margin of the first paragraph merges with the top margin of the second paragraph. Hence, the space between the two-paragraph is 34px only.

Example 2: This example demonstrates what will happen when horizontal margins are used.
HTML
< html >
< head >
< style >
p {
font-size: 24px;
display: inline-block;
margin-left: 34px;
margin-right: 34px;
background-color: rgb(79, 236, 119);
}
</ style >
</ head >
< body >
< p >This is paragraph 1.</ p >
< p >This is paragraph 2.</ p >
</ body >
</ html >
|
Output: It is clear that horizontal margins do not collapse. Margin collapsing only happens to block-level elements. Other than block-level elements no other element margin can collapse. Here, we have two <p> tags that were inline-block, hence their margin does not collapse.

Rule 2: The elements should be adjacent: Margin collapsing occurs only when the block elements come in direct contact with each other. They should not be separated by any line break or other elements. We generally provide <br> tag between two elements, but due to this, the margins will not collapse.
Example: In this example, we provide a line break between two elements.
HTML
< html >
< head >
< style >
p {
margin-top: 32px;
margin-bottom: 32px;
background-color: rgb(79, 236, 119);
}
</ style >
</ head >
< body >
< p >This is paragraph 1</ p >
< br >
< p >This is paragraph 2.</ p >
</ body >
</ html >
|
Output: You can see in the output that the top margin of the second <p> tag does not collapse with the bottom margin of the first <p>tag and this happens due to the line break between the two elements. Elements must be adjacent for margin collapsing.

Rule 3. The element with the bigger margins will be used: This rule decides what will happen when the margins are asymmetrical. That means if the top element wants 62px of space below, while the bottom element only needs 24px above. In this condition, the space between the two elements will be 62px only. As it is already explained above if you have two adjacent elements with no padding, border, or line break between them (i.e., their margins touch vertically), then their margin will collapse, and the larger margin of the two wins.
Rule 4: The overflow property should be set to visible: Margins of elements do not collapse when the value of the overflow property of the elements is set to anything other than visible. Its value must be visible so that margin can collapse. Hence, overflow: hidden and overflow: auto will not let the margin collapse.
Rule 5: Negative Margins will add up: A negative margin is used to reduce the space between two elements. It put elements closer to each other. Consider that margin-bottom of the first element is -65px and the margin-top of the second element is -25px. In that case, the space between the two elements will be -65px as it is more significant than -25px. But when one margin is positive and another is negative then these margins are added to get the space between the elements.
Example:
HTML
< html >
< head >
< style >
div {
font-size: 24px;
height: 75px;
background-color: rgb(79, 236, 119);
}
#b1 {
margin-bottom: 50px;
}
#b2 {
margin-top: -25px;
}
</ style >
</ head >
< body >
< div id = "b1" >This is block 1.</ div >
< div id = "b2" >This is block 2.</ div >
</ body >
</ html >
|
Output: In this example, the margin-bottom of the first div is 50px and the margin-top of the second div is -25px. So the size of space between them will be 25px (-25px+50px).

Rule 6: No collapsing of margins between Parent and Child elements: A margin is used to increase the space between the sibling elements. It cannot be used for increasing the space between parent and child elements as for that we have padding. Margin only gets transferred from the child element to the parent element as the margin always tries to increase the space between siblings elements and due to this we feel that the margin is applied to the parent element. So there is no margin collapsing between parent and child elements there is only transferring of a margin between them.
For transferring the margin to the parent element the below conditions have to be satisfied:
- Parent and Child should be adjacent.
- The height of the parent element should be auto.
- Parent elements should not have any padding or border.
Rule 7: Collapsing only works on a flow layout: The margin never collapses when the elements are aligned in a flexbox or grid box, or they are not in-flow e.g. absolutely positioned or floated.
Similar Reads
What is the use of CSS ruleset ?
In this article, we will learn about the CSS Ruleset & its implementation. The CSS Ruleset is various affirmations to various pieces or elements of the document. The objective is to apply a bunch of properties for certain distinct qualities to a solitary, or a particular arrangement of component
3 min read
What is the advantage of collapsing white space ?
White space refers to empty or blank values in the code which the browser reads and renders. Html has a special feature of collapsing these white spaces. If you put extra/consecutive white spaces or newlines in the code it will regard it as one white space this is known as collapsing of white spaces
2 min read
What does the CSS rule âclear: bothâ do?
The clear property is used to specify which side of floating elements are not allowed to float. It sets or returns the position of the element about floating the objects. The "clear: both" means floating the elements are not allowed to float on both sides. It is used when no need for any element to
1 min read
CSS Margin Collapse
The CSS margin property is used to set a margin on all four sides of an element. When two elements are next to each other on a page vertically, one of the elements might lose its vertical margin. This means the top and bottom margins of elements are sometimes combined into a single margin. The singl
2 min read
What are the usage of "table-layout" property in CSS ?
In this article, we are going to learn the usages of the table-layout property in CSS, The table-layout property in CSS specifies the layout of the table which consists of table cells, rows, and columns. It can have two values: "auto," which allows the table to adjust its layout based on content, an
3 min read
What is the use of box-sizing property in CSS ?
In this article we will learn about to use of the box-sizing property in CSS, The box-sizing property defines how the width and height of an element should be visible to the user i.e. border and padding are to be included or not. Syntax: box-sizing: content-box|border-box; Property Values: content-b
2 min read
What are CSS columns and how to fill it ?
CSS columns: We can break the content of an element into columns, called CSS columns. We can fill it using the column-fill property. The column-fill property control how the content of the element will break into columns. It takes the values auto, balance, inherit, and initial. Syntax: column-fill:
3 min read
What are the various techniques for clearing floats in CSS ?
In this article, we will learn various techniques for clearing float. Before diving into this topic, we will know the float property in CSS. The float property in CSS is used to change the normal flow of an element. The float property defines where should be an element positioned (right or left) in
5 min read
Which property specifies the top margin of an element in CSS ?
In this article, we are going to discuss the margin-top property that specifies the top margin of an element. In order to specify the top margin of an element, CSS provides a styling element called margin-top which sets the top margin of an element. This property accepts an integer value. The defaul
2 min read
CSS border-collapse Property
The border-collapse property in CSS is used to set the borders of the cell present inside the table and tells whether these cells will share a common border or not. Syntax: border-collapse: separate|collapse|initial|inherit;Default Value: Its default value is separate. Property Values: separate:
3 min read