css Margins – How Can we use Margin property in css?

The css Margins properties are used to generate space around elements. the margin property size of white space of the border.with CSS you have full control over the margin.There are the css property for setting the margin of each side of an element.

Margin – Individual Sides:-

CSS has the property for specifying the margin for each side of an element.

margin_top

margin-left

margin-right

margin-bottom

All the margin properties can have the following values:-

auto – the browser calculates the margin

length – specifies a margin in px, pt, cm, etc.

% – specifies a margin in % .

inherit – specifies that the margin should be inherited from the parent element

Shorthand Property:-

You can write margin property in one line to short the code, You can write the margin property for the following individual property.

margin-top , margin-right, margin-bottom, margin-left

Example:-p { margin: 100px 150px 100px 80px;}

Auto Value:-

The auto property is used to horizontally center the element within its container.

The element will then take up the specified width, and the remaining space will be split equally between the left and right margins.

div { width: 300px; margin: auto; border: 1px solid red;}

inherit Value:-

This example lets the left margin be inherited from the parent element.

div.Header { border: 1px solid red; margin-left: 100px;}

p.first { margin-left: inherit;}

Margin Collapse:-

Top and bottom margins of elements are sometimes collapsed into a single margin that is equal to the largest of the two margins.This does not happen on left and right margins? Only top and bottom margins?

Look at the following example:-

h1 { margin: 0 0 50px 0;}

h2 { margin: 20px 0 0 0;}

All CSS Margin Properties:-

Property

Description

margin

A shorthand property for setting the margin properties in one declaration