Spacing
It is important to give appropriate space between elements. Most elements/classes that we built have pre-defined spacing properties. If you are not satisfied with default spacing properties, you may use the classes below to manually adjust the spacing.
Margin and padding
You can control the spacing between elements by either padding
or margin
. Although they do similar things in CSS, they are not quite the same, and the image on the right may give you a general idea of the difference between these two.
Padding
is used to create space around an element's content, inside of any defined borders.Margin
is used to create space around two elements, outside of any defined borders.
For example, if you feel the borders of two elements are too close to each other, you should increase the margin; if you feel the element's content is too close its border, you should increase the padding.
Overview
You have full control over both margins and paddings. We've built various spacing classes for you to use, and they all follow this notation:
{property}{sides*}-{size}
*Sides is an optional field.
Where property can be one of:
m
: stands for marginp
: stands for padding
Where sides can be one of:
t
: stands for topb
: stands for bottoml
: stands for leftr
: stands for rightx
: stands for x-axis, this will set bothl
andr
.y
: stands for y-axis, this will set botht
andb
.- blank: this will set all four sides:
t, b, l, r
.
Where size can be one of:
0
: set the spacing size to 0px1
: set the spacing size to 10px2
: set the spacing size to 20px3
: set the spacing size to 30px4
: set the spacing size to 40px5
: set the spacing size to 50pxauto
(margin only): set margin to auto.
Examples
Below are a few examples to help you understand the spacing class better:
<div class="p-0">
Remove all paddings (on 4 sides) from the element inside.
<div class="pb-3">
Replace the current padding-bottom setting to 30px.
Please note: if the element's padding-bottom size is 30px by default, adding this class will not make any difference.
<div class="mx-2">
Replace the current setting on margin-left and margin-right to 20px.
If you want to learn more about advanced spacing classes, please view the 'Customisation' tab.
If you want to learn more about how to apply these spacing classes, please view the 'Use cases' tab.
Customisation
In this section, we will explain advanced spacing classes.
Responsive design
In the previous 'General' tab, we've covered the basic class notation, which will apply to all screen sizes. But in case if you would like to apply different spacing to the different viewport, you can achieve it with this format:
{property}{sides*}-{breakpoint}-{size}
Where breakpoint can be one of:
sm
: stands for small screen size, which is ≥ 576pxmd
: stands for medium screen size, which is ≥ 768pxlg
: stands for large screen size, which is ≥ 992pxxl
: stands for m screen size, which is ≥ 1200px
<div class="mt-md-3">
When the screen size is bigger or equal to 768px, set margin-top size to 30px.
<div class="mt-md-3 mt-sm-1">
When the screen size is bigger or equal to 768px, set margin-top size to 30px, and when the screen size is < 768px, set margin-top size to 10px.
If you are not familiar with breakpoints, please go through Grid layouts and Responsive design.
Negative margin
In CSS, margin properties can utilise negative values (not applicable to paddings). The notation of having a negative margin is quite similar to our basic notation, you just need to append 'n' before the {size}
.
{property}{sides*}-n-{size}
<div class="mt-n-1">
Set margin-top size to -10px.
Gutter
Our grid system has default gutter between each column. In case you want to increase or decrease the gutter size, you can use the following classes to achieve this.
g-{size}
Where size can be one of:
0
: set the gutter size to 0rem1
: set the gutter size to 0.25rem2
: set the gutter size to 0.5rem3
: set the gutter size to 1rem4
: set the gutter size to 1.5rem5
: set the gutter size to 3rem
See below as an example:
<div class="container">
<div class="row py-3">
<div class="col-6"><div class="p-3">col-6</div></div>
<div class="col-6"><div class="p-3">col-6</div></div>
</div>
<div class="row py-3 g-0">
<div class="col-6"><div class="p-3">col-6</div></div>
<div class="col-6"><div class="p-3">col-6</div></div>
</div>
</div>
In this example, we can see there was a default gutter between columns. It indicates the default gutter is not zero. If you want to make two columns adjacent to each other, you may want to add g-0
as a class.
Examples
Below are a few examples to help you understand the code structure better.
<div class="m-2">
<div class="p-2">
<div class="mx-auto">
<div class="py-md-3">
1. Introduction
Most elements in your website will have the appropriate spacing built into the styles by default. In some cases, you might want to adjust the spacing a little. The following classes allow you to do this.
2. Margin and Padding
In your websites, margin is the space around an element's border, while padding is the space between an element's border and the element's content.
For example, if you feel the border is too close to the other element, you should decrease the margin. While the padding should be increased when the content is too close to the border.
Notation
The classes are named using the format {property}{sides}-{size}
.
If you would like to apply different spacing to different viewport, you can also use the format {property}{sides}-{breakpoint}-{size}
for class names.
Where property is one of:
m
- for classes that setmargin
p
- for classes that setpadding
Where sides is one of:
t
- for classes that setmargin-top
orpadding-top
b
- for classes that setmargin-bottom
orpadding-bottom
l
- for classes that setmargin-left
orpadding-left
r
- for classes that setmargin-right
orpadding-right
x
- for classes that set both*-left
or*-right
y
- for classes that set both*-top
or*-bottom
- blank - for classes that set a
margin
orpadding
on all 4 sides of the element
Where size is one of:
0
- for classes that eliminate themargin
orpadding
by setting it to0
1
- (by default) for classes that set themargin
orpadding
to10px
2
- (by default) for classes that set themargin
orpadding
to20px
3
- (by default) for classes that set themargin
orpadding
to30px
4
- (by default) for classes that set themargin
orpadding
to40px
5
- (by default) for classes that set themargin
orpadding
to50px
auto
- for classes that set the margin to auto
Where breakpoint is one of sm
, md
, lg
, xl
.
Applying classes
Spacing classes can be applied as class="style"
to any element. When there is already a class present, use a space before adding the next one. Some examples:
<div class="container">
<div class="row mb-3 pl-1"> This will create a margin on the bottom for 30px and a padding on the left for 10px
<div class="col px-2"> This will create paddings on both left and right side for 20px
</div>
</div>
</div>
Examples
<div class="m-2">
<div class="p-2">
<div class="mx-auto">
<div class="py-md-3">
Negative margin
The syntax is nearly the same as the default, positive margin utilities, but with the addition of n before the requested size. Here’s an example class that’s the opposite of .mt-1:
.mt-n-1 {
margin-top: -10px !important;
}
3. Gutter
The grid columns come with a default gutter between them. When you are using them, you may want to increase or eliminate the gutter. In this scenario, here are the classes you can use:
The notation is similar to the margins and paddings. We use the format {property}-{size}
for class names. The property for Gutter is g
It is also using the same notation for {size}
as described above.
Here are the examples:
<div class="container">
<div class="row py-3">
<div class="col-6"><div class="p-3">col-6</div></div>
<div class="col-6"><div class="p-3">col-6</div></div>
</div>
<div class="row py-3 g-0">
<div class="col-6"><div class="p-3">col-6</div></div>
<div class="col-6"><div class="p-3">col-6</div></div>
</div>
</div>
In this example, we can see even we didn't specify the gutter, there is a space between column items. It indicates the default gutter is not zero. If you want to make two columns adjacent to each other, you may want to add g-0
as a class.