Skip to content
Related Articles

Related Articles

Primer CSS Margin

View Discussion
Improve Article
Save Article
  • Last Updated : 10 Jun, 2022

Primer CSS is a free and open-source CSS framework that is built using the GitHub design system for providing support to the broad spectrum of GitHub websites. It helps in creating the foundation of the basic style elements such as spacing, components, typography, color, etc.

In this article, we’ll see about Primer CSS Margin. A Margin allows the users to reduce the amount of custom CSS that shares the same properties and allows them to achieve many different page layouts using the same styles. We can set different margins for different sides i.e, for top, bottom, right, and left.

Primer CSS Margins:

  • Naming Convention: As margin has various variations, so to remember all the different naming variations, this utility help the users to remember the margin names. For example: m(margin), t(top), x(horizontal, left & right), 4(24px), etc.
  • Uniform margins: This utility is used to apply equal margins to all sides of an element.
  • Directional margins: This utility is used to apply margin to an individual side, or the X and Y axis of an element.
  • Extended vertical margins: This utility is used to provide extended vertical margins to the top and bottom of an item.
  • Center elements: This utility is used to center block elements with a set width.
  • Reset margins: This utility is used to set the margins on any element to zero.
  • Responsive margins: This utility is used to provide margins in a particular direction and breakpoint.
  • Negative margins: This utility is used to add negative margins to the top, right, bottom, or left of an item.
  • Extended negative margins: This utility is used to provide extended negative margins to the top and bottom of an item.

Syntax:

<div class="mt-3">
    Margin top of 3
</div>

Example 1: Below example demonstrates the uniform margins.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport"
        content="width=device-width, initial-scale=1.0" />
    <title>Primer</title>
    <link href=
"https://unpkg.com/@primer/css@^19.0.0/dist/primer.css"
        rel="stylesheet"/>
</head>
<body>
    <div class="m-4">
        <h1 style="color: green">GeeksforGeeks</h1>
        <h2>Primer CSS Margin</h2>
    </div>
    <div class="d-flex flex-items-baseline flex-justify-around">
        <div class="color-bg-success">
            <div class="m-0 p-1 color-bg-success-emphasis">
                .m-0
            </div>
        </div>
        <div class="color-bg-success">
            <div class="m-1 p-1 color-bg-success-emphasis">
                .m-1
            </div>
        </div>
        <div class="color-bg-success">
            <div class="m-2 p-1 color-bg-success-emphasis">
                .m-2
            </div>
        </div>
        <div class="color-bg-success">
            <div class="m-3 p-1 color-bg-success-emphasis">
                .m-3
            </div>
        </div>
        <div class="color-bg-success">
            <div class="m-4 p-1 color-bg-success-emphasis">
                .m-4
            </div>
        </div>
        <div class="color-bg-success">
            <div class="m-5 p-1 color-bg-success-emphasis">
                .m-5
            </div>
        </div>
        <div class="color-bg-success">
            <div class="m-6 p-1 color-bg-success-emphasis">
                .m-6
            </div>
        </div>
    </div>
</body>
</html>


Output:

 

Example 2: Below example demonstrates the directional margins.

HTML




<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content=
        "width=device-width, initial-scale=1.0" />
    <link href=
"https://unpkg.com/@primer/css@^19.0.0/dist/primer.css"
        rel="stylesheet" />
</head>
<body>
    <div class="m-4">
        <h1 style="color: green">GeeksforGeeks</h1>
        <h2>Primer CSS Margin</h2>
    </div>
    <div class="d-flex flex-items-baseline flex-justify-around">
        <div class="color-bg-success">
            <div class="mx-4 p-1 color-bg-success-emphasis">
                .mx-4
            </div>
        </div>
        <div class="color-bg-success">
            <div class="my-4 p-1 color-bg-success-emphasis">
                .my-4
            </div>
        </div>
        <div class="color-bg-success">
            <div class="mt-4 p-1 color-bg-success-emphasis">
                .mt-4
            </div>
        </div>
        <div class="color-bg-success">
            <div class="mb-4 p-1 color-bg-success-emphasis">
                .mb-4
            </div>
        </div>
        <div class="color-bg-success">
            <div class="ml-4 p-1 color-bg-success-emphasis">
                .ml-4
            </div>
        </div>
        <div class="color-bg-success">
            <div class="mr-4 p-1 color-bg-success-emphasis">
                .mr-4
            </div>
        </div>
    </div>
</body>
</html>


Output:

 

Reference: https://primer.style/css/utilities/margin


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!