The Wayback Machine - https://web.archive.org/web/20250207112012/https://www.geeksforgeeks.org/css-margins-padding/
Open In App

CSS Margins

Last Updated : 07 Jan, 2025
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Share
Report
News Follow

CSS margins are used to create space around an element, separating it from neighboring elements and the edges of the webpage. They control the layout by adjusting the distance between elements, providing better organization and readability.

Syntax:

body {
margin: value;
}
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->

    <style>
        .box {
            margin: 20px ;
        }
    </style>

<!--Driver Code Starts{-->
</head>
<body>
    <div class="box">
        This box has a margin of 20px vertically.
    </div>
</body>
</html>

<!--Driver Code Ends }-->
  • margin: 20px; applies a 20px margin to all four sides of the element.
CSS Margins layout

CSS Margin

Types of Margin Values

  • Pixels (px): The most common unit, specifies a fixed number of pixels.
  • Percentage (%): The margin is calculated as a percentage of the containing element’s width (for horizontal margins) or height (for vertical margins).
  • Other units: Less common units like em, rem, vh, and vw can also be used for relative sizing.
  • Auto: The browser calculates a suitable margin size, often used for centering elements.

Note: We can also use negative values for margins.

Margin Properties Values

Margin PropertyDescriptionExample
margin-topSets the top margin of an element.margin-top: 20px ;
margin-rightSets the right margin of an element.margin-right: 15px;
margin-bottomSpecifies the margin at the bottom of an element.margin-bottom: 30px;
margin-left:Determines the width of the margin on the left side.margin-left: 10px;
marginShorthand to set margins on all four sides.margin: 10px 20px;

Here are the examples of the margin property with different values:

Example of margin property with 4 values

margin: 40px 100px 120px 80px;
  • top margin = 40px
  • right margin = 100px
  • bottom margin = 120px
  • left margin = 80px
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->

    <style>
        p {
            margin: 40px 100px 120px 80px;
        }
    </style>

<!--Driver Code Starts{-->
</head>
<body>
    <p> Margin properties </p>
</body>

</html>

<!--Driver Code Ends }-->

Example of margin property with 3 values

margin: 40px 100px 120px; 
  • top = 40px
  • right and left = 100px
  • bottom = 120px
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->

    <style>
        p {
            margin: 40px 100px 120px;
        }
    </style>

<!--Driver Code Starts{-->
</head>
<body>
    <p>
        Margin properties
    </p>
</body>
</html>
<!--Driver Code Ends }-->

Example of margin property with 2 values

margin: 40px 100px; 
  • top and bottom = 40px;
  • left and right = 100px;
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->

    <style>
        p {
            margin: 40px 100px;
        }
    </style>

<!--Driver Code Starts{-->
</head>
<body>
    <p>
        Margin properties
    </p>
</body>
</html>
<!--Driver Code Ends }-->

Example of margin property with 1 value

margin: 40px; 
  • top, right, bottom and left = 40px
HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->

    <style>
        p {
            margin: 40px;
        }
    </style>

<!--Driver Code Starts{-->
</head>
<body>
    <p>
        Margin properties
    </p>
</body>
</html>

<!--Driver Code Ends }-->


Example of margin: auto property

margin: auto;
HTML
//Driver Code Starts{
<html>
<head>
//Driver Code Ends }

    <style>
        div {
            margin: auto;
            width: 50%;
            border: 1px solid black;
            text-align: center;
        }
    </style>

//Driver Code Starts{
</head>
<body>
    <div> Centered using margin: auto; </div>
</body>
</html>

//Driver Code Ends }
  • margin: auto;: Automatically adjusts the left and right margins to center the element horizontally.
  • The element must have a defined width for margin: auto; to work effectively.

Example of margin: inherit; Property

HTML
<!--Driver Code Starts{-->
<html>
<head>
<!--Driver Code Ends }-->

    <style>
        .parent {
            margin: 20px;
        }
        .child {
            margin: inherit;
            border: 1px solid black;
        }
    </style>

<!--Driver Code Starts{-->
</head>
<body>
    <div class="parent">
        Parent Element
        <div class="child"> Child inherits margin from parent. </div>
    </div>
</body>
</html>

<!--Driver Code Ends }-->


  • margin: inherit;: The child element inherits the margin value of its parent.
  • In this example, the child element’s margin is set to 20px, matching the parent’s margin.

CSS Margins – FAQs

What are margins in CSS?

Margins in CSS are the spaces outside an element’s border, used to create space between the element and its surrounding elements.

What is margin auto in CSS?

margin: auto; is commonly used to horizontally center block-level elements within their container, when the element has a defined width.

What is the shorthand property for margins?

The shorthand margin property sets all four margins in one declaration. For example, margin: 10px 20px 30px 40px; sets top, right, bottom, and left margins respectively.

How do you set equal margins on all sides?

Use the margin property with a single value. For example, margin: 20px; sets a 20px margin on all sides.

How do you set horizontal and vertical margins?

Use two values in the margin property. For example, margin: 10px 20px; sets a 10px vertical and 20px horizontal margin.



Next Article

Similar Reads

three90RightbarBannerImg