Using Bootstrap 5's Utility API to Create Custom Spacing Utilities
Learn how to add custom spacing, color, or typography utilities to Bootstrap 5 using its Sass Utility API, with a step‑by‑step example and notes on recompilation and naming conflicts.
04 Sept 2026, 20:34 UTC

The problem: you need a spacing utility that Bootstrap doesn’t ship
When building a UI, you often find yourself writing one‑off CSS like .my-spacer-5 { margin: 1.5rem; } just to get a specific gap that isn’t covered by Bootstrap’s default spacing scale. Adding that rule manually means you have to maintain it separately, and any theme changes won’t automatically affect it.
Thesis: Bootstrap 5’s Utility API lets you declare such utilities in Sass, keeping them in sync with the framework’s design tokens and responsive behavior.
How the Utility API works
Bootstrap stores its utility classes in a Sass map named $utilities. You can extend this map with the helper functions addUtility(), removeUtility(), and modifyUtility(). When you recompile the Sass, Bootstrap generates the corresponding CSS classes, automatically adding responsive variants if $enable-responsive-utilities is true.
Worked example: adding a custom margin utility
Create a Sass file that imports Bootstrap’s source and then adds the utility. For instance,
custom-utilities.scss:// 1. Import Bootstrap’s Sass (adjust the path to your node_modules) @import "../node_modules/bootstrap/scss/bootstrap"; // 2. Define a new utility map entry $my-utilities: ( "spacer-5": ( property: margin, values: 5: 1.5rem ) ); // 3. Merge it into Bootstrap’s $utilities map @import "bootstrap/scss/utilities/api"; $utilities: map-merge($utilities, $my-utilities); // 4. Re‑import the utilities to generate CSS @import "bootstrap/scss/utilities";Compile the Sass to CSS. If you have the Dart Sass CLI installed, run:
sass custom-utilities.scss dist/custom.css --style=compressedYou need read access to the source files and write access to the output directory; no special privileges are required.
Inspect the generated CSS. You should see something like:
.my-spacer-5{margin:1.5rem} @media (min-width:576px){ .my-sm-spacer-5{margin:1.5rem} } @media (min-width:768px){ .my-md-spacer-5{margin:1.5rem} } /* …breakpoints continue… */The class name follows Bootstrap’s convention (
my-for margin on the y‑axis) and responsive variants appear automatically.
Trade‑off and limitation
You must recompile the Sass source after any change to
$utilities. If you forget this step, the browser will never see the new utility classes, leading to confusion.Naming clashes are possible. If you add a utility with a name that already exists (e.g.,
pt-4), the later definition overrides the earlier one, which can break existing layouts.
Actionable closing
Start by identifying a recurring style you’re writing in plain CSS. Convert it into a utility entry using the pattern above, recompile, and verify the output. Keep the utility map in a dedicated partial (like _utilities-custom.scss) so you can track changes alongside your project’s Sass. This approach keeps your CSS DRY, ensures theme updates propagate, and avoids scattering one‑off rules throughout your codebase.
0 replies
A thoughtful contribution can make all the difference. Be the first to share one.