Open In App

Spacing in Bootstrap with Examples

Last Updated : 28 Sep, 2021
Improve
Improve
Like Article
Like
Save
Share
Report

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)(sides)-(size) for xs
  • (property)(sides)-(breakpoint)-(size) for sm, md, lg, and xl.

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

  • m: This property defines the margin. Margin provides an edge or border.
  • p: This property defines the padding. Padding properties are used to generate space around the content.

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

  • t : margin-top/padding-top.
  • b : margin-bottom/padding-bottom.
  • l : margin-left/padding-left.
  • r : margin-right/padding-right.
  • x : for padding-left and padding-right/margin-left and margin-right.
  • y : for padding-top and padding-bottom/margin-top and margin-bottom.
  • blank : margin/padding on all sides of the element.

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

  • 0 – 0px margin/padding.
  • 1 – 4px margin/padding.
  • 2 – 8px margin/padding.
  • 3 – 16px margin/padding.
  • 4 – 24px margin/padding.
  • 5 – 48px margin/padding.
  • auto – auto margin.

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

  • sm, md, lg, and xl are the following breakpoints.

Syntax: 

  • For breakpoint – xs:
<div class="mt-4">
  • For breakpoint – md:
<div class="mt-md-4">
  • For breakpoint – lg:
<div class="mt-lg-4">
  • For breakpoint – xl:
<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: 

HTML




<!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>
        <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: 

HTML




<!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: 

HTML




<!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:

  • Google Chrome
  • Internet Explorer
  • Firefox
  • Opera
  • Safari


Like Article
Suggest improvement
Previous
Next
Share your thoughts in the comments

Similar Reads