CSS margin:collapse;

Please Share On:

Definition:

  • CSS margin: collapse property is used to collapse the top and bottom margin and form a single margin.
  • Left and right margin doesn’t collapse and form a single margin.

Syntax:

margin: 0 0 20px 0;

Source Code:

<!DOCTYPE html>
<html>
  <head>
    <style>
      .paragraph1{
        border: 2px dashed red;
        background-color: orange;
        text-align:center;
        margin: 0 0 30px 0;
      }
      .paragraph2{
        border: 2px dashed red;
        background-color: pink;
        text-align:center;
        margin: 20px 0 0 0;
      }
    </style>
  </head>

  <body>
    <div class="paragraph1">This paragraph has bottom margin 30px; </div>
    <div class="paragraph2">This paragraph has top margin 20px; </div>
    <div class="description">Here, the first paragraph has bottom margin 30px and second paragraph has top margin 20px. The margin between first and second should be 50px. By adding first paragraph 30px and second paragraph 20px (30px + 20px) but due to margin:collapse, the margin end up only with 30px. And, collapse the 20px margin. This is how margin:collapse.</div>
  </body>
</html>

Output:


Donate to support writers


You may be interested in the following topics:

CSS margin:inherit;

Please Share On:

Definition:

CSS margin: inherit value inherit the margin with its parent elements.

Syntax:

margin:inherit;

Source Code:

<!DOCTYPE html>
<html>
  <head>
    <style>
      .title{
        text-align:center;
      }
      .paragraph{
        border: 2px dashed red;
        background-color: orange;
        text-align:center;
        margin: 40px;
      }
      .paragraph-inherit{
        border: 2px dashed red;
        background-color: pink;
        text-align:center;
        margin: inherit;
      }
    </style>
  </head>

  <body>
    <div class="title"> <b>See the different between margin with pixel and inherit.</b> </div>
    <div class="paragraph">This is a paragraph with margin:40px;. </div>
    <div class="paragraph-inherit">This is a paragraph with margin:inherit property value. </div>
  </body>
</html>

Output:


Donate to support writers


You may be interested in the following topics:

CSS Margin: auto;

Please Share On:

Definition:

CSS margin: auto value applies to set the margin property automatically to the horizontal center within the elements.

Syntax:

margin:auto;

Source Code:

<!DOCTYPE html>
<html>
  <head>
    <style>
      .paragraph{
        width: 200px;
        border: 2px dashed red;
        background-color: pink;
        text-align:center;
        margin: auto;
      }
    </style>
  </head>

  <body>
    <div class="paragraph">Margin property value is auto. </div>
  </body>
</html>

Output:


Donate to support writers


You may be interested in the following topics:

CSS margin-left

Please Share On:

Definition:

CSS margin-left applies space outside the defined left border.

Syntax:

margin-left: 10px;

Source Code:

<!DOCTYPE html>
<html>
  <head>
    <style>
      .paragraph{
        border: 2px dashed red;
        background-color: pink;
        text-align:center;
        margin-left: 10px;
      }
    </style>
  </head>

  <body>
    <div class="paragraph">Left Margin is 10px; </div>
  </body>
</html>

Output:


Donate to support writers


You may be interested in the following topics:

CSS margin-bottom

Please Share On:

Definition:

CSS margin-bottom applies space outside the defined bottom border.

Syntax:

margin-bottom: 10px;

Source Code:

<!DOCTYPE html>
<html>
  <head>
    <style>
    .paragraph{
      border: 2px dashed red;
      background-color: pink;
      text-align:center;
      margin-bottom: 10px;
    }
    </style>
  </head>

  <body>
 	 <div class="paragraph">Bottom Margin is 10px; </div>
  </body>
</html>
					
					

Output:

Bottom Margin is 10px;


Donate to support writers


You may be interested in the following topics:

CSS margin-right

Please Share On:

Definition:

CSS margin-right applies space outside the defined right border.

Syntax:

margin-right: 10px;

Source Code:

<!DOCTYPE html>
<html>
  <head>
    <style>
    .paragraph{
      border: 2px dashed red;
      background-color: pink;
      text-align:center;
      margin-right: 10px;
    }
    </style>
  </head>

  <body>
  	<div class="paragraph">Right Margin is 10px; </div>
  </body>
</html>

Output:


Donate to support writers


You may be interested in the following topics:

CSS Margin-top

Please Share On:

Definition:

CSS margin-top applies space outside the defined top border.

Syntax:

margin-top: 10px;

Source Code:

<!DOCTYPE html>
<html>
  <head>
    <style>
    .paragraph{
      border: 2px dashed red;
      background-color: pink;
      text-align:center;
      margin-top: 10px;
    }
    </style>
  </head>

  <body>
  	<div class="paragraph">Top Margin is 10px; </div>
  </body>
</html>

Output:


Donate to support writers


You may be interested in the following topics:

CSS Margin

Please Share On:

Definition:

CSS Margin property is used to put spaces outside defined borders. “Margin” property is the shorthand property of all four individual margin properties. i.e,

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

Click Here to see the margin in box-model

Syntax:

margin: 10px;

Margin Property Values

Margin property values can have 1, 2 , 3 and 4 values.

  1. One Margin Property Value

If you have one margin property value, then the margin applies to all four sides.

Syntax:

.paragraph
{
margin: 10px;
}

Source Code:

<!DOCTYPE html>
<html>
  <head>
    <style>
    .paragraph{
      border: 2px dashed red;
      background-color: pink;
      text-align:center;
      margin: 10px;
    }
  	</style>
  </head>

  <body>
  	<div class="paragraph">This element has 10px equal margin in four sides.</div>
  </body>
</html>

Output:


2. Two Margin Property Value

If you have two margin property values, the first value applies top and bottom margins, and the second value applies left and right margins.

Syntax:

.paragraph
{
margin: 10px 20px;
}

Source Code:

<!DOCTYPE html>
<html>
  <head>
    <style>
    .paragraph{
      border: 2px dashed red;
      background-color: pink;
      text-align:center;
      margin: 10px 20px;
    }
    </style>
  </head>

  <body>
 	 <div class="paragraph">Top and Bottom margins are 10px <br/> Left and Right margins are 20px.</div>
  </body>
</html>

Output:



3. Three Margin Property Value

If you have three margin property values, the first value applies top, the second value applies left and right margins and the third value applies to the bottom.

Syntax:

.paragraph
{
margin: 10px 20px 5px;
}

Source Code:

<!DOCTYPE html>
<html>
  <head>
    <style>
    .paragraph{
      border: 2px dashed red;
      background-color: pink;
      text-align:center;
      margin: 10px 20px 5px;
    }
    </style>
  </head>

  <body>
  	<div class="paragraph">Top Margin is 10px; <br/> Left and Right margins are 20px;<br/> Bottom margin is 5px;</div>
  </body>
</html>
					
					

Output:


4. Four Margin Property Value

If you have four margin property values, then the first value applies to the top, the second value applies to the right, the third value applies to the bottom and the fourth value applies to the left.

Syntax:

.paragraph
{
margin: 10px 20px 15px 10px;
}

Source Code:

<!DOCTYPE html>
<html>
  <head>
    <style>
    .paragraph{
      border: 2px dashed red;
      background-color: pink;
      text-align:center;
      margin: 10px 20px 15px 10px;
    }
    </style>
  </head>

  <body>
  	<div class="paragraph">Top Margin is 10px; <br/> Right margin is 20px;<br/> Bottom margin is 15px; <br/> Left margin is 10px;</div>
  </body>
</html>

Output:


Donate to support writers


You may be interested in the following topics:

CSS Margin (All Properties)

Please Share On:

Definition:

CSS Margin property is used to put a space outside defined borders. You can put margins individually into each side of the elements by using the following syntax.

Margin Properties:

Margin PropertiesDescription
marginApplies margin to all four sides.
margin-topApplies margin to element’s top.
margin-rightApplies margin to element’s right.
margin-bottomApplies margin to element’s bottom.
margin-leftApplies margin to element’s left.
margin:autoApplies margin to auto to horizontally center.
margin: inheritApplies margin inherited to its parent elements.
margin: collapsecollapse margin.

Donate to support writers


You may be interested in the following topics:

Copyright @2023. All Right Reserved.


Social media & sharing icons powered by UltimatelySocial