Open In App

CSS Margins

Last Updated : 07 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

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.


Next Article

Similar Reads

  翻译: