2019-05-23 11:56:03 +02:00
---
layout: docs
title: Utility API
description: The utility API is a Sass based tool to generate utility classes.
group: utilities
2020-05-13 21:36:00 +02:00
aliases: "/docs/5.0/utilities/"
2019-05-23 11:56:03 +02:00
toc: true
---
2020-11-02 18:03:15 +01:00
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 ](https://sass-lang.com/documentation/values/maps ) to get started.
2019-05-23 11:56:03 +02:00
2020-11-02 18:03:15 +01:00
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:
2019-05-23 11:56:03 +02:00
2020-11-02 18:03:15 +01:00
{{< bs-table " table text-left " > }}
| Option | Type | Description |
| --- | --- | --- |
| `property` | **Required** | Name of the property, this can be a string or an array of strings (e.g., horizontal paddings or margins). |
| `values` | **Required** | List 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. |
| `class` | Optional | Variable 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. |
| `responsive` | Optional | Boolean indicating if responsive classes need to be generated. `false` by default. |
| `rfs` | Optional | Boolean to enable fluid rescaling. Have a look at the [RFS ]({{< docsref "/getting-started/rfs" >}} ) page to find out how this works. `false` by default. |
| `print` | Optional | Boolean indicating if print classes need to be generated. `false` by default. |
{{< / bs-table > }}
2019-07-18 10:35:20 +02:00
2020-11-02 18:03:15 +01:00
## API explained
2019-05-23 11:56:03 +02:00
2020-11-02 18:03:15 +01:00
All utility variables are added to the `$utilities` variable within our `_utilities.scss` stylesheet. Each group of utilities looks something like this:
2019-05-23 11:56:03 +02:00
```scss
$utilities: (
"opacity": (
property: opacity,
values: (
0: 0,
25: .25,
2020-07-12 15:58:58 +02:00
50: .5,
2020-06-28 21:36:57 +02:00
75: .75,
2019-05-23 11:56:03 +02:00
100: 1,
)
)
);
```
2020-11-02 18:03:15 +01:00
Which outputs the following:
2019-05-23 11:56:03 +02:00
```css
2020-11-02 18:03:15 +01:00
.opacity-0 { opacity: 0; }
.opacity-25 { opacity: .25; }
.opacity-50 { opacity: .5; }
.opacity-75 { opacity: .75; }
.opacity-100 { opacity: 1; }
2019-05-23 11:56:03 +02:00
```
2020-11-02 18:03:15 +01:00
### Custom class prefix
2019-05-23 11:56:03 +02:00
2020-11-02 18:03:15 +01:00
Use the `class` option to change the class prefix used in the compiled CSS:
2019-05-23 11:56:03 +02:00
```scss
$utilities: (
"opacity": (
property: opacity,
class: o,
values: (
0: 0,
25: .25,
2020-07-12 15:58:58 +02:00
50: .5,
2020-06-28 21:36:57 +02:00
75: .75,
2019-05-23 11:56:03 +02:00
100: 1,
)
)
);
```
Output:
```css
2020-11-02 18:03:15 +01:00
.o-0 { opacity: 0; }
.o-25 { opacity: .25; }
.o-50 { opacity: .5; }
.o-75 { opacity: .75; }
.o-100 { opacity: 1; }
2019-05-23 11:56:03 +02:00
```
2020-11-02 18:03:15 +01:00
### Responsive utilities
2019-05-23 11:56:03 +02:00
2020-11-02 18:03:15 +01:00
Add the `responsive` boolean to generate responsive utilities (e.g., `.opacity-md-25` ) across [all breakpoints ]({{< docsref "/layout/breakpoints" >}} ).
2019-05-23 11:56:03 +02:00
```scss
$utilities: (
"opacity": (
property: opacity,
responsive: true,
values: (
0: 0,
25: .25,
2020-07-12 15:58:58 +02:00
50: .5,
2020-06-28 21:36:57 +02:00
75: .75,
2019-05-23 11:56:03 +02:00
100: 1,
)
)
);
```
Output:
```css
2020-11-02 18:03:15 +01:00
.opacity-0 { opacity: 0; }
.opacity-25 { opacity: .25; }
.opacity-50 { opacity: .5; }
.opacity-75 { opacity: .75; }
.opacity-100 { opacity: 1; }
2019-05-23 11:56:03 +02:00
@media (min-width: 576px) {
2020-11-02 18:03:15 +01:00
.opacity-sm-0 { opacity: 0; }
.opacity-sm-25 { opacity: .25; }
.opacity-sm-50 { opacity: .5; }
.opacity-sm-75 { opacity: .75; }
.opacity-sm-100 { opacity: 1; }
2019-05-23 11:56:03 +02:00
}
2020-11-02 18:03:15 +01:00
2019-05-23 11:56:03 +02:00
@media (min-width: 768px) {
2020-11-02 18:03:15 +01:00
.opacity-md-0 { opacity: 0; }
.opacity-md-25 { opacity: .25; }
.opacity-md-50 { opacity: .5; }
.opacity-md-75 { opacity: .75; }
.opacity-md-100 { opacity: 1; }
2019-05-23 11:56:03 +02:00
}
2020-11-02 18:03:15 +01:00
2019-05-23 11:56:03 +02:00
@media (min-width: 992px) {
2020-11-02 18:03:15 +01:00
.opacity-lg-0 { opacity: 0; }
.opacity-lg-25 { opacity: .25; }
.opacity-lg-50 { opacity: .5; }
.opacity-lg-75 { opacity: .75; }
.opacity-lg-100 { opacity: 1; }
2019-05-23 11:56:03 +02:00
}
2020-11-02 18:03:15 +01:00
2019-05-23 11:56:03 +02:00
@media (min-width: 1200px) {
2020-11-02 18:03:15 +01:00
.opacity-xl-0 { opacity: 0; }
.opacity-xl-25 { opacity: .25; }
.opacity-xl-50 { opacity: .5; }
.opacity-xl-75 { opacity: .75; }
.opacity-xl-100 { opacity: 1; }
}
@media (min-width: 1400px) {
.opacity-xxl-0 { opacity: 0; }
.opacity-xxl-25 { opacity: .25; }
.opacity-xxl-50 { opacity: .5; }
.opacity-xxl-75 { opacity: .75; }
.opacity-xxl-100 { opacity: 1; }
2019-05-23 11:56:03 +02:00
}
```
2020-11-02 18:03:15 +01:00
### Changing utilities
2019-05-23 11:56:03 +02:00
2020-11-02 18:03:15 +01:00
Override existing utilities by using the same key. For example, if you want additional responsive overflow utility classes, you can do this:
2019-05-23 11:56:03 +02:00
```scss
$utilities: (
"overflow": (
responsive: true,
property: overflow,
values: visible hidden scroll auto,
),
);
```
2020-11-02 18:03:15 +01:00
### Print utilities
2019-05-23 11:56:03 +02:00
2020-11-02 18:03:15 +01:00
Enabling the `print` option will **also** generate utility classes for print, which are only applied within the `@media print { ... }` media query.
2019-05-23 11:56:03 +02:00
```scss
$utilities: (
"opacity": (
property: opacity,
print: true,
values: (
0: 0,
25: .25,
2020-07-12 15:58:58 +02:00
50: .5,
2020-06-28 21:36:57 +02:00
75: .75,
2019-05-23 11:56:03 +02:00
100: 1,
)
)
);
```
Output:
```css
2020-11-02 18:03:15 +01:00
.opacity-0 { opacity: 0; }
.opacity-25 { opacity: .25; }
.opacity-50 { opacity: .5; }
.opacity-75 { opacity: .75; }
.opacity-100 { opacity: 1; }
2019-05-23 11:56:03 +02:00
@media print {
2020-11-02 18:03:15 +01:00
.opacity-print-0 { opacity: 0; }
.opacity-print-25 { opacity: .25; }
.opacity-print-50 { opacity: .5; }
.opacity-print-75 { opacity: .75; }
.opacity-print-100 { opacity: 1; }
2019-05-23 11:56:03 +02:00
}
```
2020-11-02 18:03:15 +01:00
### Remove utilities
2019-05-23 11:56:03 +02:00
Utilities can also be removed by changing the group key to `null` :
```scss
$utilities: (
"float": null,
);
```