English
German

CSS Margin

CSS Margin

CSS Margins are nearly identical to the CSS Padding attribute except for one important difference: a margin defines the white space around an HTML element border, while padding refers to the white space within the border. Setting the actual value of margin is just the same as with padding, so you can probably zip right through this lesson.

Please see the example below for a visual representation. Note: A border has been added to each element so you that you may see the effects of the margin attribute.

Example:

p {margin: 5px; border: 1px solid black; } 
h5{margin: 0px; border: 1px solid red;}
 

Output

HTML Tutorial learn Html example Html Example

CSS tutorial CSS Example Learn CSS tutorial

Hi Script

There are several ways to go about defining the CSS Margin attribute. We will show you every possible way and let you know which methods are the best.


CSS Margin 1 Value

As you saw in the example above, margin can be uniform outside an element. Specifying one value will create a uniform margin on all sides: top, right, bottom, left. In addition to using exact values, you may also define the margin with the use of percentages.

Example:

p {margin: 2%; border: 1px solid black; }
h5{margin: 0px; border: 1px solid red;}
 

Output

HTML Tutorial learn Html example Html Example

CSS tutorial CSS Example Learn CSS tutorial

Hi Script


CSS Margin margin direction

Each HTML element actually has four different margins: top, right, bottom, and left. It is possible to define these individual margins simply by adding a direction suffix to the margin attribute. Example form: margin-(direction). Defining only one direction will leave the other 3 margins untouched.

Margin Example:

p { margin-left: 5px; border: 1px solid black; }
h5{ margin-top: 0px;
margin-right: 2px;
margin-bottom: 13px;
margin-left: 21px;
border: 1px solid red; }
 


CSS Margin Values

Four margin values can be declared at once by either specifying two or four values. When only using two values, the first will define the margin on the top and bottom, while the second value will define the margin on the left and right.

When using the four value margin specification, the corresponding directions are: top, right, bottom, left. To help you remember what the order is, just remember that it starts at the top and then moves clockwise until it reaches the left. The examples below show partial (2) and complete (4) margin usage.

CSS Margin 4 value Example:

p {margin: 5px 15px;
border: 1px solid black; } 
h5{margin: 0px 5px 10px 3px;
 border: 1px solid red;}
 

Output

HTML Tutorial learn Html example Html Example

CSS tutorial CSS Example Learn CSS tutorial

Hi Script


CSS Tutorial,CSS Margin, CSS Margin example, learn CSS Margin,explain example CSS Margin online free training CSS Tutorial, CSS Tutorial example, learn CSS Margin, online tutorial, download tutorial, CSS Tutorial books, CSS Tutorial videos, live videos CSS Tutorial, learn CSS Tutorial, CSS Tutorial topic CSS Margin, live training CSS Tutorial, download free tutorial