NOTICE: This website is no longer updated or supported - as such many of the techniques used to build it may seem antiquated in the modern day. It is preserved for historical reasons only.

HTML XHTML The Complete Reference
home » reference » appendix b » css1 / css2 properties

margin


The margin property sets a consistent margin on all four sides of the affected element. Margin values can be set to a specific length (15pt, 2em, and so on) or to a percentage value of the block element's width. Margins can be set to negative values but content may be clipped. The value auto attempts to calculate the margin automatically. However, this value is buggy in many CSS implementations, particularly older browser versions, and might be avoided for compatibility reasons.

As a shorthand form, it is possible to set the four margins (margin-top, margin-bottom, margin-right, and margin-left) independently with this property. A single value will be copied to all four margins. With two values, the first value will specify top and bottom margins and the second value will specify the top right and left margins. If three values are specified, the first defines the top margin, the second defines the left and right margins, and the third defines the bottom margin. Note that the unspecified margin is inferred from the value defined for its opposite side. Lastly, all four values can also be set in order of top, right, bottom, and left.

Examples


p {margin: 15pt;} /* all sides 15pt */
#div1 {margin: 20px 10px;} /* 20px top-bottom, 10px left-right */
#div2 {margin: 10px 10px 5px 10px;}
/* 10px top, 10px right 5px bottom, 10px left */

Browser and CSS Support Notes


CSS1
IE 4 (buggy), 5, 5.5, 6
Nav 4,4.5-4.8 (buggy), 6, 7
Opera 4, 5, 6, 7


margin-bottom


This property sets an element's bottom margin and can be any normal measurement value as well as a negative value.

Example


p {margin-bottom: 10pt;}

Browser and CSS Support Notes


CSS1
IE 4 (buggy), 5, 5.5, 6
Nav 4, 4.5-4.8 (buggy), 6, 7
Opera 4, 5, 6, 7


margin-left


This property sets an element's left margin and can be any normal measurement value as well as a negative value.

Examples


p {margin-right: 15pt;}
.off {margin-left: -10px;}

Browser and CSS Support Notes


CSS1
IE 4 (buggy), 5, 5.5, 6
Nav 4, 4.5-4.8 (buggy), 6, 7
Opera 4, 5, 6, 7


margin-right


This property sets an element's right margin and can be any normal measurement value as well as a negative value.

Example


p {margin-right: 15pt;}

Browser and CSS Support Notes


CSS1
IE 4 (buggy), 5, 5.5, 6
Nav 4, 4.5-4.8 (buggy), 6, 7
Opera 4, 5, 6, 7


margin-top


This property sets an element's top margin and can be any normal measurement value as well as a negative value.

Example


p {margin-top: 15pt;}

Browser and CSS Support Notes


CSS1
IE 4 (buggy), 5, 5.5, 6
Nav 4, 4.5-4.8 (buggy), 6, 7
Opera 4, 5, 6, 7

(X)HTML Elements
CSS Properties
Back: list properties Next: marker-offset
< Home | About | Chapters | Examples | Errata | Reference | Site Map >