View on GitHub

Utility API

The utility API is a Sass-based tool to generate utility classes.

Bootstrap utilities are generated with our utility API and can be used to modify or extend our default set of utility classes via Sass. Our utility API is based on a series of Sass maps and functions for generating families of classes with various options. If you’re unfamiliar with Sass maps, read up on the official Sass docs to get started.

The $utilities map contains all our utilities and is later merged with your custom $utilities map, if present. The utility map contains a keyed list of utility groups which accept the following options:

OptionTypeDefault valueDescription
propertyRequiredName of the property, this can be a string or an array of strings (e.g., horizontal paddings or margins).
valuesRequiredList of values, or a map if you don’t want the class name to be the same as the value. If null is used as map key, class is not prepended to the class name.
classOptionalnullName of the generated class. If not provided and property is an array of strings, class will default to the first element of the property array. If not provided and property is a string, the values keys are used for the class names.
css-varOptionalfalseBoolean to generate CSS variables instead of CSS rules.
css-variable-nameOptionalnullCustom un-prefixed name for the CSS variable inside the ruleset.
local-varsOptionalnullMap of local CSS variables to generate in addition to the CSS rules.
stateOptionalnullList of pseudo-class variants (e.g., :hover or :focus) to generate.
responsiveOptionalfalseBoolean indicating if responsive classes should be generated.
rfsOptionalfalseBoolean to enable fluid rescaling with RFS.
printOptionalfalseBoolean indicating if print classes need to be generated.
rtlOptionaltrueBoolean indicating if utility should be kept in RTL.

API explained

All utility variables are added to the $utilities variable within our _utilities.scss stylesheet. Each group of utilities looks something like this:

  1. $utilities: (
  2. "opacity": (
  3. property: opacity,
  4. values: (
  5. 0: 0,
  6. 25: .25,
  7. 50: .5,
  8. 75: .75,
  9. 100: 1,
  10. )
  11. )
  12. );

Which outputs the following:

  1. .opacity-0 { opacity: 0; }
  2. .opacity-25 { opacity: .25; }
  3. .opacity-50 { opacity: .5; }
  4. .opacity-75 { opacity: .75; }
  5. .opacity-100 { opacity: 1; }

Property

The required property key must be set for any utility, and it must contain a valid CSS property. This property is used in the generated utility’s ruleset. When the class key is omitted, it also serves as the default class name. Consider the text-decoration utility:

  1. $utilities: (
  2. "text-decoration": (
  3. property: text-decoration,
  4. values: none underline line-through
  5. )
  6. );

Output:

  1. .text-decoration-none { text-decoration: none !important; }
  2. .text-decoration-underline { text-decoration: underline !important; }
  3. .text-decoration-line-through { text-decoration: line-through !important; }

Values

Use the values key to specify which values for the specified property should be used in the generated class names and rules. Can be a list or map (set in the utilities or in a Sass variable).

As a list, like with text-decoration utilities:

  1. values: none underline line-through

As a map, like with opacity utilities:

  1. values: (
  2. 0: 0,
  3. 25: .25,
  4. 50: .5,
  5. 75: .75,
  6. 100: 1,
  7. )

As a Sass variable that sets the list or map, as in our position utilities:

  1. values: $position-values

Class

Use the class option to change the class prefix used in the compiled CSS. For example, to change from .opacity-* to .o-*:

  1. $utilities: (
  2. "opacity": (
  3. property: opacity,
  4. class: o,
  5. values: (
  6. 0: 0,
  7. 25: .25,
  8. 50: .5,
  9. 75: .75,
  10. 100: 1,
  11. )
  12. )
  13. );

Output:

  1. .o-0 { opacity: 0 !important; }
  2. .o-25 { opacity: .25 !important; }
  3. .o-50 { opacity: .5 !important; }
  4. .o-75 { opacity: .75 !important; }
  5. .o-100 { opacity: 1 !important; }

If class: null, generates classes for each of the values keys:

  1. $utilities: (
  2. "visibility": (
  3. property: visibility,
  4. class: null,
  5. values: (
  6. visible: visible,
  7. invisible: hidden,
  8. )
  9. )
  10. );

Output:

  1. .visible { visibility: visible !important; }
  2. .invisible { visibility: hidden !important; }

CSS variable utilities

Set the css-var boolean option to true and the API will generate local CSS variables for the given selector instead of the usual property: value rules. Add an optional css-variable-name to set a different CSS variable name than the class name.

Consider our .text-opacity-* utilities. If we add the css-variable-name option, we’ll get a custom output.

  1. $utilities: (
  2. "text-opacity": (
  3. css-var: true,
  4. css-variable-name: text-alpha,
  5. class: text-opacity,
  6. values: (
  7. 25: .25,
  8. 50: .5,
  9. 75: .75,
  10. 100: 1
  11. )
  12. ),
  13. );

Output:

  1. .text-opacity-25 { --bs-text-alpha: .25; }
  2. .text-opacity-50 { --bs-text-alpha: .5; }
  3. .text-opacity-75 { --bs-text-alpha: .75; }
  4. .text-opacity-100 { --bs-text-alpha: 1; }

Local CSS variables

Use the local-vars option to specify a Sass map that will generate local CSS variables within the utility class’s ruleset. Please note that it may require additional work to consume those local CSS variables in the generated CSS rules. For example, consider our .bg-* utilities:

  1. $utilities: (
  2. "background-color": (
  3. property: background-color,
  4. class: bg,
  5. local-vars: (
  6. "bg-opacity": 1
  7. ),
  8. values: map-merge(
  9. $utilities-bg-colors,
  10. (
  11. "transparent": transparent
  12. )
  13. )
  14. )
  15. );

Output:

  1. .bg-primary {
  2. --bs-bg-opacity: 1;
  3. background-color: rgba(var(--bs-primary-rgb), var(--bs-bg-opacity)) !important;
  4. }

States

Use the state option to generate pseudo-class variations. Example pseudo-classes are :hover and :focus. When a list of states are provided, classnames are created for that pseudo-class. For example, to change opacity on hover, add state: hover and you’ll get .opacity-hover:hover in your compiled CSS.

Need multiple pseudo-classes? Use a space-separated list of states: state: hover focus.

  1. $utilities: (
  2. "opacity": (
  3. property: opacity,
  4. class: opacity,
  5. state: hover,
  6. values: (
  7. 0: 0,
  8. 25: .25,
  9. 50: .5,
  10. 75: .75,
  11. 100: 1,
  12. )
  13. )
  14. );

Output:

  1. .opacity-0-hover:hover { opacity: 0 !important; }
  2. .opacity-25-hover:hover { opacity: .25 !important; }
  3. .opacity-50-hover:hover { opacity: .5 !important; }
  4. .opacity-75-hover:hover { opacity: .75 !important; }
  5. .opacity-100-hover:hover { opacity: 1 !important; }

Responsive

Add the responsive boolean to generate responsive utilities (e.g., .opacity-md-25) across all breakpoints.

  1. $utilities: (
  2. "opacity": (
  3. property: opacity,
  4. responsive: true,
  5. values: (
  6. 0: 0,
  7. 25: .25,
  8. 50: .5,
  9. 75: .75,
  10. 100: 1,
  11. )
  12. )
  13. );

Output:

  1. .opacity-0 { opacity: 0 !important; }
  2. .opacity-25 { opacity: .25 !important; }
  3. .opacity-50 { opacity: .5 !important; }
  4. .opacity-75 { opacity: .75 !important; }
  5. .opacity-100 { opacity: 1 !important; }
  6. @media (min-width: 576px) {
  7. .opacity-sm-0 { opacity: 0 !important; }
  8. .opacity-sm-25 { opacity: .25 !important; }
  9. .opacity-sm-50 { opacity: .5 !important; }
  10. .opacity-sm-75 { opacity: .75 !important; }
  11. .opacity-sm-100 { opacity: 1 !important; }
  12. }
  13. @media (min-width: 768px) {
  14. .opacity-md-0 { opacity: 0 !important; }
  15. .opacity-md-25 { opacity: .25 !important; }
  16. .opacity-md-50 { opacity: .5 !important; }
  17. .opacity-md-75 { opacity: .75 !important; }
  18. .opacity-md-100 { opacity: 1 !important; }
  19. }
  20. @media (min-width: 992px) {
  21. .opacity-lg-0 { opacity: 0 !important; }
  22. .opacity-lg-25 { opacity: .25 !important; }
  23. .opacity-lg-50 { opacity: .5 !important; }
  24. .opacity-lg-75 { opacity: .75 !important; }
  25. .opacity-lg-100 { opacity: 1 !important; }
  26. }
  27. @media (min-width: 1200px) {
  28. .opacity-xl-0 { opacity: 0 !important; }
  29. .opacity-xl-25 { opacity: .25 !important; }
  30. .opacity-xl-50 { opacity: .5 !important; }
  31. .opacity-xl-75 { opacity: .75 !important; }
  32. .opacity-xl-100 { opacity: 1 !important; }
  33. }
  34. @media (min-width: 1400px) {
  35. .opacity-xxl-0 { opacity: 0 !important; }
  36. .opacity-xxl-25 { opacity: .25 !important; }
  37. .opacity-xxl-50 { opacity: .5 !important; }
  38. .opacity-xxl-75 { opacity: .75 !important; }
  39. .opacity-xxl-100 { opacity: 1 !important; }
  40. }

Print

Enabling the print option will also generate utility classes for print, which are only applied within the @media print { ... } media query.

  1. $utilities: (
  2. "opacity": (
  3. property: opacity,
  4. print: true,
  5. values: (
  6. 0: 0,
  7. 25: .25,
  8. 50: .5,
  9. 75: .75,
  10. 100: 1,
  11. )
  12. )
  13. );

Output:

  1. .opacity-0 { opacity: 0 !important; }
  2. .opacity-25 { opacity: .25 !important; }
  3. .opacity-50 { opacity: .5 !important; }
  4. .opacity-75 { opacity: .75 !important; }
  5. .opacity-100 { opacity: 1 !important; }
  6. @media print {
  7. .opacity-print-0 { opacity: 0 !important; }
  8. .opacity-print-25 { opacity: .25 !important; }
  9. .opacity-print-50 { opacity: .5 !important; }
  10. .opacity-print-75 { opacity: .75 !important; }
  11. .opacity-print-100 { opacity: 1 !important; }
  12. }

Importance

All utilities generated by the API include !important to ensure they override components and modifier classes as intended. You can toggle this setting globally with the $enable-important-utilities variable (defaults to true).

Using the API

Now that you’re familiar with how the utilities API works, learn how to add your own custom classes and modify our default utilities.

Override utilities

Override existing utilities by using the same key. For example, if you want additional responsive overflow utility classes, you can do this:

  1. $utilities: (
  2. "overflow": (
  3. responsive: true,
  4. property: overflow,
  5. values: visible hidden scroll auto,
  6. ),
  7. );

Add utilities

New utilities can be added to the default $utilities map with a map-merge. Make sure our required Sass files and _utilities.scss are imported first, then use the map-merge to add your additional utilities. For example, here’s how to add a responsive cursor utility with three values.

  1. @import "bootstrap/scss/functions";
  2. @import "bootstrap/scss/variables";
  3. @import "bootstrap/scss/maps";
  4. @import "bootstrap/scss/mixins";
  5. @import "bootstrap/scss/utilities";
  6. $utilities: map-merge(
  7. $utilities,
  8. (
  9. "cursor": (
  10. property: cursor,
  11. class: cursor,
  12. responsive: true,
  13. values: auto pointer grab,
  14. )
  15. )
  16. );
  17. @import "bootstrap/scss/utilities/api";

Modify utilities

Modify existing utilities in the default $utilities map with map-get and map-merge functions. In the example below, we’re adding an additional value to the width utilities. Start with an initial map-merge and then specify which utility you want to modify. From there, fetch the nested "width" map with map-get to access and modify the utility’s options and values.

  1. @import "bootstrap/scss/functions";
  2. @import "bootstrap/scss/variables";
  3. @import "bootstrap/scss/maps";
  4. @import "bootstrap/scss/mixins";
  5. @import "bootstrap/scss/utilities";
  6. $utilities: map-merge(
  7. $utilities,
  8. (
  9. "width": map-merge(
  10. map-get($utilities, "width"),
  11. (
  12. values: map-merge(
  13. map-get(map-get($utilities, "width"), "values"),
  14. (10: 10%),
  15. ),
  16. ),
  17. ),
  18. )
  19. );
  20. @import "bootstrap/scss/utilities/api";

Enable responsive

You can enable responsive classes for an existing set of utilities that are not currently responsive by default. For example, to make the border classes responsive:

  1. @import "bootstrap/scss/functions";
  2. @import "bootstrap/scss/variables";
  3. @import "bootstrap/scss/maps";
  4. @import "bootstrap/scss/mixins";
  5. @import "bootstrap/scss/utilities";
  6. $utilities: map-merge(
  7. $utilities, (
  8. "border": map-merge(
  9. map-get($utilities, "border"),
  10. ( responsive: true ),
  11. ),
  12. )
  13. );
  14. @import "bootstrap/scss/utilities/api";

This will now generate responsive variations of .border and .border-0 for each breakpoint. Your generated CSS will look like this:

  1. .border { ... }
  2. .border-0 { ... }
  3. @media (min-width: 576px) {
  4. .border-sm { ... }
  5. .border-sm-0 { ... }
  6. }
  7. @media (min-width: 768px) {
  8. .border-md { ... }
  9. .border-md-0 { ... }
  10. }
  11. @media (min-width: 992px) {
  12. .border-lg { ... }
  13. .border-lg-0 { ... }
  14. }
  15. @media (min-width: 1200px) {
  16. .border-xl { ... }
  17. .border-xl-0 { ... }
  18. }
  19. @media (min-width: 1400px) {
  20. .border-xxl { ... }
  21. .border-xxl-0 { ... }
  22. }

Rename utilities

Missing v4 utilities, or used to another naming convention? The utilities API can be used to override the resulting class of a given utility—for example, to rename .ms-* utilities to oldish .ml-*:

  1. @import "bootstrap/scss/functions";
  2. @import "bootstrap/scss/variables";
  3. @import "bootstrap/scss/maps";
  4. @import "bootstrap/scss/mixins";
  5. @import "bootstrap/scss/utilities";
  6. $utilities: map-merge(
  7. $utilities, (
  8. "margin-start": map-merge(
  9. map-get($utilities, "margin-start"),
  10. ( class: ml ),
  11. ),
  12. )
  13. );
  14. @import "bootstrap/scss/utilities/api";

Remove utilities

Remove any of the default utilities with the map-remove() Sass function.

  1. @import "bootstrap/scss/functions";
  2. @import "bootstrap/scss/variables";
  3. @import "bootstrap/scss/maps";
  4. @import "bootstrap/scss/mixins";
  5. @import "bootstrap/scss/utilities";
  6. // Remove multiple utilities with a comma-separated list
  7. $utilities: map-remove($utilities, "width", "float");
  8. @import "bootstrap/scss/utilities/api";

You can also use the map-merge() Sass function and set the group key to null to remove the utility.

  1. @import "bootstrap/scss/functions";
  2. @import "bootstrap/scss/variables";
  3. @import "bootstrap/scss/maps";
  4. @import "bootstrap/scss/mixins";
  5. @import "bootstrap/scss/utilities";
  6. $utilities: map-merge(
  7. $utilities,
  8. (
  9. "width": null
  10. )
  11. );
  12. @import "bootstrap/scss/utilities/api";

Add, remove, modify

You can add, remove, and modify many utilities all at once with the map-merge() Sass function. Here’s how you can combine the previous examples into one larger map.

  1. @import "bootstrap/scss/functions";
  2. @import "bootstrap/scss/variables";
  3. @import "bootstrap/scss/maps";
  4. @import "bootstrap/scss/mixins";
  5. @import "bootstrap/scss/utilities";
  6. $utilities: map-merge(
  7. $utilities,
  8. (
  9. // Remove the `width` utility
  10. "width": null,
  11. // Make an existing utility responsive
  12. "border": map-merge(
  13. map-get($utilities, "border"),
  14. ( responsive: true ),
  15. ),
  16. // Add new utilities
  17. "cursor": (
  18. property: cursor,
  19. class: cursor,
  20. responsive: true,
  21. values: auto pointer grab,
  22. )
  23. )
  24. );
  25. @import "bootstrap/scss/utilities/api";

Remove utility in RTL

Some edge cases make RTL styling difficult, such as line breaks in Arabic. Thus utilities can be dropped from RTL output by setting the rtl option to false:

  1. $utilities: (
  2. "word-wrap": (
  3. property: word-wrap word-break,
  4. class: text,
  5. values: (break: break-word),
  6. rtl: false
  7. ),
  8. );

Output:

  1. /* rtl:begin:remove */
  2. .text-break {
  3. word-wrap: break-word !important;
  4. word-break: break-word !important;
  5. }
  6. /* rtl:end:remove */

This doesn’t output anything in RTL, thanks to the RTLCSS remove control directive.