CSS Margin
There are many margin properties in CSS by which you can set space around an element. This space can be specified in terms of units of pixel or centimeters or in terms of persentage. Negative values are also allowed. We will discuss these properties here.Set top margin of an element
By using the margin-top property, the top margin of an element can be set. Example:Output:This paragraph has top margin of 1 cm
This paragraph has top margin of 1 cm
Set right margin of an element
The right margin of an element can be set by using margin-right property.Example:
Output:This is simple paragraph
This paragraph has right margin of 3 cm
This is simple paragraph
This paragraph has right margin of 3 cm
Set bottom margin of an element
margin-bottom property sets the bottom margin of an element.Example:
Output:This paragraph has bottom margin of 2 cm
This paragraph has bottom margin of 2 cm
Set left margin of an element
By using margin-left property, left margin of the element can be set.Example:
Output:This paragraph has left margin of 2 cm
This paragraph has negative left margin
This paragraph has left margin of 2 cm
This paragraph has negative left margin
Set all four margins in a single command
By using margin property all the four margins can be set in a single command. Let's see how..!!Example:
Output:all four margins will be 10px
top and bottom margin will be 10px, left and right margin will be 2% of the total width of the document.
top margin will be 10px, left and right margin will be 2% of the total width of the document, bottom margin will be -10px
top margin will be 10px, right margin will be 2% of the total width of the document, bottom margin will be -10px, left margin will be set by the browser
all four margins will be 10px
top and bottom margin will be 10px, left and right margin will be 2% of the total width of the document.
top margin will be 10px, left and right margin will be 2% of the total width of the document, bottom margin will be -10px
top margin will be 10px, right margin will be 2% of the total width of the document, bottom margin will be -10px, left margin will be set by the browser
