Open In App

Bootstrap 5 Spacing

The spacing utility is used to assign responsive-friendly margin or padding values to an element or a subgroup of its sides. Padding is the area inside of any div or container, as opposed to a margin, which is the area outside of it. Margin and Padding can be added by adding Bootstrap classes in an element or container.

To change an element’s appearance, Bootstrap offers a variety of responsive margin and padding utility classes. The classes are named using the format {property}{sides}-{breakpoint}-{size}  for sizes s, md, xs, lg, xl, XXL. Here sides can be t(top), b(bottom), e(end),s (start), etc.



Example 1: Here is an example of margin and padding using bootstrap spacing classes.




<!DOCTYPE html>
<html lang="en">
 
<head>
    <title>Bootstrap 5 Spacing</title>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1">
    <link href=
          rel="stylesheet">
</head>
 
<body>
    <div class="text-center">
        <h1 class="text-success">
            GeeksforGeeks
        </h1>
        <h2 class="m-1 p-1">Bootstrap 5 Spacing</h2>
    </div>
    <div class="container text-white">
 
        <div class="mt-5 px-5 bg-success">
            GeeksForGeeks-Padding to the left and margin to the top
        </div>
 
 
        <br>
 
        <div class="mx-4 bg-success">
            GeeksForGeeks - margin from left and Right of div
        </div>
        <br>
 
        <div class="py-4 bg-success">
            GeeksForGeeks - Padding to the top and bottom of div
        </div>
        <br>
        <div class="px-4 bg-success">
            This is GeeksForGeeks -Padding to the left and Right of div
        </div>
    </div>
 
</body>
 
</html>

Output:



Padding in Margin in Bootstrap 5

Example 2: Here is an example of a gap using bootstrap spacing classes.




<!DOCTYPE html>
<html lang="en">
 
<head>
    <title>Bootstrap 5 Spacing</title>
    <meta charset="utf-8">
    <meta name="viewport"
          content="width=device-width, initial-scale=1">
    <link href=
          rel="stylesheet">
 
</head>
 
<body>
    <div class="text-center">
        <h1 class="text-success">
            GeeksforGeeks
        </h1>
        <h2>Bootstrap 5 Spacing</h2>
        <div class="d-grid gap-2">
            <div class="p-1 bg-warning ">GeeksForGeeks item 1</div>
            <div class="p-1 bg-success ">GeeksForGeeks item 2</div>
            <div class="p-1 bg-primary ">GeeksForGeeks item 3</div>
        </div>
 
</body>
 
</html>

Output:

Gap utility in Bootstrap 5

Reference: https://getbootstrap.com/docs/5.0/utilities/spacing/ 


Article Tags :