0
0
Bootsrapmarkup~5 mins

Spacing utilities (margin, padding) in Bootsrap

Choose your learning style9 modes available
Introduction

Spacing utilities help you add space around elements quickly without writing custom CSS. They make your page look neat and organized.

You want to add space between buttons so they don't stick together.
You need some breathing room around text or images.
You want consistent spacing across different parts of your page.
You want to adjust space on different screen sizes easily.
You want to fix layout issues caused by elements being too close.
Syntax
Bootsrap
m{side}-{size} for margin
p{side}-{size} for padding

Where:
- {side} can be: t (top), b (bottom), s (start/left), e (end/right), x (left and right), y (top and bottom), or empty (all sides)
- {size} is a number from 0 to 5 (0 means no space, 5 is the largest space)

Example: mt-3 means margin-top with size 3

Sizes 0 to 5 correspond to spacing values defined by Bootstrap, roughly from 0 to 3rem.

Use ms and me for left and right in left-to-right languages, which helps with right-to-left support.

Examples
Adds margin to the bottom with size 4.
Bootsrap
mb-4
Adds padding on left and right with size 2.
Bootsrap
px-2
Removes all margin from all sides.
Bootsrap
m-0
Adds large padding on top with size 5.
Bootsrap
pt-5
Sample Program

This example shows two buttons with space between them using me-3 (margin-end). The paragraph below has margin on top and padding inside. The blue box also has padding and margin on top to separate it from the paragraph.

Bootsrap
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1" />
  <title>Bootstrap Spacing Utilities Example</title>
  <link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet" />
</head>
<body>
  <main class="container mt-5">
    <h1 class="mb-4">Spacing Utilities Demo</h1>
    <button class="btn btn-primary me-3">Button 1</button>
    <button class="btn btn-secondary">Button 2</button>
    <p class="mt-4 p-3 border bg-light">This paragraph has margin-top and padding all around.</p>
    <div class="bg-info text-white p-4 mt-3">This box has padding and margin top.</div>
  </main>
</body>
</html>
OutputSuccess
Important Notes

Use spacing utilities to quickly adjust layout without writing CSS.

Combine different sides and sizes to get the exact spacing you want.

Remember that m is for margin (outside space) and p is for padding (inside space).

Summary

Spacing utilities add margin or padding easily with classes.

Use m for margin and p for padding, plus side and size.

They help keep your design neat and responsive without custom CSS.