Margin in CSS:

The margin property in CSS is use to clear area outside the border of a HTML element. We can not set color for margin because it is a transparent property.

We can set margin property for top, bottom, left and right. So we can set margin property in CSS in 5 different types.

  • margin: it will set the margin from all four sides (shorthand property)
  • margin-top: it will only set margin from top
  • margin-right: it will only set margin from right
  • margin-bottom: it will only set margin from bottom
  • margin-left: it will only set margin from left

You can set the value of margin in pixels and in percentage also.

Example:

In following example we use all four types of margin property

 Shorthand Property of Margin:

We can also use the shorthand property for Margin. Means we can set the same value in margin property and also we can set four different values in margin property.

When we set four different values in margin so the first value refers to “top”, second refer to “right”, third refer to “bottom” and last refers to “left”.

In following example we set value same and also set the different values.

Example:

 Note:

You can also set the margin value in “negative” if it needs.

Leave a Reply

Your email address will not be published. Required fields are marked *