Exploring Bootstrap: Building Modern Responsive Websites. Third Part: Spacing Utilities

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:

1. Exploring Bootstrap: Building Modern Responsive Websites. First Part: Introduction to the Grid System

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:

  1. t - for classes that set margin-top or padding-top 
    b - for classes that set margin-bottom or padding-bottom 
    s (start) - for classes that set the margin-left or padding-left for LTR direction, and margin-right or padding-right for RTL direction. 
    e (end) - for classes that set the margin-right for RTL direction, and margin-left for RTL direction. 
    x - for classes that set both the margin-left and margin-right properties or padding-top and padding-right properties. 
    y - for classes that set both the margin-top and margin-bottom properties, respectively padding-top and padding-bottom
    blank - for classes that set the margin or padding for all sides of an element.

The sizes can be:

  1. 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 to auto

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!

Post last updated on Dec 31, 2023