Latest web development tutorials

CSS Margin (Margin)

CSS Margin (margin) property defines the space around elements.


Margin

clear margin around elements (outside the borders) area. margin no background color is completely transparent

margin can be changed on individual elements, bottom, left and right margins. You can also change once all the properties.

Possible values

value Explanation
auto Set your browser margins.
The result will depend on the browser
length Define a fixed margin (using the pixel, pt, em, etc.)
% Defined using a percentage margin

Remark Margin can use negative overlapping content.


<H2Margin - unilateral margin property

In CSS, it can specify a different side of the different margins:

Examples

margin-top:100px;
margin-bottom:100px;
margin-right:50px;
margin-left:50px;

try it"


Margin - shorthand property

To shorten the code, it is possible to use all the margin properties in one property specified margin. This is called a shorthand property.

Shorthand property for all the margin properties is "margin":

Examples

margin:100px 50px;

try it"

margin property can have one to four values. / P>

  • margin: 25px 50px 75px 100px;
    • On the margins of 25px
    • Right margin to 50px
    • The bottom margin is 75px
    • Left margin of 100px

  • margin: 25px 50px 75px;
    • On the margins of 25px
    • Left and right margins to 50px
    • The bottom margin is 75px

  • margin: 25px 50px;
    • Top and bottom margins to 25px
    • Left and right margins to 50px

  • margin: 25px;
    • All four margins are 25px

Examples

More examples

Margin settings on the text using a cm value
This example demonstrates how to set up a text value centimeters of the top margin.

Set the text using the percentage value of the bottom margin
This example demonstrates how to set up using a percentage of the value of the bottom margin with respect to the width of the containing element.


All CSS margin properties

Attributes description
margin Shorthand property. Set all margin properties in one declaration.
margin-bottom Setting outside of an element.
margin-left The element in the left margin.
margin-right The element of the right margin.
margin-top Element is set on the margin.