Open In App

How to fix Spacing Classes in Bootstrap?

Last Updated : 30 Jul, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

Bootstrap 5 provides a comprehensive set of spacing utilities that can be used to control the spacing between elements. These utilities cover various spacing properties, including margin and padding that can be applied to different sides of an element (top, right, bottom, left, and all sides).

Below are the approaches to fix spacing classes in Bootstrap 5:

Applying Margin Classes

Margin classes in Bootstrap 5 are prefixed with m for margins and followed by the side (t for top, r for right, b for bottom, l for left, x for horizontal, y for vertical, and blank for all sides). The values range from 0 to 5, representing different spacing levels.

Syntax:

<div class="bg-primary text-white p-3 m-3">This element has a margin of 3 on all sides.</div>
<div class="bg-secondary text-white p-3 mt-5">This element has a top margin of 5.</div>

Example: To demonstrate fixing the spacing classes by applying margin classes.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Margin Classes Example</title>
    <link href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" 
          rel="stylesheet">
</head>

<body>
    <div class="container my-5">
        <div class="bg-primary text-white p-3 m-3">
              This element has a margin of 3 on all sides.
          </div>
        <div class="bg-secondary text-white p-3 mt-5">
              This element has a top margin of 5.
          </div>
    </div>
</body>

</html>

Ouput:

Screenshot-(935)
Spacing classes in Bootstrap 5

Applying Padding Classes

Padding classes in Bootstrap 5 are prefixed with p for padding and followed by the side (t for top, r for right, b for bottom, l for left, x for horizontal, y for vertical, and blank for all sides). The values range from 0 to 5, representing different spacing levels.

Syntax:

<div class="bg-primary text-white p-3">This element has a padding of 3 on all sides.</div>
<div class="bg-secondary text-white px-5">This element has a horizontal padding of 5.</div>

Example: To demonstrate fixing the spacing classes by applying padding classes in Bootstrap 5.

HTML
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" 
        content="width=device-width, initial-scale=1.0">
  <title>Margin Classes Example</title>
  <link href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css" 
        rel="stylesheet">
</head>
<body>
  <div class="container my-5">
    <div class="bg-primary text-white p-3 m-3">
          This element has a margin of 3 on all sides.
        </div>
    <div class="bg-secondary text-white p-3 mt-5">
          This element has a top margin of 5.
        </div>
  </div>
</body>
</html>

Output:

Screenshot-(936)
Spacing classes in Bootstrap 5

Combining Margin and Padding Classes

You can combine margin and padding classes to achieve the desired spacing effect.

Syntax:

<div class="bg-primary text-white p-3">
This element has a padding of 3 on all sides.
</div>

Example: To demonstrate fixing the spacing classes by combining margin and padding classes in Bootstrap 5.

HTML
<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" 
          content="width=device-width, initial-scale=1.0">
    <title>Combining Margin and Padding Classes</title>
    <link href=
"https://cdn.jsdelivr.net/npm/[email protected]/dist/css/bootstrap.min.css"
          rel="stylesheet">
</head>

<body>
    <div class="container my-5">
        <div class="bg-primary text-white p-3 m-5">
              This element has padding
            of 3 and margin of 5 on all sides.
          </div>
        <div class="bg-secondary text-white pt-4 pb-2 mx-3">
              This element has a top padding of 4, a bottom 
              padding of 2, and horizontal margins of 3.
          </div>
    </div>
</body>

</html>

Output:

Screenshot-(937)
Spacing classes in Bootstrap 5

Next Article

Similar Reads