Applying Margin and Padding in Bootstrap 5, 4, and 3

Last Updated on January 14, 2025 by Roshan Parihar

Learn how to add margin and padding in Bootstrap 5 and other versions. You can apply utility classes in Bootstrap 5 to add spacings without using CSS. Let’s find out with the examples.

Margin and Padding in Bootstrap 5

The simple syntax for margin and padding in Bootstrap 5 is given below:

Syntax

Property in Bootstrap 5:

  • m for margin
  • p for padding

Sides in Bootstrap 5:

  • t for top
  • b for bottom
  • s for start
  • e for end
  • x for horizontal (left and right)
  • y for vertical (top and bottom)
  • Blank (no side) – applies to all sides.

Sizes in Bootstrap 5:

  • 0 for no space
  • 1 for 0.25rem (4px by default)
  • 2 for 0.5rem (8px by default)
  • 3 for 1rem (16 px by default)
  • 4 for 1.5rem (24px by default)
  • 5 for 3rem (48 px by default)
  • auto for automatic margin

Margins in Bootstrap 5

To apply the margin in Bootstrap 5, use the property m with dash (-) and size after it. Add positions with the property without any space to give margins accordingly.

Test it Live

Output

Margin in Bootstrap 5

I have added a background to each div elements to easily analyze the margins given to them.

Paddings in Bootstrap 5

If you want to apply the padding in Bootstrap 5, use the property p with dash (-) and size. Add the position with the property without any spaces to give padding according to positions.

Test it Live

Output

Padding in Bootstrap 5

Backgrounds are added to div elements above to easily identify the paddings given to them.

Note: Negative paddings are not allowed in Bootstrap or CSS. It can create undefined or illogical behaviour because the content cannot overlap the containers edge.

Give Margin and Padding in Bootstrap 4

Syntax

Property in Bootstrap 4:

  • m for margin
  • p for padding

Sides in Bootstrap 4:

  • t for top
  • b for bottom
  • l for start
  • r for end
  • x for horizontal (left and right)
  • y for vertical (top and bottom)
  • Blank (no side) – applies to all sides.

Sizes in Bootstrap 4:

  • 0 for no space
  • 1 for 0.25rem (4px by default)
  • 2 for 0.5rem (8px by default)
  • 3 for 1rem (16 px by default)
  • 4 for 1.5rem (24px by default)
  • 5 for 3rem (48 px by default)
  • auto for automatic margin

Margins in Bootstrap 4

For applying margin in Bootstrap 4, use the property m with a dash(-) after it followed by a size. Use positions with the property to give positionwise margins.

Test it Live

Output

Examples for Margin

The above examples showing the backgrounds with each div elements to easily find the margins given.

Paddings in Bootstrap 4

Use the property p followed by dash(-) and size after it. Add positions with the property without any space as given below:

Test it Live

Output

Examples for Margin

Added backgrounds to the examples to easily get the idea of how paddings are working.

Applying Margins and Paddings in Bootstrap 3

There are no classes available for margin and padding in Bootstrap 3. However, you can add custom CSS for this as given below:-

Test it Live

Output

Examples for Margins and paddings in Bootstrap 3

Leave a comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.