Margin and Padding

Introduction:
This is the fourth part in my CSS Styling tutorials, in which I will be covering Margins and Padding.

Difference?
Both the margin and padding properties give white space around an object, the main difference is the order of which they occur in. At the same time, margin is mostly used for positioning while padding is used for giving white space around an object.

Order:
As a good example of which order the properties go in, please see that attached image which is a screen shot of the Inspect Element feature within Google Chrome. This handy, interactive image highlights the different properties around the selected element right on the page so you can see exactly where each white space is. It also shows the order the properties go in (from furthest away to the closest to the element);
Margin
Border
Padding
Element

In the screenshot, I am hovering my mouse over the padding element which is highlighting it on the page as green. The order of the properties is important because it allows more customisation for your elements (for example, in the image there is padding to make the border separate from the text, if margin was used, the border would be right up against the text and there would be white space around the outside of the border).

Setting Margins and Paddings:
A custom margin and padding can be set for each element, this is done by defining the property followed by the value, just like any other CSS property.

Setting a 5px (pixel) margin all around the element...

margin:5px5pxauto;

The above margin property sets the margin in the order of Top, Right, Bottom, Left. If two arguments are given (top and right) followed by auto, the bottom will be the same as the top, and the left will be the same as the right. Otherwise, if one property is given (top) followed by auto, the bottom will be the same as the top, and the left and right will be equal to the full width of the container where the element is stored (in other words, the element will horizontally center).

Setting a 5px (pixel) margin all around the element, separately (any can be removed to give sides different values)...

margin-top:5px;

margin-bottom:5px;

margin-left:5px;

margin-right:5px;

Setting a 5px (pixel) padding all around the element...

padding:5px;

Setting a 5px (pixel) padding all around the element, separately (any can be removed to give sides different values)...

padding-top:5px;

padding-bottom:5px;

padding-left:5px;

padding-right:5px;

When are they used?
Margins and paddings can be used to position any HTML element or object. They can be used despite the position value of the element (absolute, relative, etc.).

Linking CSS To a HTML Element:
To link your CSS to an HTML element (text, div, etc.) you will need to decide whether you want to use a class or id, you will also need a unique name. Once you have those, go to your element in HTML and add...

class='myClass'

... to the attributes of that element if you chose a class, or...

id='myID'

if you chose an id.

Make sure you replace 'myClass' and/or 'myID' with your unique name. Then, in the CSS you will want to encase your properties with...

.myClass{

//Properties go here

}

if you chose a class, or...

#myID{

//Properties go here

}

(You can remove the line beginning with // if you wish).

Here's an example...

<html>

<head>

<style>

.margincenter{

color:blue;

width:300px;

height:100px;

margin:0pxauto;

//Margin centered.

}

.padded{

color:#1a1a1a;

width:300px;

height:100px;

padding:5px;

//Padded by 5px

}

.marginpadded{

color:#000;

width:300px;

height:100px;

margin-top:10px;

margin-left:6px;

margin-bottom:1px;

margin-right:20px;

padding:2px5px10px20px;

//Margined and padded.

}

</style>

</head>

<body>

<div class='margincenter'>This div is centered through the use of margins.</div>