Margin:

Looking back to our box model, we can see that the margin is the space between the box/element and other elements or the edge of the parent element.

The Box Model
Margin
Border
Padding
The Content!

As with padding, margin accepts both absolute and relative size units.

Example 1

In the following example, look at the two containers and how the are positioned based on the margin settings.

Example 2

In the next example, notice how the .child-container-03, has both margin and padding properties set. Also notice, that the parent-container has neither. Note that the browser does not allow the child container’s margin to push the height of the parent container. However, it does still create space between the top of the containing window and both the parent and child elements.

Using Margin to Set Center Alignment

The margin property is also used to center align element, horizontally, in there parent elements. This can be accomplished by setting the properties value to auto, or by setting both margin-left: auto; & margin-right: auto;.

I'm in the middle!

{ TODO: }

  1. Open the example in a separate tab and resize the window to observe the difference.
  2. Read page 314 of Chapter 13 in Duckett.

Previous section:
Next section: