This page is Ready to Use

Notice: The WebPlatform project has been discontinued when the Stewards partnership agreement ended in September 2015. This site has been frozen, and the assets we have created will remain.

margin-bottom

Summary

margin-bottom sets the bottom margin of an element.

Overview table

Initial value
Depends on the particular element. Different elements have different default margins.
Applies to
All elements except elements with table display types other than table-caption, table, and inline-table.
Inherited
No
Media
visual
Computed value
As specified, but with relative lengths converted into absolute pixel values.
Animatable
Yes
CSS Object Model Property
marginBottom

Syntax

  • margin-bottom: auto
  • margin-bottom: inherit
  • margin-bottom: length
  • margin-bottom: percentage

Values

length
Specifies a fixed length, using any standard CSS length units . Negative Values are allowed.
percentage
A percentage of the width of the containing block. Negative values are allowed. (Even though this is margin-bottom, the browser will take the percentage from the width, not the height of the containing block.)
auto
The browser calculates a bottom margin dependent on the space available
inherit
Inherits the parent element’s specified margin-bottom width.

Examples

In this example there are three blocks, styled identically except for their margin-bottom values:

  • The first one has a margin-bottom of 2 centimeters, meaning that everything else is pushed down by 2cm, leaving a gap between the top block and the rest of the content.
  • The second one has a margin-bottom of -1em, meaning that the bottom block is actually pulled upwards so it overlaps the second block slightly.
  • The bottom block has no margin-bottom of its own.
<div class="one"></div>
<div class="two"></div>
<div class="three"></div>

View live example

CSS applied to the HTML shown in the first example.

/**
 * margin-bottom examples
 */

 * {
   margin: 0;
 }

div {
  width: 200px;
  height: 100px;
  background: linear-gradient(rgba(0,0,0,0.25), rgba(0,0,0,0));
  border-radius: 10px;
}

.one {
  background-color: red;
  margin-bottom: 2cm;
}

.two {
  background-color: blue;
  margin-bottom: -1em;
}

.three {
  background-color: green;
  margin-bottom: 0px;
}

View live example

Usage

 ===Usage===
  • When calculating the height and width of an element, DO NOT include the margins in your calculations (i.e. include everything else: content area, padding, and border). However, DO include margin size when calculating available space within an element’s containing element.
  • When two margins collide, for example when one block level element has a bottom margin set, immediately followed by another block level element with a top margin, the larger of the two margins remains, and the smaller one collapses and disappears.
  • Margins are always transparent.

Best Practices

  • When possible, use margin shorthand (i.e. {margin: 10px 15px 20px 15px;}) to specify margin-widths rather than writing out each margin’s specifications as this clutters code and makes it difficult to read. Use margin-bottom if there is a specific reason to call attention to it (e.g. one element has a different bottom margin than the rest in its class, etc.).

Notes

As of Microsoft Internet Explorer 4.0 or later, you can specify possible 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 value of this property is used to compute the border area of a surrounding inline element, if present. This value does not contribute to the height of a line. Negative margins are supported, except for top and bottom margins on inline objects.

Related specifications

CSS 2
W3C Recommendation

See also

Related articles

Box Model

Related pages

Attributions