Export (0) Print
Expand All

margin property

Sets or retrieves the width of the top, right, bottom, and left margins of the object.

CSS 2.1, Section 5.5.5Internet Explorer 9

 

Syntax

margin: top | right | bottom | left

Property values

A variable of type String that specifies or receives up to four of the following space-delimited values:

top

Any of the range of margin width values available to the margin-top property.

right

Any of the range of margin width values available to the margin-right property.

bottom

Any of the range of margin width values available to the margin-bottom property.

left

Any of the range of margin width values available to the margin-left property.

CSS information

Applies ToAll elements
Mediavisual
Inheritedno
Initial Value(see individual properties)

Standards information

Remarks

This is a composite property that specifies up to four width values, in the following order: top, right, bottom, left. If one width value is specified, it is used for all four sides. If two width values are specified, the first is used for the top and bottom borders, and the second is used for left and right borders. If three width values are specified, they are used for the top, right/left, and bottom borders, respectively. Negative margins are supported except for top and bottom margins on inline objects.

As of Microsoft Internet Explorer 4.0, you can specify length values relative to the height of the element's font (em) or the height of the letter "x" (ex).

In Microsoft Internet Explorer 3.0, the specified margin value is added to the default value of the object. In Internet Explorer 4.0 or later, the margin value is absolute. The margin properties do not work with the td and tr objects in Internet Explorer 4.0, but they do work in Internet Explorer 3.0. To set margins in the cell for Internet Explorer 4.0 or later, apply the margin to an object, such as div or p, within the td.

As of Microsoft Internet Explorer 5.5, this property applies to inline elements. With earlier versions of Windows Internet Explorer, inline elements must have an absolute position or layout to use this property. Element layout is set by providing a value for the height property or the width property.

For inline elements, the top and bottom values are used to compute the border area of a surrounding inline element, if present. These values do not contribute to the height of a line.

Margins are always transparent.

The below image shows the content, padding, border, and margin areas of a basic box. The red labels illustrate the edges of each of these areas.

An illustration of the basic box model

Examples

In the following example, three div elements specify height and width values. The second div element uses the margin property to set margin-top to 5px, margin-right to 0px, margin-bottom to 15px, and margin-left to -10px.


<div id="blue"></div>
<div id="yellow"></div>
<div id="green"></div>


#blue {
   background-color: #00A4EF;
   height: 50px;
   width: 50px;
}
#yellow {
   background-color: #FFB900;
   height: 50px;
   width: 50px;
   margin: 5px 0px 15px -10px;
}
#green {
   background-color: #7FBA00;
   height: 50px;
   width: 50px;
}

The following image shows the result.

Three divs demonstrating the margin property

The following example uses inline scripting to set the margin of the image to 5 millimeters when an onmouseover event occurs.

Code example: http://samples.msdn.microsoft.com/workshop/samples/author/dhtml/refs/margin_s.htm


<img src="sphere.jpg" onmouseover="this.style.margin='5mm'">

See also

CSSStyleDeclaration
currentStyle
defaults
runtimeStyle
style
Conceptual
CSS Values and Units Reference
Other Resources
CSS Enhancements in Internet Explorer 6

 

 

Show:
© 2014 Microsoft