Margin Bootstrap
In Bootstrap 4, margins can be managed using a set of predefined classes that follow a consistent naming convention. These classes are used to add spacing around elements in a flexible way. Here's a breakdown of how margin classes work in Bootstrap 4:
Margin Classes
The margin classes in Bootstrap 4 are formatted as follows:
m{t|r|b|l|x|y}-{size}
Where:
m
stands for margin.t
stands for top.r
stands for right.b
stands for bottom.l
stands for left.x
stands for horizontal (left and right).y
stands for vertical (top and bottom).{size}
is a number from 0 to 5, where each number represents a different margin size.
Margin Sizes
The sizes range from 0 to 5, with the following pixel values:
0
: 0px (no margin)1
: 0.25rem (4px)2
: 0.5rem (8px)3
: 1rem (16px)4
: 1.5rem (24px)5
: 3rem (48px)
Examples
-
Margin on All Sides
To apply a margin of 3 units (16px) to all sides of an element:
<div class="m-3">Content</div>
-
Margin on the Top
To apply a margin of 2 units (8px) to the top of an element:
<div class="mt-2">Content</div>
-
Margin on the Right
To apply a margin of 4 units (24px) to the right of an element:
<div class="mr-4">Content</div>
-
Margin on the Bottom
To apply a margin of 1 unit (4px) to the bottom of an element:
<div class="mb-1">Content</div>
-
Horizontal Margin
To apply a margin of 5 units (48px) to both the left and right of an element:
<div class="mx-5">Content</div>
-
Vertical Margin
To apply a margin of 3 units (16px) to both the top and bottom of an element:
<div class="my-3">Content</div>
Responsive Margins
Bootstrap 4 also allows for responsive margins, which means you can apply different margins at different breakpoints. The format for responsive margins is:
m{t|r|b|l|x|y}-{size}-{breakpoint}
For example, to apply a margin of 2 units on the top for small screens and 4 units for medium screens and up:
<div class="mt-2 mt-md-4">Content</div>
This will apply a margin of 8px on the top for screens smaller than 768px and 24px for screens 768px and larger.
These classes make it easy to manage spacing and maintain consistency across your Bootstrap-based designs.
At Online Learner, we're on a mission to ignite a passion for learning and empower individuals to reach their full potential. Founded by a team of dedicated educators and industry experts, our platform is designed to provide accessible and engaging educational resources for learners of all ages and backgrounds.
Copyright 2023-2025 © All rights reserved.