Home » CSS » css Margins – How Can we use Margin property in css?

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.

css margin


Margin – Individual Sides:-

CSS has the property for specifying the margin for each side of an element.
  1. margin_top
  2. margin-left
  3. margin-right
  4. margin-bottom
All the margin properties can have the following values:-
  1. auto – the browser calculates the margin
  2. length – specifies a margin in px, pt, cm, etc.
  3. % – specifies a margin in % .
  4. inherit – specifies that the margin should be inherited from the parent element
Note: Negative values are allowed.

The following example sets margins for all sides of a <p> element:-

    margin-top: 100px;
    margin-bottom: 100px;
    margin-right: 150px;
    margin-left: 80px;

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


    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
margin-bottom Sets the bottom margin of an element
margin-left Sets the left margin of an element
margin-right Sets the right margin of an element
margin-top Sets the top margin of an element

You Can Also Visit: CSS Padding

IF you have any question about this lecture you can comment blow. feel free to contact me.
, , ,

Leave a Reply

Your email address will not be published. Required fields are marked *


error: Sorry You Have Not Permission To Copy!