Margins in Practice

In the following example, look at the two containers and their position 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 does not have either. Note that the browser does not allow the child container’s margin to push the parent container’s height. However, it still creates space between the top of the containing window and the parent and child elements.

Using Margin to Set Center Alignment

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