Bootstrap 5 Utility Classes Quick Reference: Margin, Padding, and Show/Hide Techniques

Bootstrap5 utility classes act as a “CSS code generator,” allowing you to quickly style elements by adding simple class names without writing a single line of CSS, significantly improving development efficiency. This article focuses on the most commonly used margin/padding utility classes and show/hide techniques, suitable for beginners to get started quickly.

I. Margin and Padding Utility Classes

Margin (outer spacing) and padding (inner spacing) are core utility classes for controlling element spacing. Their naming convention is clear and intuitive.

1. Naming Convention

The format for margin/padding classes in Bootstrap5 is: [property]-[direction]-[size], where:
- Property: m (margin) or p (padding)
- Direction: t (top), b (bottom), l (left), r (right), x (left+right), y (top+bottom)
- Size: 0 (0 spacing), 1 (0.25rem), 2 (0.5rem), 3 (1rem), 4 (1.5rem), 5 (3rem), auto (auto, only for margin)

2. Common Margin Classes

  • Single-direction control:
  • mt-3: Top margin (margin-top: 1rem)
  • mb-2: Bottom margin (margin-bottom: 0.5rem)
  • ml-4: Left margin (margin-left: 1.5rem)
  • mr-1: Right margin (margin-right: 0.25rem)

  • Multi-direction control:

  • mx-3: Set left and right margins (margin-left: 1rem; margin-right: 1rem)
  • my-2: Set top and bottom margins (margin-top: 0.5rem; margin-bottom: 0.5rem)
  • mx-auto: Auto left/right margins (block element centering: e.g., <div class="mx-auto" style="width: 50%;">)

3. Common Padding Classes

Padding classes are similar to margin, but use p as the property (inner spacing between content and border):
- pt-2: Top padding (padding-top: 0.5rem)
- pb-4: Bottom padding (padding-bottom: 1.5rem)
- px-3: Left and right padding (padding-left: 1rem; padding-right: 1rem)
- py-1: Top and bottom padding (padding-top: 0.25rem; padding-bottom: 0.25rem)

4. Example Code

<!-- Div with margin and padding -->
<div class="mt-4 p-3 bg-light border">
  This is a styled div:
  - Top margin (mt-4, 1rem top spacing)
  - Padding around (p-3, 0.75rem inner spacing)
  - Background color and border controlled by other classes
</div>

II. Show and Hide Techniques

Utility classes let you control element visibility without writing media queries.

1. Basic Display Control

Control element display type (display property) with d-* classes:
- d-none: Hide element (display: none, no space occupied)
- d-block: Block display (display: block)
- d-flex: Flex container (display: flex)
- d-inline: Inline display (display: inline)

Example: Hide button (hidden by default, shown on hover)

<button class="d-none d-md-block">Hidden on mobile, visible on desktop</button>

2. Responsive Show/Hide (by Breakpoints)

Bootstrap5 provides breakpoint-based visibility control. Breakpoints: sm (576px), md (768px), lg (992px), xl (1200px), xxl (1400px).
- Hide logic: d-*-none (replace * with breakpoint, e.g., d-md-none = hidden on medium+ screens)
- Show logic: d-*-[type] (e.g., d-sm-block = visible as block on small+ screens)

Common combinations:
- d-none d-md-block: Hidden by default (mobile), visible on medium+
- d-md-none d-lg-block: Hidden on medium, visible on large
- d-none d-xl-flex: Hidden by default, visible as flex container on extra large

3. Visibility Control (Preserve Space)

For elements that are “invisible but still occupy space,” use invisible:

<div class="invisible">Content is invisible but still takes up space</div>
<div>Content below will display normally</div>

III. Summary

Bootstrap5 utility classes streamline styling with clear naming and responsive design. For margin/padding, remember “direction + size”; for show/hide, remember “d-* + breakpoint”. In practice, use these classes to quickly build pages, then combine with semantic HTML for structure clarity.

Practice combinations like mt-2 mx-auto to center buttons or d-sm-none to hide mobile navigation to familiarize yourself with usage scenarios.

Xiaoye