CSS margin-bottom Property

CSS margin-bottom property sets the bottom side margin to HTML Element(s).

The syntax to specify a value for margin-bottom property is

margin-bottom: value;

The following table gives the possible values that could be given to margin-bottom property.

ValueDescriptionExamples
lengthAny valid CSS length unit. Refer CSS Length Units tutorial.margin-bottom: 10px;
margin-bottom: 5pt;
margin-bottom: 2cm;
Percentage ValueBottom margin in percent of the width of the containing element.margin-bottom: 20%;
initialSets margin-bottom to default value.margin-bottom: initial;
inheritInherits margin-bottom value from its parent element.margin-bottom: inherit;

Examples

In the following examples, we set bottom margin to HTML Element(s) with a few allowed values for margin-bottom CSS property.

Simple Example

The following is a simple example, where we set margin-bottom for paragraph with 50px.

index.html

</>
Copy
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <style>
        #p1 {
            margin-bottom: 50px;
        }
    </style>
</head>
<body>
    <p id="p1">A paragraph with 50px bottom margin.</p>
</body>
</html>

Different Values for margin-bottom

The following is a simple example, where we set different margin-bottom values for paragraphs.

index.html

</>
Copy
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <style>
        p{
            margin: 0;
        }
        #p1 {
            margin-bottom: 50px;
        }
        #p2 {
            margin-bottom: 0px;
        }
        #p3 {
            margin-bottom: 20px;
        }
        #p4 {
            margin-bottom: 10px;
        }
    </style>
</head>
<body>
    <p id="p1">A paragraph with 50px bottom margin.</p>
    <p id="p2">A paragraph with 0px bottom margin.</p>
    <p id="p3">A paragraph with 20px bottom margin.</p>
    <p id="p4">A paragraph with 10px bottom margin.</p>
</body>
</html>

margin-bottom property with CSS Length Units

In the following example, we apply margin-bottom property to paragraphs with a few different CSS Length Unit values.

index.html

</>
Copy
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <style>
        p{
            margin: 0;
        }
        #p1 {
            margin-bottom: 50px;
        }
        #p2 {
            margin-bottom: 1in;
        }
        #p3 {
            margin-bottom: 10pt;
        }
    </style>
</head>
<body>
    <p id="p1">A paragraph with 50px bottom margin.</p>
    <p id="p2">A paragraph with 1in bottom margin.</p>
    <p id="p3">A paragraph with 10pt bottom margin.</p>
</body>
</html>

margin-bottom property with Percentage Values

In the following example, we apply margin-bottom property to paragraphs with a few different percentage values.

If margin-bottom is specified in percentage values, and if width of the HTML element changes, then the margin also changes. For example, try to resize the browser window, and observe the bottom margin for these HTML Elements.

index.html

</>
Copy
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <style>
        p{
            margin: 0;
        }
        #p1 {
            margin-bottom: 5%;
        }
        #p2 {
            margin-bottom: 10%;
        }
        #p3 {
            margin-bottom: 20%;
        }
    </style>
</head>
<body>
    <p id="p1">A paragraph with 5% bottom margin.</p>
    <p id="p2">A paragraph with 10% bottom margin.</p>
    <p id="p3">A paragraph with 20% bottom margin.</p>
</body>
</html>

margin-bottom property for a few HTML elements

In the following example, we take a few HTML elements like h1, p, a, and pre, and set margin-bottom for these.

index.html

</>
Copy
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <style>
        h1, p, a, pre {
            margin: 0;
            display: block;
        }
        h1 {
            margin-bottom: 50px;
        }
        p {
            margin-bottom: 50px;
        }
        a {
            margin-bottom: 50px;
        }
        pre {
            margin-bottom: 50px;
        }
    </style>
</head>
<body>
    <h1>Heading 1</h1>
    <p>A paragraph.</p>
    <a href="#">Anchor Text</a>
    <pre>pre
hello
world</pre>
</body>
</html>

Conclusion

In this CSS Tutorial, we learned about margin-bottom property, and how to use this property for HTML Elements, with examples.