How to give margin top in bootstrap 4?

Hi Friends 👋,

Welcome To aGuideHub! ❤️

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

To give margin top, put .mt-1 class to create margin top in bootstrap 4.

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, use in mt-sm-1 class to mack a margin-top.

<div class="container">
  <h1>Margin Top Utilities</h1>
  <p>Responsive margin top utilities. Resize the browser window to see the effect:</p>
  <div class="mt-sm-1 bg-primary">Margin Top 5 on SM screens</div>
  <div class="mt-md-2 bg-warning">Margin Top 5 on MD screens</div>
  <div class="mt-lg-3 bg-success">Margin Top 5 on LG screens</div>
  <div class="mt-xl-5 bg-danger">Margin Top 5 on XL screens</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">
  <h1>Margin Top Utilities</h1>
  <p>Responsive margin top utilities. Resize the browser window to see the effect:</p>
  <div class="mt-sm-1 bg-primary">Margin Top 5 on SM screens</div>
  <div class="mt-md-2 bg-warning">Margin Top 5 on MD screens</div>
  <div class="mt-lg-3 bg-success">Margin Top 5 on LG screens</div>
  <div class="mt-xl-5 bg-danger">Margin Top 5 on XL screens</div>
</div> 
</body>
</html>

Check the output of the above code example.

Bootstrap, Margin, Top

All the best 👍