The CSS reference

"margin" - css property (v. css 1)

  • The 'margin' property is a shorthand property for setting 'margin-top' , 'margin-right' , 'margin-bottom' and 'margin-left' at the same place in the style sheet.
  • If four length values are specified they apply to top, right, bottom and left respectively.
  • If there is only one value, it applies to all sides.
  • If there are two or three values, the missing values are taken from the opposite side.
  • Initial value: see individual properties
  • The property is not inherited
  • HTML usage: All elements except elements with table display types other than table-caption, table and inline-table
  • Browsers that support the margin - property :
    Internet Explorer Mozilla Firefox Opera Google Chrome Apple Safari

How width, height, margin, border and padding properties relates to each other:


Possible margin values:

margin value Comments CSS
length With this you specify a positive(+) or negative(-) value followed with a unit identifier. 1
percentage With this you specify a value with a percentage. It refer to width of containing block. 1
auto The outcome of this property value is much dependant on the related containing block type, replaced or none-replaced elements and the browser. For details, please study computing widths and margins for behavior. 1
inherit Will inherit the value from parent specification. This property value is new with CSS 2. 2

Possible unit identifier:

unit identifier Comments CSS
em The relative unit 'em' is relative to the font size of the element itself. 1
ex The relative unit 'ex' is relative to the font size of the element itself. 1
px Pixels, relative to the viewing device 1
in inches -> 1 inch is equal to 2.54 centimeters. 1
cm centimeters. 1
mm millimeters. 1
pt points -> the points used by CSS2 are equal to 1/72th of an inch. 1
pc picas -> 1 pica is equal to 12 points. 1

Property "margin" example:

<!DOCTYPE HTML >
<html>
  <head>
    <style   type="text/css">
      .div1 { position: relative;
              width: 300px;
              margin: 10px 10px 10px 10px;
              border: #000000 1px solid;
      }
    </style>
  </head>
  <body >
    Text in Body
    <div class="div1">
      Some text in the DIV element with <br/>
      margin: 10px 10px 10px 10px;<br/>
    </div>
  </body>
</html>

Javascript access:

// To SET values ([o] is the target object)
[o].style.margin="margin values"
// You can not retrieve values through this property
// To get values, look at each of the margin properties.
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.