XML technologies / CSS / Advanced websites with CSS / CSS property reference / margin

(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)

The shorthand property margin describes the size of an element’s outer margins.

The properties margin-bottom, margin-left, margin-right and margin-top let you declare margins separately for each side.

Inherited: no

See also: margin-bottom, margin-left, margin-right, margin-top

Values

One to four values for the sides of an element, separated by spaces. The following table describes how values are assigned.

Valid values are CSS measurements, percentages of the parent element’s width or height, or the constant auto. With auto, the browser calculates margins so that all elements can occupy their default or assigned width.

Table: Value combinations for margin and their effects.

Number of values

Effect

1

The value applies to all four margins.

2

The first value applies to the horizontal top and bottom margins; the second to the vertical left and right margins.

3

The first value applies to the top margin, the second to the left and right margins, and the third to the bottom margin.

4

The first value applies to the top margin, the second to the right margin, the third to the bottom margin, and the fourth to the left margin.

Default value: 0

Compatibility

CSS version: 1

Works in all CSS-capable browsers, including Internet Explorer 4 and Netscape 4.

Example

The following style rule displays elements blockquote at 80% of their parent’s width, with 10-pixel top and bottom margins. The browser automatically calculates equal left and right padding to center the element horizontally:

blockquote {
 width: 80%;
 margin: 10px auto;
}

  

 

 

 

back to top