Spacing in Bootstrap with Examples - GeeksforGeeks

28 Sep, 2021
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: 
Example 1: 
HTML
<!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>        <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>        <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.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 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>    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.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 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>    <script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.6/umd/popper.min.js"></script>    <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.2.1/js/bootstrap.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
ysachin2314
sweetyty
Picked
Web-Programs
Bootstrap
Web Technologies
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to change navigation bar color in Bootstrap ?
Form validation using jQuery
How to pass data into a bootstrap modal?
How to align navbar items to the right in Bootstrap 4 ?
How to Show Images on Click using HTML ?
Remove elements from a JavaScript Array
Installation of Node.js on Linux
Convert a string to an integer in JavaScript
How to fetch data from an API in ReactJS ?
How to insert spaces/tabs in text using HTML/CSS?