How to give negative margin in bootstrap 4?

Hi Friends 👋,

Welcome To aGuideHub! ❤️

In this tutorial, we will learn how to give negative margin in bootstrap 4.

To give negative margin, put .m-n3 class to create negative margin in bootstrap 4.

Negative margins are usually used to stretch elements to fill or overflow their containers. Let us discuss a use case for negative margins. Take the following markdown that has three div that is centered and of width 300px.

Each of these div has a padding of 1rem and has a child div inside them.

Table of contents

  • Includes bootstrap view
  • Includes bootstrap library
  • Define its class name

Step 1: Includes bootstrap view

To ensure proper rendering and touch zooming for all devices, add the responsive viewport meta tag to your <head>.

<meta name="viewport" content="width=device-width, initial-scale=1">

Step 2: Includes bootstrap library

First of all, load the Bootstrap framework CSS into the head tag of your webpage.

<!-- Bootstrap CSS -->
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</head>

Step 3: Define its class name

After that, using the .mx-n4 class to create a negative margin.

<div class="container">
    <div class="mx-auto p-3 border" style="width: 300px">
        <div class=" p-2 bg-primary ">
            Div with no margin
        </div>
    </div>
    <div class="mx-auto p-3 border" style="width: 300px">
        <div class="mx-n4 p-2 bg-danger ">
            Div with negative margin overflow
        </div>
    </div>
    <div class="mx-auto p-3 border" style="width: 300px">
        <div class="m-n3 p-2 bg-warning ">
            Div with negative margin stretched to fit
        </div>
    </div>
</div>

Example.

Let’s look at the following example to understand how it basically works:

<!DOCTYPE html>
<html lang="en">

<head>
    <title>Bootstrap Example</title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css">
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/jquery.slim.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/umd/popper.min.js"></script>
    <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/js/bootstrap.bundle.min.js"></script>
</head>

<body>

<div class="container">
    <div class="mx-auto p-3 border" style="width: 300px">
        <div class=" p-2 bg-primary ">
            Div with no margin
        </div>
    </div>
    <div class="mx-auto p-3 border" style="width: 300px">
        <div class="mx-n4 p-2 bg-danger ">
            Div with negative margin overflow
        </div>
    </div>
    <div class="mx-auto p-3 border" style="width: 300px">
        <div class="m-n3 p-2 bg-warning ">
            Div with negative margin stretched to fit
        </div>
    </div>
</div>

</body>

</html>

Check the output of the above code example.

Bootstrap, Margin, Negative

All the best 👍

Premium Content

You can get all the below premium content directly in your mail when you subscribe us

Books

Interview Questions

Soon You will get CSS, JavaScript, React Js, and TypeScript So Subscribe to it.

Portfolio Template

View | Get Source Code

Cheat Sheets

Cheat Sheets Books are basically Important useful notes which we use in our day-to-day life.

Related Posts