Margins in Practice

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

Margin and Padding Combined

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;.