CSS Margins

CSS Margins: Spacing Outside Elements

CSS margins are used to create space outside an element’s border, helping to separate it from other elements on a webpage. They help in organizing the layout and preventing content from appearing too close together.

Basic Margin Example:

<!DOCTYPE html>
<html>
<head>
    <style>
        .box {
            margin: 20px;
            background-color: lightblue;
            border: 2px solid navy;
            padding: 10px;
        }
    </style>
</head>
<body>
    <div style="border: 1px dashed red;">
        <div class="box">
            I have a 20px margin separating me from the red dashed container!
        </div>
    </div>
</body>
</html>

In this example:

Syntax:

body {
    margin: value;
}

Types of Margin Values

Note: We can also use negative values for margins to pull elements closer together or overlap them.


Margin Properties

CSS margin properties control the space outside an element, with margin serving as a shorthand for all sides.

Margin Property Description Example
margin-top Sets the top margin of an element. margin-top: 20px;
margin-right Sets the right margin of an element. margin-right: 15px;
margin-bottom Specifies 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;
margin Shorthand to set margins on all four sides. margin: 10px 20px;

Margin Shorthand Property

The margin property can take 1 to 4 values to quickly set the spacing for different sides.

1. Margin Property with 4 Values

If the margin property contains four values, it applies them in the order of Top, Right, Bottom, Left (clockwise).

margin: 40px 100px 120px 80px;

Margin with 4 Values:

<!DOCTYPE html>
<html>
<head>
    <style>
        p {
            margin: 40px 100px 120px 80px;
            background-color: yellow;
            border: 1px solid black;
        }
    </style>
</head>
<body>
    <div style="border: 1px dashed black;">
        <p>This paragraph has different margins for all four sides.</p>
    </div>
</body>
</html>

2. Margin Property with 3 Values

If the margin property contains three values, the first value sets the top, the second sets both left and right, and the third sets the bottom margin.

margin: 40px 100px 120px;

Margin with 3 Values:

<!DOCTYPE html>
<html>
<head>
    <style>
        p {
            margin: 40px 100px 120px;
            background-color: lightblue;
            border: 1px solid black;
        }
    </style>
</head>
<body>
    <div style="border: 1px dashed black;">
        <p>Top is 40px, Left/Right are 100px, Bottom is 120px.</p>
    </div>
</body>
</html>

3. Margin Property with 2 Values

If the margin property contains two values, the first value applies to the top and bottom margins, and the second value applies to the right and left margins.

margin: 40px 100px;

Margin with 2 Values:

<!DOCTYPE html>
<html>
<head>
    <style>
        p {
            margin: 40px 100px;
            background-color: lightpink;
            border: 1px solid black;
        }
    </style>
</head>
<body>
    <div style="border: 1px dashed black;">
        <p>Top/Bottom are 40px, Left/Right are 100px.</p>
    </div>
</body>
</html>

4. Margin Property with 1 Value

If the margin property has one value, it applies to all four sides equally.

margin: 40px;

The margin: auto; Property

You can use margin: auto; to horizontally center a block-level element within its container. The browser calculates and splits the remaining horizontal space evenly between the left and right margins.

Margin Auto Example:

<!DOCTYPE html>
<html>
<head>
    <style>
        div.center-box {
            margin: auto;
            width: 50%;
            border: 2px solid navy;
            background-color: lightgray;
            text-align: center;
            padding: 20px;
        }
    </style>
</head>
<body>
    <div class="center-box">
        I am horizontally centered using margin: auto!
    </div>
</body>
</html>

In this example:


The margin: inherit; Property

The inherit value allows an element to copy the margin values of its direct parent.

Margin Inherit Example:

<!DOCTYPE html>
<html>
<head>
    <style>
        .parent {
            margin: 30px;
            border: 2px solid red;
        }
        .child {
            margin: inherit;
            border: 2px solid blue;
            padding: 10px;
        }
    </style>
</head>
<body>
    <div class="parent">
        This is the parent with a 30px margin.
        <div class="child">
            This child inherits the 30px margin from its parent.
        </div>
    </div>
</body>
</html>

In this example:


Exercise

?

If a CSS rule is set to margin: 10px 20px;, what does it apply to?