Margins in Practice
In the following example, look at the two containers and how they 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 elements, horizontally, in their parent elements. This can be accomplished by setting the properties value to auto
, or by setting both margin-left: auto;
and margin-right: auto;
.
- Previous
- Next