Exploring Bootstrap: Building Modern Responsive Websites. Third Part: Spacing Utilities
In Bootstrap June 2, 2023
Updated on Dec 31, 2023
In this third part, we'll discuss layout utilities, specifically spacing utilities. With Bootstrap's margin and padding utilities, we can easily adjust how elements and components are sized and spaced. The spaces utilities are based on a 1rem value default $spacer variable.
All parts:
2. Exploring Bootstrap: Building Modern Responsive Websites. Second Part: Typography
3. Exploring Bootstrap: Building Modern Responsive Websites. Third Part: Spacing Utilities
4. Exploring Bootstrap: Building Modern Responsive Websites. Fourth Part: Border Utilities
As with other Bootstrap classes, we can set margins and padding for all viewports or target-specific viewports.
The naming convention follows the format {property}{sides}-{size}
for extra small (xs
)
devices, and {property}{sides}-{breakpoint}-{size}
for small (sm
), medium (md
),
large (lg
), extra large (xl
), and extra extra large (xxl
) devices.
The property is m
for margins, and p
for paddings.
The sides can be specified with the following values:
-
t
- for classes that setmargin-top
orpadding-top
b
- for classes that setmargin-bottom
or padding-bottom
s
(start) - for classes that set themargin-left
orpadding-left
forLTR
direction, andmargin-right
orpadding-right
forRTL
direction.
e
(end) - for classes that set themargin-right
forRTL
direction, andmargin-left
forRTL
direction.
x
- for classes that set both themargin-left
andmargin-right
properties orpadding-top
andpadding-right
properties.
y
- for classes that set both themargin-top
andmargin-bottom
properties, respectivelypadding-top
andpadding-bottom
.
blank - for classes that set the margin or padding for all sides of an element.
The sizes can be:
-
0
- to remove all margins and paddings from the element.
1
- margins and padding are set to $spacer * .25
2
- margins and paddings are set to$spacer * .5
3
- margins and paddings are set to$spacer
4
- margins and paddings are set to$spacer * 1.5
5
- margins and paddings are set to $spacer * 3
auto
- margins and paddings are set toauto
These classes provide a flexible way to control the spacing and positioning of elements in a responsive manner using predefined sizes and sides.
We want a better layout for our product page, so let's start adding margins and paddings to our elements. Let's start
with the header. We do not want this header to be so close to the top, so we'll add an mt-5
class to it:
<header class="mt-5">
...
</header>
The div
containing the invitation information is too close to the image, so we'll apply some padding, but
only for medium devices and up:
<div class="col-12 col-md-7 ps-md-5">
...
</div>
Also, we'll apply some top margin to the h1 heading:
<h1 class="mt-5 fw-bold text-center text-md-start">Pink Guitar Last Bash in Nash Bachelorette Invitation</h1>
And a bottom margin for the paragraph containing the price:
<p class="h2 text-center text-md-start mb-5">$3.90</p>
Conclusion
In this part of the tutorial, we explored the power of Bootstrap's layout utilities, specifically focusing on spacing utilities. By leveraging margin and padding classes, we were able to enhance the layout of our product page, adjusting the spacing between elements and improving the overall visual appeal. Bootstrap's intuitive class naming conventions and responsive design approach make it easy to create well-structured and visually appealing web pages. In the next part of the tutorial, we will delve into additional Bootstrap features to further enhance our product page. Stay tuned!