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:

OptionTypeDescription
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, it isn’t compiled.
classOptionalVariable for the class name if you don’t want it to be the same as the property. In case you don’t provide the class key and property key is an array of strings, the class name will be the first element of the property array.
stateOptionalList of pseudo-class variants like :hover or :focus to generate for the utility. No default value.
responsiveOptionalBoolean indicating if responsive classes need to be generated. false by default.
rfsOptionalBoolean to enable fluid rescaling. Have a look at the RFS page to find out how this works. false by default.
printOptionalBoolean indicating if print classes need to be generated. false by default.
rtlOptionalBoolean indicating if utility should be kept in RTL. true by default.

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; }

Custom class prefix

Use the class option to change the class prefix used in the compiled CSS:

  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; }
  2. .o-25 { opacity: .25; }
  3. .o-50 { opacity: .5; }
  4. .o-75 { opacity: .75; }
  5. .o-100 { opacity: 1; }

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; }
  2. .opacity-25-hover:hover { opacity: .25; }
  3. .opacity-50-hover:hover { opacity: .5; }
  4. .opacity-75-hover:hover { opacity: .75; }
  5. .opacity-100-hover:hover { opacity: 1; }

Responsive utilities

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

Changing 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. );

Print utilities

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; }
  2. .opacity-25 { opacity: .25; }
  3. .opacity-50 { opacity: .5; }
  4. .opacity-75 { opacity: .75; }
  5. .opacity-100 { opacity: 1; }
  6. @media print {
  7. .opacity-print-0 { opacity: 0; }
  8. .opacity-print-25 { opacity: .25; }
  9. .opacity-print-50 { opacity: .5; }
  10. .opacity-print-75 { opacity: .75; }
  11. .opacity-print-100 { opacity: 1; }
  12. }

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.

Add utilities

New utilities can be added to the default $utilities map with a map-merge. Make sure our _utilities.scss is 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/utilities";
  2. $utilities: map-merge(
  3. $utilities,
  4. (
  5. "cursor": (
  6. property: cursor,
  7. class: cursor
  8. responsive: true,
  9. values: auto pointer grab,
  10. )
  11. )
  12. );

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/utilities";
  2. $utilities: map-merge(
  3. $utilities,
  4. (
  5. "width": map-merge(
  6. map-get($utilities, "width"),
  7. (
  8. values: map-merge(
  9. map-get(map-get($utilities, "width"), "values"),
  10. (10: 10%),
  11. ),
  12. ),
  13. ),
  14. )
  15. );

Remove utilities

Remove any of the default utilities by setting the group key to null. For example, to remove all our width utilities, create a $utilities map-merge and add "width": null within.

  1. @import "bootstrap/scss/utilities";
  2. $utilities: map-merge(
  3. $utilities,
  4. (
  5. "width": null
  6. )
  7. );

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.