Spacing utilities help you add space around elements quickly without writing custom CSS. They make your page look neat and organized.
Spacing utilities (margin, padding) in 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 3Sizes 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.
mb-4
px-2
m-0
pt-5
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.
<!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>
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).
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.