Skip to content

Commit fd49c06

Browse files
committed
Document the util() mixin
1 parent 91e25a3 commit fd49c06

2 files changed

Lines changed: 112 additions & 0 deletions

File tree

Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
---
2+
layout: docs
3+
title: Utility mixin
4+
description: Don't want to use utility classes directly in your HTML? Use the `util()` mixin to compose custom component styles in your source Sass files.
5+
group: utilities
6+
aliases: "/docs/5.1/utilities/mixin/"
7+
toc: true
8+
---
9+
10+
## How it works
11+
12+
Bootstrap generates hundreds of utilities to quickly and easily style elements through the addition of classes in your HTML. Now with Bootstrap v5.3.0, you can add utilities in your custom Sass with the `util()` mixin. Using the [utility API]({{< docsref "/utilities/api" >}}), we generate placeholders for every utility that can be included via Sass mixin. Use the mixin in your own Sass files to include quick styles, override defaults, or compose entire components.
13+
14+
```html
15+
<!-- Utility classes -->
16+
<div class="d-inline-flex p-4 mb-md-3">...</div>
17+
18+
<!-- Utility mixin -->
19+
<div class="test">...</div>
20+
```
21+
22+
## Motivation
23+
24+
There are two major motivations for creating the utility mixin:
25+
26+
1. **Bootstrap should be as approachable and useful to as many people as possible.** This is why we provide compiled ready-to-go distribution files alongside source Sass and JavaScript. Similarly, not every situation calls for only pre-built components or all utilities.
27+
28+
2. **Working with utilities is systems-based development.** No matter where you apply your styles, you can now think and develop with the same powerful system of responsive property-value pairings, in your HTML or in your Sass.
29+
30+
The `util()` mixin takes something familiar and brings it into a new context, allowing you to style with a utility-based approach no matter where you apply your styles without ever changing how you think about CSS. It makes Bootstrap even more flexible and powerful, without forcing your hand into a particular development methodology.
31+
32+
## Example
33+
34+
Consider this `.test` example. We've set the display, added padding, included a responsive margin, and a custom color.
35+
36+
```scss
37+
.test {
38+
@include util(d-inline-flex);
39+
@include util(p-4);
40+
@include util(mb-md-3);
41+
color: purple;
42+
}
43+
```
44+
45+
Which outputs to the following:
46+
47+
```css
48+
.test {
49+
display: inline-flex;
50+
padding: 1.5rem;
51+
color: purple;
52+
}
53+
54+
@media (min-width: 768px) {
55+
.test {
56+
margin-bottom: 1rem;
57+
}
58+
}
59+
```
60+
61+
## Working responsively
62+
63+
While you can include responsive utilities individually, Sass will generate multiple media queries each time you include a responsive utility with the `util()` mixin. When you have multiple responsive values, use media queries to group them.
64+
65+
```scss
66+
// Avoid this
67+
.test {
68+
@include util(p-3);
69+
@include util(p-md-5);
70+
@include util(mb-3);
71+
@include util(mb-md-5);
72+
}
73+
74+
// Do this
75+
.test {
76+
@include util(p-3);
77+
@include util(mb-3);
78+
79+
@include media-breakpoint-up(md) {
80+
@include util(p-5);
81+
@include util(mb-5);
82+
}
83+
}
84+
```
85+
86+
This way, you only output one media query in your compiled CSS and no further optimization or action is needed to clean up the output.
87+
88+
89+
## Custom setup
90+
91+
Want to use only the utilities and mixin in your own project? In your project's Sass file, include the following:
92+
93+
```scss
94+
// Required Bootstrap imports
95+
@import "bootstrap/scss/functions";
96+
@import "bootstrap/scss/variables";
97+
@import "bootstrap/scss/maps";
98+
@import "bootstrap/scss/mixins";
99+
100+
// Import the utilities maps and mixins
101+
@import "bootstrap/scss/utilities";
102+
@import "bootstrap/scss/utilities/api";
103+
104+
// Write your own styles
105+
.test {
106+
@include util(d-inline-flex);
107+
@include util(p-4);
108+
@include util(mb-md-3);
109+
color: purple;
110+
}
111+
```

site/data/sidebar.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,7 @@
9898
- title: Utilities
9999
pages:
100100
- title: API
101+
- title: Mixin
101102
- title: Background
102103
- title: Borders
103104
- title: Colors

0 commit comments

Comments
 (0)