Open In App

Spacing in Bootstrap with Examples

Bootstrap has many facility of classes to easily style elements in HTML. It includes various responsive padding and margin classes for modification of the appearance of element. Spacing utilities have no breakpoints symbols to apply to the breakpoints.
Following Syntax are used in the Various Classes for adding spacing: 

Property: There are two ways of adding spacing to the elements.



Sides: This allows users to add spacing in content to a specific side wherever required.

Size: This allows users to add a specific amount of spacing to a level. 



Breakpoint: Breakpoints are points where the website content can adjust according to the device and allow to show the best layout to the user.  

Syntax: 

<div class="mt-4">
<div class="mt-md-4">
<div class="mt-lg-4">
<div class="mt-xl-4">

Responsive Spacing Table: 

Screen Size Class
Appears on all .ml-3
Appears only on xs .ml-3 .ml-sm-0
Appears only on sm .ml-sm-3 .ml-md-0
Appears only on md .ml-md-3 .ml-lg-0
Appears only on lg .ml-lg-3 .d-xl-0
Appears only on xl .ml-xl-3

Example 1: 




<!DOCTYPE html>
<html>
    <head>
     
        <!-- Link Bootstrap CSS -->
        <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/css/bootstrap.min.css">
         
        <!-- Link Bootstrap Js and Jquery -->
        <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
 
        <title>
            GeeksForGeeks Bootstrap Spacing Example
        </title>
    </head>
     
    <body>
        <br>
        <tab>
        <h3>Padding And Margin</h3>
         
        <div class="container text-white">
            <br><br>
             
            <div class="pt-5 bg-success">
                GeeksForGeeks
            </div>
            <br>
             
            <div class="p-4 bg-success">
                GeeksForGeeks
            </div>
             
            <div class="m-4 pb-5 bg-success">
                GeeksForGeeks
            </div>
        </div>
    </body>
</html>

Output: 

Example 2: 




<!DOCTYPE html>
<html>
<head>
    <!-- Link bootstrap CSS -->
     
    <!-- Link bootstrap JS and Jquery -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
 
    <title>
        GeeksforGeeks Bootstrap Spacing Example
    </title>
</head>
 
<body>
 
    <div class="container"><br>
        <h4 style="color:green;">
            GeeksforGeeks is a regularly spaced.
        </h4>
         
        <h4 class="ml-5 ml-lg-0"style="color:green;">
            GFG Geeks1 has left margin visible on
            xs, sm and md displays.
        </h4>
    </div>
     
</body>
</html>                                       

Output: 

Example 3: 




<!DOCTYPE html>
<html>
<head>
    <!-- Link Bootstrap CSS -->
     
    <!-- Link Bootstrap JS and Jquery -->
    <script src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
 
    <title>
        GeeksForGeeks Bootstrap Spacing Example
    </title>
</head>
 
<body>
    <div class="container"><br>
        <div class="mx-auto" style="width:300px;
                            background-color:green;">
            <h3 style="color:white;">GeeksForGeeks</h3>
        </div>
    </div>
</body>
</html>                   

Output:

Supported Browser:


Article Tags :