What are the margin utilities in Bootstrap 4?
Bootstrap 4 comes with different spacing utilities that help us in responsive design. One of these is the margin utility classes.
They work for all breakpoints.
- Extra small:
xs(<=576px) - Small:
sm(>=576px) - Medium:
md(>=768px) - Large:
lg(>=992px) - Extra large:
xl(>=1200px)
Syntax
The utility classes are used in this format:
{property}{sides}-{breakpoint}-{size}
This is the same for all breakpoints, except the xs, which is used in this format:
{property}{sides}-{size}
This syntax is very easy to use, and a developer needs to study its components. The shorthand for the components is explained below.
Property
The property is margin, and is denoted by m.
Sides
The sides include:
t: Sets themargin-top.b: Sets themargin-bottom.l: Sets themargin-left.r: Sets themargin-right.x: Sets themargin-leftandmargin-right.y: Sets themargin-topandmargin-bottom.
Sizes
The sizes include the following:
0: Sets themarginto 0.1: Sets themarginto .25rem. This will be 4px if the font size is 16px, that is, 25% of the spacer.2: Sets themarginto .5rem. This will be 8px if the font size is 16px, that is, 50% of the spacer.3: Sets themarginto 1rem. This will be 16px if the font size is 16px, that is, 100% of the spacer.4: Sets themarginto 1.5rem. This will be 24px if the font size is 16px, that is, 150% of the spacer.5: Sets themarginto 3rem. This will be 48px if the font size is 16px, that is, 300% of the spacer.auto: Sets themarginto auto.
Note:
-
We can also set
marginto negative. This is done by addingnin front of the size.For example,
n1sets themarginto -.25rem. This will be -4px if the font size is 16px. -
You can set fixed-width block-level content to the center horizontally by setting the horizontal margins to
auto.class="mx-auto"The code above moves the
divto the center.
Fixed-width block-level content refers to an element that has its display set to
block, and haswidthset.
You can keep practicing to improve your understanding.