Spacing

Techniques and properties used to control the amount of space around and between elements. This helps in designing layouts and improving the visual aesthetics of web pages.

How It Works

  • To apply the spacing on any element, the format will be: hpu-{property}{side}-{size}.
    • Where the property is one of:
      • m - for classes set to margin
      • p - for classes set to padding
    • Where the side is one of:
      • t - for classes set to margin-top or padding-top.
      • b - for classes set to margin-bottom or padding-bottom.
      • l - for classes set to margin-left or padding-left.
      • r - for classes set to margin-right or padding-right.
      • x - for classes set to both *-left and *-right.
      • y - for classes set to both *-top and *-bottom.
      • blank - for classes to set margin or padding on all 4 sides of the element (e.g. .hpu-m-* or .hpu-p-*).
    • Where the size is one of:
      • auto - sets margin classes to auto.
      • 0 - sets margin and padding to 0.
      • 1 - sets margin and padding to $spacer * .5.
      • 2 - sets margin and padding to $spacer.
      • 3 - sets margin and padding to $spacer * 1.75.
      • 4 - sets margin and padding to $spacer * 2.5.
      • 5 - sets margin and padding to $spacer * 3.
  • $spacer by default is set to 1.2rem in _variables.scss file.
  • Low-level spacing is also available with unit to explicitly set margin or padding.
  • Responsive spacing can also be set.

Margin

Used to create space around elements, outside of any defined borders.

In the examples below, margin-top property is used: .hpu-mt-{0 - 5}

.hpu-mt-0

.hpu-mt-1

.hpu-mt-2

.hpu-mt-3

.hpu-mt-4

.hpu-mt-5

Padding

Creates spaces between an element’s content and its border. Unlike margins, padding affects only the content inside the element

In the examples below, padding-top and padding-bottom properties are used, which is the y-axis: .hpu-py-{0 - 5}.

.hpu-py-0

.hpu-py-1

.hpu-py-2

.hpu-py-3

.hpu-py-4

.hpu-py-5

Auto Margin

Auto margins can be used to center elements or distribute space within a container. They work on block-level elements with a defined width:

  • .hpu-mr-auto - aligns the element to the left by pushing it away from the right
  • .hpu-mx-auto - centers the element horizontally using equal left and right margins
  • .hpu-ml-auto - aligns the element to the right by pushing it away from the left

In the example below, the set width for the content is .hpu-w7rem. Please refer to HPUCSS Dimensions for sizing usage guides.

.hpu-mr-auto

HTML



<div class="hpu-py-2 hpu-border-gray">
    <div class="hpu-w7rem hpu-p-2 hpu-border-1st hpu-bg-2nd hpu-mr-auto"></div>
</div>

.hpu-mx-auto

HTML



<div class="hpu-py-2 hpu-border-gray">
    <div class="hpu-w7rem hpu-p-2 hpu-border-1st hpu-bg-2nd hpu-mx-auto"></div>
</div>

.hpu-ml-auto

HTML



<div class="hpu-py-2 hpu-border-gray">
    <div class="hpu-w7rem hpu-p-2 hpu-border-1st hpu-bg-2nd hpu-ml-auto"></div>
</div>

Low-level Utility Spacing Classes

So far, the examples above have predefined margin and padding values, which can sometimes result in too much or too little spacing for specific needs. This is where the low-level utility classes for margin and padding come in handy.

  • The rule for applying low-level utility classes is similar to the previous one, but with a slight variation and the unit addition: .hpu-{property}{side}-{size}{unit}
  • Where the size is one of:
    • 1 - 1000 - for setting the margin and padding value.
  • Where the unit is one of:
    • px - for classes set to pixels unit.
    • rem - for classes set to rem unit.

In the examples below, px unit is used with .hpu-mt-{size}px (margin-top) classes, and rem unit is used with .hpu-py-{size}rem (padding-top and padding-bottom) classes.

Margin in px unit using .hpu-mt-{size}px (margin-top):

.hpu-mt-25px

.hpu-mt-50px

.hpu-mt-75px

.hpu-mt-100px

Padding in rem unit using .hpu-py-{size}rem (padding-top and padding-bottom):

.hpu-py-2rem

.hpu-py-4rem

.hpu-py-6rem

.hpu-py-8rem