Lesson 41 of 60 – Bootstrap Spacing
68%

Bootstrap Spacing

Bootstrap provides spacing utility classes that make it easy to add margin and padding to HTML elements. These utilities help developers create clean layouts without writing custom CSS for every spacing requirement.

Note: Bootstrap spacing utilities use classes such as m-* for margin and p-* for padding. They can also be applied to specific sides such as top, bottom, start, and end.

1. What Is Bootstrap Spacing?

Spacing means the empty space around or inside an HTML element. Bootstrap provides utility classes to control this space quickly.

Bootstrap spacing utilities mainly control:

  • Margin.
  • Padding.
  • Top spacing.
  • Bottom spacing.
  • Start spacing.
  • End spacing.
  • Horizontal spacing.
  • Vertical spacing.

2. Margin and Padding

Margin creates space outside an element, while padding creates space inside an element.

<div class="m-3">

    Margin Example

</div>

<div class="p-3">

    Padding Example

</div>

Bootstrap uses m for margin and p for padding.

3. Bootstrap Margin Classes

Margin classes begin with the letter m.

<div class="m-0">
No margin
</div>

<div class="m-3">
Medium margin
</div>

<div class="m-5">
Large margin
</div>

The number determines the spacing size.

4. Bootstrap Padding Classes

Padding classes begin with the letter p.

<div class="p-0">
No padding
</div>

<div class="p-3">
Medium padding
</div>

<div class="p-5">
Large padding
</div>

Padding adds space between the content and the element's border.

5. Spacing Scale

Bootstrap provides a predefined spacing scale commonly used with spacing utilities.

Class Value Typical Spacing
0 0
1 0.25rem
2 0.5rem
3 1rem
4 1.5rem
5 3rem

For example, p-3 applies the spacing value associated with 3.

6. Margin Top

The mt-* classes add margin to the top of an element.

<div class="mt-1">
Small top margin
</div>

<div class="mt-3">
Medium top margin
</div>

<div class="mt-5">
Large top margin
</div>

Here, t represents top.

7. Margin Bottom

The mb-* classes add margin to the bottom of an element.

<h2 class="mb-3">
Bootstrap
</h2>

<p>
Learn Bootstrap spacing.
</p>

Here, b represents bottom.

8. Margin Start

The ms-* classes add margin to the start side of an element.

<div class="ms-4">

    Start Margin

</div>

The use of start and end utilities also works better with right-to-left layouts than fixed left and right terminology.

9. Margin End

The me-* classes add margin to the end side.

<button class="btn btn-primary me-2">

    Save

</button>

<button class="btn btn-secondary">

    Cancel

</button>

Here, e represents end.

10. Margin Horizontal

The mx-* classes apply margin to both the start and end sides.

<div class="mx-3">

    Horizontal Margin

</div>

This is useful when you want equal horizontal spacing.

11. Margin Vertical

The my-* classes apply margin to both the top and bottom.

<div class="my-4">

    Vertical Margin

</div>

This is useful for creating equal vertical spacing.

12. Padding Top

The pt-* classes add padding to the top of an element.

<div class="pt-4">

    Top Padding

</div>

Here, p means padding and t means top.

13. Padding Bottom

The pb-* classes add padding to the bottom.

<div class="pb-4">

    Bottom Padding

</div>

Here, b represents bottom.

14. Padding Start

The ps-* classes add padding to the start side.

<div class="ps-4">

    Start Padding

</div>

Start-based utilities are useful for layouts that support different text directions.

15. Padding End

The pe-* classes add padding to the end side.

<div class="pe-4">

    End Padding

</div>

Here, e represents end.

16. Padding Horizontal

The px-* classes add padding to both the start and end sides.

<div class="px-4">

    Horizontal Padding

</div>

This is useful for adding equal left and right spacing in a left-to-right layout.

17. Padding Vertical

The py-* classes add padding to both the top and bottom.

<div class="py-4">

    Vertical Padding

</div>

This is commonly used for buttons, sections, cards, and banners.

18. Margin Auto

Bootstrap provides the m-auto utility for automatic margins.

<div
class="mx-auto"
style="width: 200px;">

    Centered Element

</div>

mx-auto is commonly used to horizontally center a fixed-width block element.

19. Negative Margin

Bootstrap also provides negative margin utilities using an n prefix in supported spacing utilities.

<div class="mt-n2">

    Negative Top Margin

</div>
Important: Negative spacing utilities should be used carefully because they can affect layout and overlap elements.

20. Responsive Spacing

Bootstrap spacing utilities can be combined with responsive breakpoints.

<div
class="p-2 p-md-4 p-lg-5">

    Responsive Padding

</div>

In this example, the padding changes at different viewport breakpoints.

21. Responsive Margin

Margin can also change according to the viewport size.

<div
class="mt-2 mt-md-4 mt-lg-5">

    Responsive Margin

</div>

The breakpoint-prefixed utility applies from that breakpoint upward, unless another class overrides it.

22. Spacing Between Buttons

Spacing utilities are useful for separating buttons.

<button class="btn btn-primary me-2">

    Save

</button>

<button class="btn btn-success me-2">

    Submit

</button>

<button class="btn btn-danger">

    Delete

</button>

The me-2 class adds spacing after the first two buttons.

23. Spacing in Cards

Spacing utilities can be used to create comfortable spacing inside cards.

<div class="card p-4">

    <h3 class="mb-3">
        Course
    </h3>

    <p class="mb-4">
        Learn Bootstrap from basics.
    </p>

    <button class="btn btn-primary">
        Start
    </button>

</div>

24. Spacing in Forms

Spacing utilities can make forms easier to read and use.

<div class="mb-3">

    <label class="form-label">
        Name
    </label>

    <input
    type="text"
    class="form-control">

</div>


<div class="mb-3">

    <label class="form-label">
        Email
    </label>

    <input
    type="email"
    class="form-control">

</div>

The mb-3 class creates space between form fields.

25. Spacing in Sections

Large padding values can be used to create breathing room around sections.

<section class="py-5">

    <h2>
        About Us
    </h2>

    <p>
        Learn more about our organization.
    </p>

</section>

The py-5 class adds vertical padding.

26. Gap Utilities

Bootstrap also provides gap utilities that work with flex and grid layouts.

<div
class="d-flex gap-3">

    <button class="btn btn-primary">
        One
    </button>

    <button class="btn btn-success">
        Two
    </button>

    <button class="btn btn-danger">
        Three
    </button>

</div>

The gap-3 class adds spacing between flex or grid items.

27. Row and Column Gaps

Bootstrap provides row-gap-* and column-gap-* utilities for controlling gaps separately.

<div
class="d-grid
row-gap-3
column-gap-2">

    <div>
        Item One
    </div>

    <div>
        Item Two
    </div>

</div>

These utilities are useful when vertical and horizontal gaps need different values.

28. Common Spacing Mistakes

Avoid these common mistakes when using Bootstrap spacing utilities:

  • Using too much margin between elements.
  • Using too much padding inside small components.
  • Adding custom CSS when a Bootstrap utility already exists.
  • Using inconsistent spacing throughout a page.
  • Forgetting responsive spacing on smaller screens.
  • Using negative margins without understanding their effect.
  • Confusing margin with padding.
  • Using too many utility classes on a single element.

29. Practical Spacing Example

The following example demonstrates margin, padding, and gap utilities together.

Bootstrap Course

Learn Bootstrap components, utilities, responsive layouts, and practical projects.

<div class="p-4 bg-light rounded">

    <h3 class="mb-3">
        Bootstrap Course
    </h3>

    <p class="mb-4">
        Learn Bootstrap components,
        utilities and responsive layouts.
    </p>

    <div class="d-flex gap-2">

        <button class="btn btn-primary">
            Start Course
        </button>

        <button class="btn btn-success">
            View Lessons
        </button>

    </div>

</div>

30. Complete Bootstrap Spacing Example

This example combines Bootstrap margin, padding, responsive spacing, and gap utilities to create a simple responsive course section.

<section
class="py-5">

    <div
    class="container">

        <div
        class="p-4 p-md-5 bg-light rounded">


            <h2 class="mb-3">

                Learn Bootstrap

            </h2>


            <p class="mb-4">

                Build responsive websites
                using Bootstrap utilities
                and components.

            </p>


            <div
            class="d-flex
            flex-column
            flex-md-row
            gap-2">


                <button
                class="btn btn-primary">

                    Start Learning

                </button>


                <button
                class="btn btn-success">

                    View Course

                </button>


            </div>


        </div>

    </div>

</section>

Learn Bootstrap

Build responsive websites using Bootstrap utilities and components.

📌 Key Points

  • m-* controls margin.
  • p-* controls padding.
  • mt-* controls margin-top.
  • mb-* controls margin-bottom.
  • ms-* controls margin-start.
  • me-* controls margin-end.
  • mx-* controls horizontal margin.
  • my-* controls vertical margin.
  • pt-* controls padding-top.
  • pb-* controls padding-bottom.
  • ps-* controls padding-start.
  • pe-* controls padding-end.
  • px-* controls horizontal padding.
  • py-* controls vertical padding.
  • mx-auto is commonly used for horizontal centering.
  • Responsive spacing can use breakpoint prefixes such as p-md-4.
  • gap-* controls spacing between flex or grid items.
  • row-gap-* and column-gap-* can control gaps separately.
  • Spacing utilities help reduce the need for custom CSS.
  • Consistent spacing improves readability and page layout.

🧠 Quick Quiz

Question: Which Bootstrap class adds margin to all four sides of an element?