Margins Property

This chapter discusses about the margins in CSS. Margins are used to create space around an element.

They define the amount of space between an element’s border and adjacent elements.

Possible Values

Following values can be passed to each side:

ValueDescription
autoThe margin is calculated automatically by the browser
lengthThe margin is specified in px, pt, cm, etc
%The margin is specified in percentage (%) of the width of the containing element
inheritThe margin is inherited from the parent element

Note: Negative values are allowed to be passed as margin.

Syntax

selector {
    margin: top right bottom left;
  }

You can specify margins for all sides at once (top, right, bottom, left) or set specific values for individual sides.

CSS Margins – Related Properties

You can set the margin for each side individually, which is as follows:

PropertyDescription
margina shorthand property that sets the margin properties in one declaration
margin-topsets the top margin of the element
margin-rightsets the right margin of the element
margin-bottomsets the bottom margin of the element
margin-leftsets the left margin of the element
margin-blocksets logical block start and end margins for an element.
margin-inlinesets logical inline start and end margins of an element.

Comments

Leave a Reply

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