Fandom

HTML & CSS Wiki

Margin

648pages on
this wiki
Add New Page
Comments0 Share

The margin property is used in CSS and certain HTML elements. It sets the margin for all four sides of the object. It is a shorthand to avoid setting each side separately with the other margin properties: margin-top, margin-right, margin-bottom and margin-left.


ValuesEdit

  • <length> - Specifies a fixed width. Negative values are allowed. Ex. pt, px, etc.
  • <percentage> - A <percentage> relative to the width of the containing block. Negative values are allowed.
  • auto - auto is replaced by some suitable value, e.g. it can be used for the centering of blocks.
div { width:50%;  margin:0 auto; }

centers the div container horizontally.

  • One single value applies to all four sides.
  • Two values apply first to top and bottom, the second one to left and right.
  • Three values apply first to top, second to left and right and third to bottom.
  • Four values apply to top, right, bottom and left in that order.


HTML example:

<h1 style="margin:5px 10px 5px 10px;">
1st heading content.
</h1>


CSS example:

h1 {
    margin:5px 10px 5px 10px;
}

Ad blocker interference detected!


Wikia is a free-to-use site that makes money from advertising. We have a modified experience for viewers using ad blockers

Wikia is not accessible if you’ve made further modifications. Remove the custom ad blocker rule(s) and the page will load as expected.