API
Bootstrap 5 API
The utility API is a Sass-based tool to generate utility classes.
If you need more extensive support and step-by-step guidance, check out our dedicated tutorial.
Bootstrap utilities are generated with our utility API 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:
-
property:Name of the property, this can be a string or an array of strings (e.g., horizontal paddings or margins). -
responsive(optional): Boolean indicating if responsive classes need to be generated.falseby default. -
rfs(optional): Boolean to enable fluid rescaling with RFS.falseby default. -
class(optional):Name of the generated class. If not provided andpropertyis an array of strings,classwill default to the first element of thepropertyarray.nullby default. -
state(optional): List of pseudo-class variants like :hover or :focus to generate for the utility. No default value. -
values: List of values, or a map if you don't want the class name to be the same as the value. Ifnullis used as map key, it isn't compiled. -
print(optional): Boolean indicating if print classes need to be generated.falseby default. -
rtl(optional): Boolean indicating if utility should be kept in RTL.trueby default. -
css-var(optional): Boolean to generate CSS variables instead of CSS rules.falseby default. -
local-vars(optional):Map of local CSS variables to generate in addition to the CSS rules.falseby 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:
Which outputs the following:
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:
Output:
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:
As a map, like with opacity utilities:
As a Sass variable that sets the list or map, as in our position utilities:
Class
Use the class option to change the class prefix used in the compiled CSS. For example, to change from .opacity-* to .o-*:
Output:
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. Consider our .text-opacity-* utilities:
Output:
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:
Output:
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.
Output:
Responsive
Add the responsive boolean to generate responsive utilities (e.g., .opacity-md-25) across all breakpoints.
Output:
Enabling the print option will also generate utility classes for print, which are only applied within the @media print { ... } media query.
Output:
Override utilities
Override existing utilities by using the same key. For example, if you want additional responsive overflow utility classes, you can do this:
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.
Add utilities
New utilities can be added to the default $utilities map with a map-merge. For example, here's how to add a responsive cursor utility with three values.
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.
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:
This will now generate responsive variations of .border and .border-0 for each breakpoint. Your generated CSS will look like this:
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.